Spring @@Bean外部自动连线获得nullpointerexception
我的src代码如下,如何修复Spring @@Bean外部自动连线获得nullpointerexception,spring,Spring,我的src代码如下,如何修复 @Component("shiroSecurityConfig") public class ShiroSecurityConfig { @Autowired (required = true) private TestService testService; @Bean(name = "shiroFilter") public ShiroFilterFactoryBean shiroFilter() { testS
@Component("shiroSecurityConfig")
public class ShiroSecurityConfig {
@Autowired (required = true)
private TestService testService;
@Bean(name = "shiroFilter")
public ShiroFilterFactoryBean shiroFilter() {
testService.getrecords(); //this line got null pointer exception.
}
}
自动连接总是在构建bean对象之后发生,这就是为什么您将testService设置为null,因为它还没有自动连接,有关bean生命周期的更多信息,请参阅
自动连接总是在构建bean对象之后发生,这就是为什么您将testService设置为null,因为它尚未自动连接,有关bean生命周期的更多信息,请参阅
请在configuration context.xml文件中共享您的配置上下文xml文件,否则请使用组件扫描程序扫描您的包,以便可以在所有类上进行自动连接。问题是,为什么要使用这种方式?您拥有TestService,可以在需要ShiroFilterFactoryBean时将其注入任何bean中,只需调用方法getrecords()。或者您可以将@Bean添加到TestService#getrecords()。我认为最好您能共享您的配置上下文xml文件,否则在configuration context.xml文件中,使用组件扫描程序扫描您的包,以便可以在所有类上进行自动连接。问题是您为什么使用这种方式?您拥有TestService,可以在需要ShiroFilterFactoryBean时将其注入任何bean中,只需调用方法getrecords()。或者您可以将@Bean添加到TestService#getrecords()。我想这样会更好