我需要在UserDetailsDaoImpl中读取属性值。我正在使用Spring Security。
它成功地读取@Controller
内部,但不在此类中,因为它是@Repository
。
我该怎么做才能读取属性值?
UserDetailsDaoImpl:
@Repository
public class UserDetailsDaoImpl extends JdbcDaoSupport implements UserDetailsDao {
@Value("${emails_blocked}")
private String emails_blocked;
豆类:
<context:property-placeholder location="classpath:config.properties"/>
编辑:
这就是我如何调用UserDetailsDaoImpl:
@Autowired
UserDetailsDao userDetailsDao;
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
try {
Authentication auth = super.authenticate(authentication);
// if reach here, means login success, else exception will be thrown
// reset the user_attempts
userDetailsDao.resetFailAttempts(authentication.getName());
return auth;
} catch (BadCredentialsException e) {
userDetailsDao.updateFailAttempts(authentication.getName());
throw e;
}
我的豆子更新了:
<beans:bean id="userDetailsDao" class="com.setelog.spring.dao.UserDetailsDaoImpl" >
<beans:property name="dataSource" ref="dataSource" />
</beans:bean>
<beans:bean id="encoder" class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder"/>
<beans:bean id="authenticationProvider"
class="com.setelog.spring.handler.LimitLoginAuthenticationProvider">
<beans:property name="userDetailsService" ref="customUserDetailsService" />
<beans:property name="userDetailsDao" ref="userDetailsDao" />
<beans:property name="passwordEncoder" ref="encoder" />
</beans:bean>
答案 0 :(得分:-1)
我的问题是,因为我调用了一个方法,它不会加载@Value所以我不得不注入一些bean。 像这样:
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:config.properties</value>
</list>
</property>
</bean>
<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="staticMethod" value="com.setelog.spring.dao.UserDetailsDaoImpl.setEmails_Blocked"/>
<property name="arguments">
<list>
<value>${emails_blocked}</value>
</list>
</property>
</bean>
我的UserDetailsDaoImpl:
static String emails_blocked;
public static void setEmails_Blocked(String emails_blocked){
UserDetailsDaoImpl.emails_blocked= emails_blocked;
}
这个答案给了我很多帮助: https://stackoverflow.com/a/24649692/4790786