我正在尝试使用Spring Boot 2 + Spring Data + Custom Auto Configuration Classes
,但由于某种原因无法在第三堂课中注入DataSource
(由HikariCP提供)。
@Configuration
@AutoConfigureOrder(Ordered.LOWEST_PRECEDENCE)
public class InitialAutoConfiguration {
//Beans to load in theory first.
}
@Configuration
@AutoConfigureOrder(Ordered.LOWEST_PRECEDENCE)
@AutoConfigureAfter(InitialAutoConfiguration.class)
@EntityScan(basePackageClasses = Asset.class)
@EnableJpaRepositories(basePackageClasses = AssetRepository.class,
repositoryBaseClass = BaseRepositoryImpl.class,
repositoryFactoryBeanClass = ExtendedJpaRepositoryFactoryBean.class)
public class JpaAutoConfiguration { //Load Jpa Classes
}
@Configuration
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE)
@AutoConfigureAfter(JpaAutoConfiguration.class)
@ComponentScan(basePackages = {"com.xxx"})
public class ServiceConfiguration {
@Inject
private DataSource dataSource; //Datasource is null
@Bean
public DbPropertySourcesPlaceholderConfigurer dbPropertySourcesPlaceholderConfigurer() {
DbPropertySourcesPlaceholderConfigurer placeholderConfigurer = new DbPropertySourcesPlaceholderConfigurer(dataSource);
placeholderConfigurer.setPlaceholderPrefix("%{");
placeholderConfigurer.setPlaceholderSuffix("}");
return placeholderConfigurer;
}
}
META-INF/spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.xetec.autoconfigure.InitialAutoConfiguration,\
com.xetec.autoconfigure.JpaAutoConfiguration,\
com.xetec.autoconfigure.ServiceConfiguration
好像我的类在Spring数据类(DataSourceAutoConfiguration.Hikari
)之前加载。
有没有一种方法可以先加载Spring Boot Starters类,然后再加载我的自定义类?
谢谢
答案 0 :(得分:0)
我不确定以下更改是否可以解决您的问题,但我遇到类似的问题
在您的方法中添加静态
@Bean
public static DbPropertySourcesPlaceholderConfigurer dbPropertySourcesPlaceholderConfigurer() {
....
}
答案 1 :(得分:0)
您使用最高和最低优先级是错误的方法。您的ServiceConfiguration
的优先级最高,这意味着它将首先被评估。
我会使用@AutoConfigureAfter(DataSourceAutoConfiguration.class)