开发Spring Boot + Spring Data Redis
示例时出现以下错误。在此示例中,我正在寻求实现contains
按spring
doc不可能实现的方法,并在此处链接:[原因:java.lang.IllegalArgumentException:CONTAINING(1):Redis查询不支持IsContaining,Containing,Contains]派生
Redis将根据匹配的详细信息提取数据。
***************************
APPLICATION FAILED TO START
***************************
Description:
Field redisTemplate in com.baeldung.MainAppDemo required a bean of type 'org.springframework.data.redis.core.RedisTemplate' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
The following candidates were found but could not be injected:
- Bean method 'redisTemplate' in 'RedisAutoConfiguration' not loaded because @ConditionalOnMissingBean (names: redisTemplate; SearchStrategy: all) found beans named redisTemplate
Action:
Consider revisiting the entries above or defining a bean of type 'org.springframework.data.redis.core.RedisTemplate' in your configuration.
我已经实现了如下 MainAppDemo.java的* 方法 *
@SpringBootApplication
public class MainAppDemo implements CommandLineRunner{
@Autowired
private UserRepository userRepository;
@Autowired
private RedisTemplate<String, Object> redisTemplate;
public static void main(String[] args) {
SpringApplication.run(MainAppDemo.class, args);
}
@Override
public void run(String... args) throws Exception {
Role role1 = Role.builder().id("R1").roleName("ADMIN").build();
User user1 = User.builder().firstName("Matt").middleName("Mike").lastName("Wixson").role(role1).build();
Role role2 = Role.builder().id("R2").roleName("API").build();
User user2 = User.builder().firstName("John").middleName("Lima").lastName("Kerr").role(role2).build();
userRepository.save(user1);
userRepository.save(user2);
List<User> u = userRepository.findByFirstNameAndLastName("Matt", "Wixson");
System.out.println("Find By First Name and Last Name = "+u.toString());
final String key = String.format("user:%s", "J");
final String firstName = (String) redisTemplate.opsForHash().get(key, "firstName");
final String middleName = (String) redisTemplate.opsForHash().get(key, "middleName");
final String lastName = (String) redisTemplate.opsForHash().get(key, "lastName");
User user = User.builder().firstName(firstName).middleName(middleName).lastName(lastName).build();
System.out.println("Custom Redis Template Example ="+user.toString());
List<User> adminUser = userRepository.findByRole_RoleName("ADMIN");
System.out.println("ADMIN USER ="+adminUser);
}
}
RedisConfig.java
@Configuration
@ConfigurationProperties
@EnableRedisRepositories("com.baeldung.spring.data.redis.repository")
public class RedisConfig {
@Bean
JedisConnectionFactory jedisConnectionFactory() {
return new JedisConnectionFactory();
}
@Bean
RedisTemplate<Object, Object> redisTemplate() {
final RedisTemplate<Object, Object> template = new RedisTemplate<>();
template.setConnectionFactory(jedisConnectionFactory());
template.setKeySerializer(new StringRedisSerializer());
template.setHashValueSerializer(new GenericToStringSerializer<Object>(Object.class));
template.setValueSerializer(new GenericToStringSerializer<Object>(Object.class));
return template;
}
}
User.java
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@RedisHash("user")
public class User {
private @Id String id;
private @Indexed String firstName;
private @Indexed String middleName;
private @Indexed String lastName;
private Role role;
}
角色.java
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@RedisHash("Role")
public class Role {
private @Id String id;
private @Indexed String roleName;
}
好的,我找到了解决方案。我应该用
@Autowired
private RedisTemplate<Object, Object> redisTemplate;
代替
@Autowired
private RedisTemplate<String, Object> redisTemplate;
下午好,我正在尝试使用Spring Boot运行SOAP服务,但出现以下错误: 应用程序启动失败 描述: 我知道也许这应该是个愚蠢的错误,但我看不出来 附件MI代码: SpringBootSoapApp.java ClienteServiceImpl.java client.xsd 应用程序.属性 我的项目结构 我试图遵循以下示例:https://www.javaspringclub.com/pu
描述:com.mongotest.demo.seeder中构造函数的参数0需要类型为“com.mongotest.repositories.studentrepository”的bean,但找不到该bean。 pom.xml
我有 ProductController.java ProductService.java ProductServiceImpl.java 这是我的错误: 我有完整日志:https://gist.github.com/donhuvy/b918e20eeeb7cbe3c4be4167d066f7fd 这是我的完整源代码https://github.com/donhuvy/accounting/com
我有一个Java Spring Boot项目,我正在其中创建一个post请求。代码如下所示: 主要: 但是,当我运行该项目时,我会得到以下错误: 感谢您对此的任何帮助!非常感谢,
应用程序启动失败 考虑在您的配置中定义一个类型为'com.service.adminService'的bean。