将Spring数据Rest存储库注入实用程序类
在搜索了一天之后,尝试了所有配置应用程序上下文和注释autowire、inject、component等,我似乎无法让我的pojo类成功地注入一个工作存储库-该值始终为空。 我开始怀疑注入控制器之外的任何东西是否与SpringDataREST的体系结构相反 以下是我从spring文档中获取的应用程序上下文:将Spring数据Rest存储库注入实用程序类,spring,dependency-injection,spring-boot,spring-data-rest,Spring,Dependency Injection,Spring Boot,Spring Data Rest,在搜索了一天之后,尝试了所有配置应用程序上下文和注释autowire、inject、component等,我似乎无法让我的pojo类成功地注入一个工作存储库-该值始终为空。 我开始怀疑注入控制器之外的任何东西是否与SpringDataREST的体系结构相反 以下是我从spring文档中获取的应用程序上下文: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/sc
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">
<jpa:repositories base-package="com.test.springservice"/>
</beans>
我的工作存储库和模型:
package com.test.springservice.greek;
import ...
@RepositoryRestResource
public interface GreekLetterRepository extends CrudRepository<GreekLetter, Integer> {
@Query("SELECT l FROM GreekLetter l WHERE l.translit Like :translit% ORDER BY length(l.translit) Desc")
public List<GreekLetter> findByTranslitStartsWith(@Param("translit") String translit);
}
package com.test.springservice.greek.model;
import ....
@Entity
@Table(name="letters", catalog="greek")
public class GreekLetter extends Letter {
public GreekLetter() {}
public GreekLetter(String name, String translit, String present, String types) { super(name,translit,present,types); }
}
最后,我无法将存储库注入的类:
package com.test.springservice.greek.model;
import ...
public class GreekString extends Letters {
@Autowired
public GreekLetterRepository repository; // this is null (class not managed by container)
public GreekString(String str) {
super();
setTranslit(str);
if (this.getTranslit().equals(this.getPresent())) { setPresent(str); }
}
public void setTranslit(String str) { // litterates from str as translit
List<Letter> lets = new ArrayList<Letter>();
for (int i = 0; i < str.length(); i++) {
String partialWord = str.substring(i);
String partialWord0 = partialWord.substring(0,1);
List<GreekLetter> potentialMatches = repository.findByTranslitStartsWith(partialWord0);
....
}
....
}
....
}
有人看到这种方法的基本缺陷吗
提前感谢。问题是,在Spring有机会通过@Autowired注释将存储库注入之前,您正在使用存储库 一种修复方法是使用GreekLetterRepository的基于构造函数的连接: 公共GreekStringString str,GreekLetterRepository GreekLetterRepository 我看不出您在哪里实例化这个bean,但如果它在java配置中,您可以这样做:
@Bean
public GreekString something(GreekLetterRepository greekLetterRepository) {
return GreekString("something", greekLetterRepository);
}
public class GreekString extends Letters {
@Autowired
public GreekLetterRepository repository;
public GreekString(String str) {
super();
}
.....
@PostConstruct
public void init() {
setTranslit(str);
...
}
}
现在它应该可以干净地工作了
但是,我建议不要在构造函数中立即使用存储库,使用依赖bean的更好地方是在整个bean完全初始化之后,您可以用@PostConstruct注释一个方法,在bean完全初始化之后调用,方法如下:
@Bean
public GreekString something(GreekLetterRepository greekLetterRepository) {
return GreekString("something", greekLetterRepository);
}
public class GreekString extends Letters {
@Autowired
public GreekLetterRepository repository;
public GreekString(String str) {
super();
}
.....
@PostConstruct
public void init() {
setTranslit(str);
...
}
}
另请注意:
@EnableJpaRepositoriescom.test.springservice:和xml jpa:存储库具有相同的用途,您可以继续删除xml配置甚至更好,因为他使用Spring Boot,他可以完全删除它,这同样适用于@PropertySource和@ImportRestPositoryRestMVCCConfiguration.class。这只会留下@Configuration、@ComponentScan和@EnableAutoConfiguration,如果使用的版本是Spring Boot 1.2.0,则可以用@SpringBootApplication替换。尚未接受答案,因为即使进行了更改,我在注入的存储库中仍然会得到null。如果您可以在github repo中共享代码,我可以给你发个补丁。很难从代码片段中看出问题的其他方面。您没有在任何地方将GreekString定义为Spring bean,这就是问题所在,Spring只能在已知的情况下自动连接bean,如果您使用新的GreekString绕过Spring,那么Spring根本看不到它,因此无法自动连接。作为一个演示,我在这里制作了GreekString一个springbean,看到@PostConstruct和afterPropertiesSet都被调用-克隆了你的GitHub repo,用maven构建,并用startcontainer.sh启动了容器,但仍然看不到PostConstruct,或者当我点击Strange时调用afterPropertiesSet