2017-02-17 64 views
0

我配置Spring安全注册豆,下面是我的代码 -春:通过私有方法

@Override 
protected void configure(AuthenticationManagerBuilder auth) throws Exception { 
    auth.userDetailsService(customUserDetailsService).passwordEncoder(encoder); 
} 

@Bean(name="encoder") 
public BCryptPasswordEncoder getPasswordEncoder(){ 
    return new BCryptPasswordEncoder(); 
} 

@Override 
protected void configure(HttpSecurity http) throws Exception { 
... 
} 

只要我申请@Autowire导通

@Override 
@Autowire 
    protected void configure(HttpSecurity http) throws Exception { 
    ... 
    } 

这引发异常无豆在容器中键入'HttpSecurity',这是预期的。

但是当我申请@Autowire导通

@Override 
@Autowire 
    protected void configure(AuthenticationManagerBuilder auth) throws Exception { 
     auth.userDetailsService(customUserDetailsService).passwordEncoder(encoder); 
    } 

有没有例外? 这个AuthenticationManagerBuilder这个bean是在bean factory吗?

当我改变了我的豆登记私有方法像这个 -

@Override 
protected void configure(AuthenticationManagerBuilder auth) throws Exception { 
    auth.userDetailsService(customUserDetailsService).passwordEncoder(getPasswordEncoder()); 
} 

@Bean(name="encoder") 
private BCryptPasswordEncoder getPasswordEncoder(){ 
    return new BCryptPasswordEncoder(); 
} 

这是抛出异常,方法不能是private.Why这样呢?

+0

标记为最终或私人你有两个问题。后者应该是不言自明的。首先,你使用Boot吗? – chrylis

+0

@chrylis这是spring mvc项目,Iam不在这里使用spring boot。 – TheCurious

回答

0

从Spring文档

通常,@Bean方法@Configuration类中声明。在这种情况下,bean方法可以通过直接调用它们来引用同一类中的其他@Bean方法。这确保了bean之间的引用是强类型和可导航的。这种所谓的“bean间引用”保证像getBean()查找那样尊重范围检查和AOP语义。这些是最初的'Spring JavaConfig'项目中已知的语义,它们需要在运行时对每个这样的配置类进行CGLIB子类化。因此,@Configuration类和它们的工厂方法不能在此模式下

Ref link