2

I am using spring 4.0.3.RELEASE

Here is my applicationContext.xml where i am configuring PropertyPlaceHolder

<?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:p="http://www.springframework.org/schema/p"
       xmlns:util="http://www.springframework.org/schema/util"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
          http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd
          http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
          http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd

       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
    <mvc:annotation-driven/>
    <context:component-scan base-package="org.graphsearch.tutor.service.impls, org.graphsearch.tutor.dao.impls, org.graphsearch.tutor.configs, org.graphsearch.tutor.utils"/>
    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>/WEB-INF/properties/database/jdbc.properties</value>
                <value>/WEB-INF/properties/alert/message.properties</value>
                <value>/WEB-INF/properties/email/mail.properties</value>
                <value>/WEB-INF/properties/logger/logging.properties</value>
            </list>
        </property>
    </bean>
</beans>

In Email client class i am injecting property values like this

@Component
public class EmailClient {

    @Value("${tutor.mail.common.note}")
    private static String NOTE;

    @Value("${tutor.mail.common.regards}")
    private static String REGARDS;

    @Value("${tutor.mail.from}")
    private static String FROM;

    @Autowired
    private MailSender mailSender;

    @Autowired
    private Environment env;

    public void sendRegisterMail(User user){
        SimpleMailMessage message = new SimpleMailMessage();
        message.setTo(user.getEmailID());
        String subject = env.getProperty("tutor.register.success.mail.subject"); //retuns null
        String contentTemplate = env.getProperty("tutor.register.success.mail.content"); //returns null
        MessageFormat format = new MessageFormat(contentTemplate);
        Object[] args = {user.getFullName()};
        StringBuffer content = new StringBuffer();
        content.append(format.format(args));
        content.append(NOTE);
        content.append(REGARDS);
        message.setSubject(subject);
        message.setText(content.toString());
        mailSender.send(message);
    }
}

Now problem is @Value("${property.key}") does work like charm when i inject a private field of the class like NOTE, REGARDS, FROM.

But if i need this value inside the method sendRegisterMail() @Value("$key") is giving compiler error. I searched on web few examples are there getting properties value through environment so i used like i have done in EmailClient class but it always give me null. I checked the log it says it can not find the property key.

Can some body give me a clue how to inject properties value inside a method. Thanks in advance

Abhishek Nayak
  • 3,732
  • 3
  • 33
  • 64
Ravi Kumar
  • 993
  • 1
  • 12
  • 37
  • Why don't you just inject the variable in a `String` field as you're currently doing and then use these fields in your methods? What's the problem with this approach? – Luiggi Mendoza May 04 '14 at 05:55
  • possible duplicate of [How to programmatically resolve property placeholder in Spring](http://stackoverflow.com/questions/5172392/how-to-programmatically-resolve-property-placeholder-in-spring) – Luiggi Mendoza May 04 '14 at 05:56
  • @LuiggiMendoza This class is going to have some more than 100 properties value and most of them are going to be used only in one method. so it's not a good idea to make all properties as private field as it will take more memory. This is my understanding do correct me if i am wrong – Ravi Kumar May 04 '14 at 05:58
  • I don't see any memory problems there, and you should not either until proven by usage of a profiler or another third agent. Looks like a premature optimization to me, and remember that *premature optimization is the root of all evil*. Anyway, there's a possible dup Q/A where it states how you may do this programatically. – Luiggi Mendoza May 04 '14 at 06:00
  • 1
    @RaviKumar What you are trying to do is not a good idea. If there are going to be too many properties in the class, what you should be thinking is breaking up the class into smaller ones – geoand May 04 '14 at 07:42

1 Answers1

1

Try

@Value("#{propertyConfigurer['tutor.mail.common.note']}")
private static String NOTE;

For more info have a look at How can I inject a property value into a Spring Bean which was configured using annotations?

Community
  • 1
  • 1
Braj
  • 46,415
  • 5
  • 60
  • 76