Java @Configuration内的springboot自动连线@Repository不工作
我有一个简单的spring boot应用程序,其中有一个jpa存储库对象,我想在Java @Configuration内的springboot自动连线@Repository不工作,java,spring,spring-boot,spring-data-jpa,Java,Spring,Spring Boot,Spring Data Jpa,我有一个简单的spring boot应用程序,其中有一个jpa存储库对象,我想在@Configuration类中自动连接,如下所示 @Configuration public class Appconfig { @Bean @Autowired public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer(OctopusPropertiesRepository repo
@Configuration
类中自动连接,如下所示
@Configuration
public class Appconfig {
@Bean
@Autowired
public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer(OctopusPropertiesRepository repo) {
PropertySourcesPlaceholderConfigurer property = new PropertySourcesPlaceholderConfigurer();
Map<String,Object> props = new ConcurrentHashMap<>();
List<OctopusProperties> loadedSettings = repo.findAll();
loadedSettings.forEach(entry -> props.put(entry.getKey(), entry.getValue()));
MutablePropertySources mutablePropertySources = new MutablePropertySources();
mutablePropertySources.addFirst(new MapPropertySource("custom", props));
property.setPropertySources(mutablePropertySources);
return property;
}
}
我得到以下例外
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.example.app.OctopusPropertiesRepository]
如果我没有成功地进行配置并启动应用程序,我会从执行器中看到bean是可用的。这里有什么问题?为什么我不能在@Configuration
内部连接@Repository
p.S.这两个java文件位于同一文件夹下:com.example.app
p.p.S.我的项目的Eclipse视图:
类似的问题也在讨论中得到了解决 允许像这样更改
AppConfig
类吗
@Configuration
public class Appconfig {
@Autowired
private OctopusPropertiesRepository repo;
@Bean
public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
PropertySourcesPlaceholderConfigurer property = new PropertySourcesPlaceholderConfigurer();
Map<String,Object> props = new ConcurrentHashMap<>();
List<OctopusProperties> loadedSettings = repo.findAll();
loadedSettings.forEach(entry -> props.put(entry.getKey(), entry.getValue()));
MutablePropertySources mutablePropertySources = new MutablePropertySources();
mutablePropertySources.addFirst(new MapPropertySource("custom", props));
property.setPropertySources(mutablePropertySources);
return property;
}
}
@配置
公共类Appconfig{
@自动连线
私人八达通财产储蓄回购;
@豆子
公共属性资源占位符配置器属性资源占位符配置器(){
PropertySourcesPlaceholderConfigurer属性=新的PropertySourcesPlaceholderConfigurer();
Map props=新的ConcurrentHashMap();
List loadedSettings=repo.findAll();
loadedSettings.forEach(entry->props.put(entry.getKey(),entry.getValue());
MutablePropertySources MutablePropertySources=新的MutablePropertySources();
addFirst(新的MapPropertySource(“custom”,props));
属性。setPropertySources(可变属性资源);
归还财产;
}
}
如果此解决方案有效或无效,请进行还原。根据@Bean javadoc:
BeanFactoryPostProcessor-returning @Bean methods
Special consideration must be taken for @Bean methods that return Spring
BeanFactoryPostProcessor (BFPP) types. Because BFPP objects must be
instantiated very early in the container lifecycle, they can interfere with
processing of annotations such as @Autowired, @Value, and @PostConstruct
within @Configuration classes. To avoid these lifecycle issues, mark BFPP
returning @Bean methods as static. For example:
@Bean
public static PropertyPlaceholderConfigurer ppc() {
// instantiate, configure and return ppc ...
}
By marking this method as static, it can be invoked without causing
instantiation of its declaring @Configuration class, thus avoiding the above-
mentioned lifecycle conflicts. Note however that static @Bean methods will not
be enhanced for scoping and AOP semantics as mentioned above. This works out
in BFPP cases, as they are not typically referenced by other @Bean methods. As
a reminder, a WARN-level log message will be issued for any non-static @Bean
methods having a return type assignable to BeanFactoryPostProcessor.
因此,根据这一点,您的属性资源占位符配置器()
应该是静态的
编辑:
很抱歉编辑晚了。
我不认为您可以在这里使用Hibernate存储库,因为BeanFactoryPostProcessor
在Hibernate和jpa需要任何依赖项之前被实例化。
也许您可以在这里使用JdbcTemplate,至少对我来说是这样:
@Configuration
public class Appconfig {
@Bean
public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
PropertySourcesPlaceholderConfigurer property = new PropertySourcesPlaceholderConfigurer();
Map<String, Object> props = new ConcurrentHashMap<>();
List<OctopusProperties> loadedSettings = getAll();
loadedSettings.forEach(entry -> props.put(entry.getKey(), entry.getValue()));
MutablePropertySources mutablePropertySources = new MutablePropertySources();
mutablePropertySources.addFirst(new MapPropertySource("custom", props));
property.setPropertySources(mutablePropertySources);
return property;
}
@Bean
public static DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/test");
dataSource.setUsername("root");
dataSource.setPassword("root");
return dataSource;
}
private List<OctopusProperties> getAll() {
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource());
List<OctopusProperties> all = jdbcTemplate.query("select id,key_column,value_column from octopus_properties",
new RowMapper<OctopusProperties>() {
@Override
public OctopusProperties mapRow(final ResultSet rs, final int rowNum) throws SQLException {
OctopusProperties op = new OctopusProperties();
op.setId(rs.getLong("id"));
op.setKey(rs.getString("key_column"));
op.setValue(rs.getString("value_column"));
return op;
}
});
return all;
}
}
@配置
公共类Appconfig{
@豆子
公共属性资源占位符配置器属性资源占位符配置器(){
PropertySourcesPlaceholderConfigurer属性=新的PropertySourcesPlaceholderConfigurer();
Map props=新的ConcurrentHashMap();
List loadedSettings=getAll();
loadedSettings.forEach(entry->props.put(entry.getKey(),entry.getValue());
MutablePropertySources MutablePropertySources=新的MutablePropertySources();
addFirst(新的MapPropertySource(“custom”,props));
属性。setPropertySources(可变属性资源);
归还财产;
}
@豆子
公共静态数据源DataSource(){
DriverManager数据源dataSource=新的DriverManager数据源();
setDriverClassName(“com.mysql.jdbc.Driver”);
setUrl(“jdbc:mysql://localhost:3306/test");
dataSource.setUsername(“根”);
dataSource.setPassword(“根”);
返回数据源;
}
私有列表getAll(){
JdbcTemplate JdbcTemplate=新的JdbcTemplate(dataSource());
List all=jdbcTemplate.query(“从八达通属性中选择id、键列、值列”,
新的行映射器(){
@凌驾
公共OctopusProperties映射行(最终结果集rs,最终int rowNum)抛出SQLException{
OctopusProperties op=新的OctopusProperties();
op.setId(rs.getLong(“id”);
op.setKey(rs.getString(“key_列”);
op.setValue(rs.getString(“value_列”);
返回op;
}
});
全部归还;
}
}
请注意,我在这里使用了MySQL,因此相应地配置了数据源。使用@PostConstruct
@Autowired PropertySourcesPlaceholderConfigurer property;
@Autowired OctopusPropertiesRepository repo;
@PostConstruct
public void onInit() {
Map<String,Object> props = new ConcurrentHashMap<>();
List<OctopusProperties> loadedSettings = repo.findAll();
loadedSettings.forEach(entry -> props.put(entry.getKey(), entry.getValue()));
MutablePropertySources mutablePropertySources = new MutablePropertySources();
mutablePropertySources.addFirst(new MapPropertySource("custom", props));
property.setPropertySources(mutablePropertySources);
}
@autowiredpropertysourcesplaceconfigurer属性;
@自动连线章鱼财产储蓄回购;
@施工后
公共无效onInit(){
Map props=新的ConcurrentHashMap();
List loadedSettings=repo.findAll();
loadedSettings.forEach(entry->props.put(entry.getKey(),entry.getValue());
MutablePropertySources MutablePropertySources=新的MutablePropertySources();
addFirst(新的MapPropertySource(“custom”,props));
属性。setPropertySources(可变属性资源);
}
包含正在扫描组件的类。还请在列表中包含软件包。可能是主类?主类在根包中,所有其他类在同一个包中。我认为这与时间有关,因为如果我不使用这个配置,spring会生成这个bean。如果你在配置类中添加一个简单的成员,比如下面,spring会找到它吗@自动连线OctopusProperties存储OctopusProperties存储no,我尝试过,但没有改变:(@luboskrnac我已经添加了屏幕快照good points,我不知道提到的部分,并满怀希望地尝试过,但仍然是相同的错误:(是的,我也试过了,同样的错误,让我再研究一下,我会再联系你的,你是不是说@PostConstruct?我不知道你是需要手动创建PropertySourcesPlaceholderConfigurer Bean实例,还是Spring boot自动创建它。
@Autowired PropertySourcesPlaceholderConfigurer property;
@Autowired OctopusPropertiesRepository repo;
@PostConstruct
public void onInit() {
Map<String,Object> props = new ConcurrentHashMap<>();
List<OctopusProperties> loadedSettings = repo.findAll();
loadedSettings.forEach(entry -> props.put(entry.getKey(), entry.getValue()));
MutablePropertySources mutablePropertySources = new MutablePropertySources();
mutablePropertySources.addFirst(new MapPropertySource("custom", props));
property.setPropertySources(mutablePropertySources);
}