我正在使用Spring Boot MVC和Spring ServletRegistrationBean
在我的数据源配置之前调用,该数据源配置加载到StandardContext
。
WebSecurityConfig
,因此永远不会为自动装配的prodDatasource
定义bean。
我尝试了从@Order
到@ConditionalOnMissingBean
的所有内容。我还没有找到合适的解释。但我需要在运行WebSecurity之前为Datasource创建一个Bean
@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());
}
}
答案 0 :(得分:0)
我认为这个解决方案似乎有所帮助。
而不是在Spring Security中使用@Autowire。我用@Resource
替换它们,它似乎等待我的bean首先被创建。