2017-06-15 56 views
0

我的src代碼如下所示,我該如何解決這個問題?@Autowired outside @Bean得到了nullpointerexception

@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. 
    } 
} 
+0

可以請你分享你的配置上下文xml文件,否則在配置context.xml文件中使用組件掃描器來掃描你的軟件包,以便自動裝配可以在所有類上完成。 –

+0

問題是你爲什麼要用這種方式?你有TestService,並且你可以在需要ShiroFilterFactoryBean並且只需調用方法getrecords()的時候在任何bean中注入它。或者,您可以將@Bean添加到TestService#getrecords()。我認爲這會更好 –

回答