【发布时间】:2015-08-10 20:12:52
【问题描述】:
我需要在我的 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>
【问题讨论】:
-
确保您没有自己创建新实例,并且您在
@Repository所在的上下文中也有一个<context:property-placeholder location="classpath:config.properties"/>。如果不是,则不会替换任何内容。 -
@M.Deinum 你能给我一个例子吗?我不明白。谢谢
-
你有什么不明白的?检查您使用此控制器的位置,您没有自己创建新实例,并确保您在定义/加载存储库的应用程序上下文中配置了占位符。
-
我已经用我的称呼更新了我的问题。我还添加了一个自动装配并检查了占位符,但仍然相同
-
那么您的配置文件中没有
<context:property-placeholder location="classpath:config.properties"/>,因此您的@Value基本上没用。添加它。
标签: java spring spring-mvc properties spring-security