0

I'm using Spring MVC and i can't inject property value using @Value annotation in my controller class. Here is my controller:

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

And here is my application context xml file:

    <?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>

I read many answers on stack but in my case nothing to work. Content of my propery file:

...
#Mail properties
mail.server.host=smtp.gmail.com
mail.server.port=587
mail.server.username=myemail@gmail.com
mail.server.password=password
fidel150992
  • 303
  • 5
  • 17

1 Answers1

2

If your commented line <context:property-placeholder location="classpath:content-stream.properties" /> not working, then you can get work in other way.

load property file using

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

Make sure you have the following at the top of your file to include the "util" namespace:

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"

Next, you can access properties in controller like

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

This will help you to organize multiple properties in different files.

UPDATE:

As far i know, we can't directly inject properties in spring controller.

Check this link for why you can't use Value with Controller.

But you can achieve this by two ways:

  1. Above approach, using util properties.

  2. Using one dedicated config class to serve properties to controller. like as below

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

Autowire AppConfig in your controller, access value using getter.

In Second option you don't need any xml configuration.

Community
  • 1
  • 1
Dilip Kumar
  • 2,504
  • 2
  • 17
  • 33
  • thanks a lot it's working but, maybe, you will explain why `${mail.server.username}` not working – fidel150992 May 25 '15 at 10:19
  • Still helpful and relevant today. The config class was the most important part. You can access the config class using @Autowired – Zee Apr 19 '18 at 17:25