【发布时间】:2014-06-20 14:39:52
【问题描述】:
我正在使用 spring 4.0.3.RELEASE
这是我的applicationContext.xml,我在其中配置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>
在电子邮件客户端类中,我正在注入这样的属性值
@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);
}
}
现在的问题是@Value("${property.key}") 在我注入类的私有字段(如 NOTE、REGARDS、FROM)时确实像魅力一样工作。
但是如果我在方法sendRegisterMail() 中需要这个值
@Value("$key") 给出编译器错误。我在网上搜索了几个例子,有没有通过环境获取属性值,所以我像在EmailClient 类中所做的那样使用,但它总是给我空值。我检查了它说找不到属性键的日志。
有人能告诉我如何在方法中注入属性值吗? 提前致谢
【问题讨论】:
-
为什么不直接将变量注入到
String字段中,就像您目前所做的那样,然后在您的方法中使用这些字段?这种方法有什么问题? -
@LuiggiMendoza 此类将有超过 100 个属性值,其中大多数将仅用于一种方法。所以将所有属性都设为私有字段并不是一个好主意,因为它会占用更多内存。这是我的理解,如果我错了,请纠正我
-
我没有看到任何内存问题,在使用分析器或其他第三方代理证明之前,您不应该这样做。对我来说看起来像是过早的优化,请记住过早的优化是万恶之源。无论如何,有一个可能的 dup Q/A 说明您可以如何以编程方式执行此操作。
-
@RaviKumar 你试图做的不是一个好主意。如果类中的属性太多,您应该考虑将类分解为更小的类