【问题标题】:Spring can't inject property value using Value annotationSpring 无法使用 Value 注释注入属性值
【发布时间】:2015-05-25 09:22:08
【问题描述】:

我正在使用 Spring MVC,但无法在控制器类中使用 @Value 注释注入属性值。这是我的控制器:

 @Controller
    public class MailController {
        @Value("${mail.server.username}")
        private String destinationEmail;
        ...
        }

这是我的应用程序上下文 xml 文件:

    <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:util="http://www.springframework.org/schema/util"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

    <context:component-scan base-package="org.content.stream">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

    <!--<context:property-placeholder location="classpath:content-stream.properties" />-->

    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
          p:location="classpath:content-stream.properties" name="propertyPlaceholderConfigurer"/>

    <import resource="security.xml" />

    <bean id="messageSource"
          class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basenames">
            <list>
                <value>mymessages</value>
            </list>
        </property>
    </bean>

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${dataSource.driverClassName}" />
        <property name="url" value="${dataSource.url}" />
        <property name="username" value="${dataSource.username}" />
        <property name="password" value="${dataSource.password}" />
    </bean>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="packagesToScan" value="org.content.stream.entities"/>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                <prop key="show_sql">${hibernate.show_sql}</prop>
                <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
            </props>
        </property>
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager"/>

    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="dataSource" ref="dataSource" />
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="50000000"/>
    </bean>

    <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
        <property name="host" value="${mail.server.host}" />
        <property name="port" value="${mail.server.port}" />
        <property name="username" value="${mail.server.username}" />
        <property name="password" value="${mail.server.password}" />
        <property name="javaMailProperties">
            <util:properties location="classpath:javamail.properties" />
        </property>
    </bean>

    <bean id="destinationEmail" class="java.lang.String">
        <constructor-arg value="${mail.server.username}"/>
    </bean>

    <bean id="templateMailMessage"
          class="org.springframework.mail.SimpleMailMessage">
        <property name="text">
            <value>
                <![CDATA[
            Уважаемый, %s!
            Содержымое : %s
            С уважением %s !
            ]]>
            </value>
        </property>
    </bean>

</beans>

我在堆栈上阅读了很多答案,但在我的情况下没有任何工作。我的属性文件的内容:

...
#Mail properties
mail.server.host=smtp.gmail.com
mail.server.port=587
mail.server.username=myemail@gmail.com
mail.server.password=password

【问题讨论】:

  • 分享content-stream.properties文件中的内容。
  • @DilipKumar 我分享了
  • 您的评论行&lt;context:property-placeholder location="classpath:content-stream.properties" /&gt; 应该有效吗?使用这条线时遇到什么异常?
  • @DilipKumar 我没有遇到异常,只是我的destinationEmail 字段值为${mail.server.username}

标签: java spring spring-mvc


【解决方案1】:

如果您的评论行&lt;context:property-placeholder location="classpath:content-stream.properties" /&gt; 不起作用,那么您可以通过其他方式获得工作。

使用加载属性文件

&lt;util:properties id="contentProps" location="content-stream.properties" /&gt;

确保文件顶部包含以下内容以包含“util”命名空间:

xmlns:util="http://www.springframework.org/schema/util" 
xsi:schemaLocation= "... http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"

接下来,您可以访问控制器中的属性,如

@Value("#{contentProps['mail.server.username']}")
private String destinationEmail;

这将帮助您在不同的文件中组织多个属性。

更新:

据我所知,我们不能直接在 spring 控制器中注入属性。

查看link,了解为什么不能将 Value 与 Controller 一起使用。

但是你可以通过两种方式实现:

  1. 上述方法,使用 util 属性。

  2. 使用一个专用的配置类为控制器提供属性。 如下所示

    @Configuration
    @PropertySource("classpath:content-stream.properties")
    public class AppConfig {
        @Value("${mail.server.username}")
        private String destinationEmail;
    
        // getters
    }
    

在您的控制器中自动装配AppConfig,使用getter 访问值。

在第二个选项中,您不需要任何 xml 配置。

【讨论】:

  • 非常感谢它的工作,但也许,你会解释为什么 ${mail.server.username} 不工作
  • 今天仍然有用且相关。配置类是最重要的部分。您可以使用 @Autowired 访问配置类
猜你喜欢
  • 1970-01-01
  • 2019-09-01
  • 2022-06-22
  • 1970-01-01
  • 2011-09-19
  • 2016-06-05
  • 1970-01-01
  • 1970-01-01
  • 2015-07-03
相关资源
最近更新 更多