我正在开发一个相对简单的Spring Boot应用程序,其中使用了几个Java配置类。然而,似乎并没有读取到配置信息。尽管我在各处设置了断点,但没有任何反应。我甚至抛出了一些RuntimeException
来验证我的调试器是否有问题。
在我的主类中,我有标准的Spring Boot主方法:
@ComponentScan
@EnableAutoConfiguration
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
如您所见,我使用了@ComponentScan
和@EnableAutoConfiguration
标记它。应用程序类位于classpath的根目录下。我对@ComponentScan
注解的理解是它将搜索其下方所有的配置类。
在下一层的一个包中,我有所有的配置类:
我的“公共”配置
@Configuration
@EnableJpaRepositories("com.codechimp.XXX.repository")
@EnableTransactionManagement
public class AppCommonConfig {
@Inject
private Environment environment;
/* Define common beans here like datasource and such */
}
我的Spring Security配置
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Inject
private LocalXXXUserDetailsService localXXXUserDetailsService;
/**
* @see org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter#configure(HttpSecurity)
*/
@Autowired
protected void configure(HttpSecurity http) throws Exception {
// Configure http
}
/**
* @see org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter#configureGlobal(AuthenticationManagerBuilder)
*/
@Autowired
protected void configureGlobal(AuthenticationManagerBuilder auth)
throws Exception {
// Configure auth
}
}
然而,当我运行应用程序时,似乎没有调用这些配置类中的任何方法。就像它们完全被忽略了一样。正如我所说,我已经尝试设置断点,甚至在所有方法的开头抛出RuntimeException:
if (true)
throw new RuntimeException("Break!");
诚然,我在使用Java配置方面没有太多经验,但我一次又一次地查看了文档,却没有发现缺失的部分。