在 XML、Spring 中传递属性文件
Passing a properties file in XML,Spring
我想使用 XMl 在 Spring 中自动装配我的服务。
我想要自动装配的 class 看起来像这样:
public class Service{
public Service(String type,Properties property){....}
....
}
我的控制器中有这个对象的一个实例。
@Autowired
protected Service service;
所以现在我不想在我的 xml 文件中实例化这个对象。这在我开始使用我的构造函数中的属性之前就起作用了,所以一切都以正确的方式配置。但是现在我已经添加了属性 Obect,我不知道如何将它们传递给构造函数。这是现在的样子:
<bean id="service" class="service.service">
<constructor-arg >
<value>MYSQL</value>
</constructor-arg>
<constructor-arg >
<property name="url">jdbc:....</property>
<property name="user">myUsername</property>
<property name="password">mypass</property>
</constructor-arg>
</bean>
第一个参数有效 (MYSQL),但我不知道如何在 XML 中创建属性对象并将其传递给我的服务。有帮助吗?
尝试添加 util:properties
并将其作为 ref
传递给构造函数。我没有测试过,但应该给你想法。
<util:properties id="myproperties" location="classpath:/myproperties.properties" />
<bean id="service" class="service.service">
<constructor-arg >
<value>MYSQL</value>
</constructor-arg>
<constructor-arg ref="myproperties"/>
</bean>
我想使用 XMl 在 Spring 中自动装配我的服务。 我想要自动装配的 class 看起来像这样:
public class Service{
public Service(String type,Properties property){....}
....
}
我的控制器中有这个对象的一个实例。
@Autowired
protected Service service;
所以现在我不想在我的 xml 文件中实例化这个对象。这在我开始使用我的构造函数中的属性之前就起作用了,所以一切都以正确的方式配置。但是现在我已经添加了属性 Obect,我不知道如何将它们传递给构造函数。这是现在的样子:
<bean id="service" class="service.service">
<constructor-arg >
<value>MYSQL</value>
</constructor-arg>
<constructor-arg >
<property name="url">jdbc:....</property>
<property name="user">myUsername</property>
<property name="password">mypass</property>
</constructor-arg>
</bean>
第一个参数有效 (MYSQL),但我不知道如何在 XML 中创建属性对象并将其传递给我的服务。有帮助吗?
尝试添加 util:properties
并将其作为 ref
传递给构造函数。我没有测试过,但应该给你想法。
<util:properties id="myproperties" location="classpath:/myproperties.properties" />
<bean id="service" class="service.service">
<constructor-arg >
<value>MYSQL</value>
</constructor-arg>
<constructor-arg ref="myproperties"/>
</bean>