如何将java.util.Properties对象赋予spring,以从Properties对象解析spring中的属性值,如${mail.port}
您好,我正在创建一个spring应用程序的独立jar,它将是一个实用程序服务,因此我将获得java.util.Properties的对象,我需要在我的spring应用程序中使用这个属性 所以我需要做的是创建一个类,该类将Properties对象作为其构造中的参数 我所做的是如何将java.util.Properties对象赋予spring,以从Properties对象解析spring中的属性值,如${mail.port},java,spring,Java,Spring,您好,我正在创建一个spring应用程序的独立jar,它将是一个实用程序服务,因此我将获得java.util.Properties的对象,我需要在我的spring应用程序中使用这个属性 所以我需要做的是创建一个类,该类将Properties对象作为其构造中的参数 我所做的是 public class MailService { public MailService(Properties properties) { PropertySourcesPlaceholderConfigu
public class MailService {
public MailService(Properties properties) {
PropertySourcesPlaceholderConfigurer pops = new PropertySourcesPlaceholderConfigurer();
pops.setProperties(properties);
ClassPathXmlApplicationContext ctx = null;
try {
ctx = new ClassPathXmlApplicationContext("/resources/xml/spring/mailservice_context.xml");
} catch (Exception e) {
e.printStackTrace();
} finally {
if (ctx != null) {
ctx.close();
ctx = null;
}
}
}
}
我想从我在MailService构造函数中传递的属性对象中获得“mail.host”、“mail.port”
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="${mail.host}" />
<property name="port" value="${mail.port}" />
<property name="username" value="${mail.username}" />
<property name="password" value="${mail.password}" />
<property name="javaMailProperties">
<props>
<prop key="mail.transport.protocol">${mail.transport.protocol}</prop>
<prop key="mail.smtps.auth">${mail.smtps.auth}</prop>
<prop key="mail.smtps.starttls.enable">${mail.smtps.starttls.enable}</prop>
<prop key="mail.debug">${mail.debug}</prop>
</props>
</property>
</bean>
${mail.transport.protocol}
${mail.smtps.auth}
${mail.smtps.starttls.enable}
${mail.debug}
如何从java的properties对象中获取此值,我正在使用spring 4.1这是我在应用程序启动时加载数据库配置文件的方法:
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="${mail.host}" />
<property name="port" value="${mail.port}" />
<property name="username" value="${mail.username}" />
<property name="password" value="${mail.password}" />
<property name="javaMailProperties">
<props>
<prop key="mail.transport.protocol">${mail.transport.protocol}</prop>
<prop key="mail.smtps.auth">${mail.smtps.auth}</prop>
<prop key="mail.smtps.starttls.enable">${mail.smtps.starttls.enable}</prop>
<prop key="mail.debug">${mail.debug}</prop>
</props>
</property>
</bean>
System.setProperty("db.config", options.getDbConfigFilePath());
其中options.getDbConfigFilePath()
是一个带有文件路径的字符串(从apache cli传递)
在context.xml中
<context:property-placeholder location="file:${db.config}"/>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="password" value="${jdbc.password}"/>
<property name="username" value="${jdbc.username}"/>
</bean>
jdbc
参数来自db.config文件添加以下两行:
ctx.addBeanFactoryPostProcessor(pops);
ctx.refresh();
之后:
ctx = new
ClassPathXmlApplicationContext("/resources/xml/spring/mailservice_context.xml");
这将在
ClassPathXmlApplicationContext
中设置属性,.refresh()
将加载它们。我建议使用Spring Boot和@ConfigurationProperties
。感谢您的建议,但我必须从java.util.properties对象传递此属性。我没有任何文件