Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring @@Bean外部自动连线获得nullpointerexception_Spring - Fatal编程技术网

Spring @@Bean外部自动连线获得nullpointerexception

Spring @@Bean外部自动连线获得nullpointerexception,spring,Spring,我的src代码如下,如何修复 @Component("shiroSecurityConfig") public class ShiroSecurityConfig { @Autowired (required = true) private TestService testService; @Bean(name = "shiroFilter") public ShiroFilterFactoryBean shiroFilter() { testS

我的src代码如下,如何修复

@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()。我想这样会更好