gpt4 book ai didi

java - 如何在spring类的方法中访问属性文件值

转载 作者:行者123 更新时间:2023-11-30 11:19:02 25 4
gpt4 key购买 nike

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

在电子邮件客户端类中,我正在注入(inject)这样的属性值

@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}") 在我注入(inject)类的私有(private)字段(如 NOTE、REGARDS、FROM)时确实像魅力一样工作。

但是如果我在方法 sendRegisterMail() 中需要这个值@Value("$key") 给出了编译器错误。我在 web 上搜索了几个示例,这些示例是通过环境获取属性值的,所以我像在 EmailClient 类中所做的那样使用,但它总是给我 null。我检查了日志,它说找不到属性 key 。

有人能告诉我如何在方法中注入(inject)属性值吗?提前致谢

最佳答案

尝试

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

有关更多信息,请查看 How can I inject a property value into a Spring Bean which was configured using annotations?

关于java - 如何在spring类的方法中访问属性文件值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23453095/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com