spring的分散配置
来源:互联网 发布:杨辉三角java代码* 编辑:程序博客网 时间:2024/04/28 11:57
spring的分散配置 (利用资源文件给xml配置文件里属性赋值)
1,建立配置文件,MyBean.properties
MyBean.properties代码如下:
MyBean.name=\u8D85\u4EBA
MyBean.value=30
dd.title=\u9AD8\u9E4F
2,建立NewMyBean类
NewMyBean.class代码如下:
package cn.entity;
public class NewMyBean {
private String name;
private String value;
private String title;
.
.get和set方法省略
.
}
3,装配config.PropertyPlaceholderConfigurer类 文件名为a.xml
a.xml代码如下:
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>MyBean.properties</value>
</property>
</bean>
</beans>
4,装配NewMyBean类 文件名为b.xml
b.xml代码如下:
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="newMyBean" class="cn.entity.NewMyBean">
<property name="name">
<value>${MyBean.name}</value>
</property>
<property name="value">
<value>${MyBean.value}</value> <!--此处是调取资源文件里的值-->
</property>
<property name="title">
<value>${dd.title}</value>
</property>
</bean>
</beans>
5,最后一步就是测试啦,测试spring的分散配置
测试类代码如下:
ApplicationContext ctx = new ClassPathXmlApplicationContext(
new String[] { "a.xml", "b.xml" });
NewMyBean newMyBean = (NewMyBean) ctx.getBean("newMyBean");// 从容器里取
System.out.println("name:" + newMyBean.getName() + "\n");
System.out.println("value:" + newMyBean.getValue());
System.out.println("title:" + newMyBean.getTitle());
1,建立配置文件,MyBean.properties
MyBean.properties代码如下:
MyBean.name=\u8D85\u4EBA
MyBean.value=30
dd.title=\u9AD8\u9E4F
2,建立NewMyBean类
NewMyBean.class代码如下:
package cn.entity;
public class NewMyBean {
private String name;
private String value;
private String title;
.
.get和set方法省略
.
}
3,装配config.PropertyPlaceholderConfigurer类 文件名为a.xml
a.xml代码如下:
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>MyBean.properties</value>
</property>
</bean>
</beans>
4,装配NewMyBean类 文件名为b.xml
b.xml代码如下:
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="newMyBean" class="cn.entity.NewMyBean">
<property name="name">
<value>${MyBean.name}</value>
</property>
<property name="value">
<value>${MyBean.value}</value> <!--此处是调取资源文件里的值-->
</property>
<property name="title">
<value>${dd.title}</value>
</property>
</bean>
</beans>
5,最后一步就是测试啦,测试spring的分散配置
测试类代码如下:
ApplicationContext ctx = new ClassPathXmlApplicationContext(
new String[] { "a.xml", "b.xml" });
NewMyBean newMyBean = (NewMyBean) ctx.getBean("newMyBean");// 从容器里取
System.out.println("name:" + newMyBean.getName() + "\n");
System.out.println("value:" + newMyBean.getValue());
System.out.println("title:" + newMyBean.getTitle());
- spring的分散配置
- spring的分散配置
- Spring的分散配置
- 使用spring的特殊bean --- 分散配置
- Spring分散配置
- spring 分散配置
- Spring 的 分散装配
- 分散配置的一个案例
- spring boot实战(第四篇)分散配置
- spring boot实战(第四篇)分散配置
- Spring框架——使用spring的特殊bean完成分散配置
- 分散配置
- 分散配置
- spring的注解使用和分散配置(10级 学员张帅鹏课堂总结)
- spring分散案例
- spring 分散装配
- 分散配置总结
- 关于在Spring配置特殊bean-分散配置的时候抛出的异常:无法找到元素 'context:component-scan' 的声明 的解决方法
- 对夏日校园着装的一些迂见
- 一年成为Emacs高手(像神一样使用编辑器)
- IOS面试题目 整理
- uva 10089 Repackaging
- 解决“服务没有及时响应启动或控制请求”
- spring的分散配置
- spring整合hibernate
- B\S架构的视频监控管理平台
- ORACLE--Connect By、Level、Start With的使用(Hierarchical query-层次查询)
- 无法从“void (__thiscall CtestDlg::* )(void)”转换为“LRESULT (__thiscall CWnd::* )(WPARAM,LPARAM)”
- Design Pattern_Visitor(访问者模式)
- 取消项目的SVN管理
- 解除SVN管理项目
- 关于近期的一些想法和学习规划