I am using Spring Boot MVC and Spring's ServletRegistrationBean
gets called before my Datasource Configuration, which loads in StandardContext
.
WebSecurityConfig
always gets called first and therefore there is never a bean defined for the Autowired prodDatasource
.
I tried everything from @Order
to @ConditionalOnMissingBean
. I haven't been able to find a decent explanation. But I need to get a Bean created for the Datasource before it runs the WebSecurity
@EnableAutoConfiguration
@SpringBootApplication
@EnableCaching
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Bean
public CacheManager cacheManager() {
return new ConcurrentMapCacheManager("appInfo");
}
}
@Configuration
@PropertySource({ "classpath:application.properties" })
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = "App.Models",
entityManagerFactoryRef = "prodEntityManager",
transactionManagerRef = "transactionManager"
)
public class PrimaryDatasourceConfig {
@Autowired
private Environment env;
@Bean
@ConfigurationProperties(prefix = "spring.datasource.prod")
public DataSource prodDataSource() {
EmbeddedDatabaseFactory factory = new EmbeddedDatabaseFactory();
factory.setDatabaseName("spring-social-quickstart");
factory.setDatabaseType(EmbeddedDatabaseType.H2);
return factory.getDatabase();
}
}
@Configuration
@EnableWebSecurity
@Import(PrimaryDatasourceConfig.class)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private DataSource prodDataSource;
SuperiorUserService jdbcDao;
.......
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth
.jdbcAuthentication()
.dataSource(prodDataSource);
auth.authenticationProvider(authProvider());
}
}
Well I figured a solution that seems to help.
Instead of using @Autowire in my Spring Security. I replaced them with @Resource
and it appears to wait for my beans to be created first.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.