簡體   English   中英

Spring無法使用值注釋注入屬性值

[英]Spring can't inject property value using Value annotation

我正在使用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

如果您的注釋行<context:property-placeholder location="classpath:content-stream.properties" />不起作用,則可以通過其他方式進行工作。

使用加載屬性文件

<util:properties id="contentProps" location="content-stream.properties" />

確保文件頂部包含以下內容,以包含“ 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控制器中注入屬性。

檢查此鏈接,了解為什么不能將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配置。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM