【问题标题】:Spring MVC read property file null in DAOImplSpring MVC 在 DAOImpl 中读取属性文件 null
【发布时间】: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 所在的上下文中也有一个 &lt;context:property-placeholder location="classpath:config.properties"/&gt;。如果不是,则不会替换任何内容。
  • @M.Deinum 你能给我一个例子吗?我不明白。谢谢
  • 你有什么不明白的?检查您使用此控制器的位置,您没有自己创建新实例,并确保您在定义/加载存储库的应用程序上下文中配置了占位符。
  • 我已经用我的称呼更新了我的问题。我还添加了一个自动装配并检查了占位符,但仍然相同
  • 那么您的配置文件中没有&lt;context:property-placeholder location="classpath:config.properties"/&gt;,因此您的@Value 基本上没用。添加它。

标签: java spring spring-mvc properties spring-security


【解决方案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

【讨论】:

  • 不,您不需要在正确的上下文中指定&lt;context:property-placeholder location="classpath:config.properties"/&gt;。它只会在相同的上下文中对 bean 进行操作,在您的情况下,只有 DispatcherServlet 加载的 bean,也将这一行添加到 ContextLoaderListener
猜你喜欢
  • 2018-10-16
  • 2014-01-19
  • 1970-01-01
  • 2013-05-26
  • 1970-01-01
  • 2016-04-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多