Spring MVC 读取 属性 DAOImpl 中的空文件
Spring MVC read property file null in DAOImpl
我需要在我的 UserDetailsDaoImpl 中读取一个 属性 值。我正在使用 Spring 安全性。
它在 @Controller
中成功读取,但在 class 中却没有,可能是因为它是 @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>
我的问题是,由于我调用了一个方法,它不会加载@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;
}
这个答案对我帮助很大:
我需要在我的 UserDetailsDaoImpl 中读取一个 属性 值。我正在使用 Spring 安全性。
它在 @Controller
中成功读取,但在 class 中却没有,可能是因为它是 @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>
我的问题是,由于我调用了一个方法,它不会加载@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;
}
这个答案对我帮助很大: