Spring继承注入与加载多个配置文件
2013-01-29 22:47
465 查看
一.Spring继承(parent)注入
1.抽象继承类使用 abstract="true"
2.子类继承 parent="父类Id"
二.Spring中引用对象方式:
1. local:在当Xml文件中查找对象
<property name="student3">
<ref local="student3" />
</property>
2. bean:在所有的Xml文件中查找对象
spring 默认使用bean方式
bean有两种方式
方式一:
<property name="student3">
<ref bean="student3" />
</property>
方式二:
<property name="student3" ref="student3"/>
3. parent:在父对象中查找
三.使用容器加载多个配置文件
(1)数组方式:
new ClassPathXmlApplicationContext(new String[] {"applicationContext.xml", "".... });
(2)通配符(*)
new ClassPathXmlApplicationContext("applicationContext*.xml"});
注意:配置文件命名统一
例:名字都是以applicationContext开头
applicationContext.xml(通用配置)
applicationContext-web.xml
applicationContext-biz.xml
applicationContext-dao.xml
举例:
Student1{studentId,studentName,password}
Student2{studentId,studentName}
Student3{address}
Student4{student2,student3}
两个配置文件
applicationContext.xml中
applicationContext-bean.xml中
测试类:
1.抽象继承类使用 abstract="true"
2.子类继承 parent="父类Id"
二.Spring中引用对象方式:
1. local:在当Xml文件中查找对象
<property name="student3">
<ref local="student3" />
</property>
2. bean:在所有的Xml文件中查找对象
spring 默认使用bean方式
bean有两种方式
方式一:
<property name="student3">
<ref bean="student3" />
</property>
方式二:
<property name="student3" ref="student3"/>
3. parent:在父对象中查找
三.使用容器加载多个配置文件
(1)数组方式:
new ClassPathXmlApplicationContext(new String[] {"applicationContext.xml", "".... });
(2)通配符(*)
new ClassPathXmlApplicationContext("applicationContext*.xml"});
注意:配置文件命名统一
例:名字都是以applicationContext开头
applicationContext.xml(通用配置)
applicationContext-web.xml
applicationContext-biz.xml
applicationContext-dao.xml
举例:
Student1{studentId,studentName,password}
Student2{studentId,studentName}
Student3{address}
Student4{student2,student3}
两个配置文件
applicationContext.xml中
<!-- 公共类必须定义成 abstract="true",让子类继承parent="student" --> <bean id="student" abstract="true"> <property name="studentId" value="1" /> <property name="studentName" value="老赵" /> </bean> <bean id="student2" class="com.tarena.entity.Student2" parent="student" />
applicationContext-bean.xml中
<bean id="student1" class="com.tarena.entity.Student1" parent="student"> <property name="password" value="123456" /> </bean> <bean id="student3" class="com.tarena.entity.Student3"> <property name="address" value="北京海淀" /> </bean> <bean id="student4" class="com.tarena.entity.Student4"> <property name="student2"> <!-- 在当前xml文件中找对象 --> <ref bean="student2" /> </property> <!--<property name="student3"> <ref local="student3" /> </property> --> <property name="student3" ref="student3"/> </bean>
测试类:
public class StudetTest { private static Log log = LogFactory.getLog(StudetTest.class); private ApplicationContext ac; @Before public void setUp() { // ac =new ClassPathXmlApplicationContext("applicationContext.xml"); /*ac = new ClassPathXmlApplicationContext(new String[] { "applicationContext.xml", "applicationContext-bean.xml" });*/ ac = new ClassPathXmlApplicationContext("applicationContext*.xml"); } @Test @Ignore public void testStudent1() { Student1 student = (Student1) ac.getBean("student1"); log.info(student.getPassword()); log.info(student.getStudentId()); log.info(student.getStudentName()); } @Test @Ignore public void testStudent2() { Student2 student = (Student2) ac.getBean("student2"); log.info(student.getStudentId()); log.info(student.getStudentName()); } @Test @Ignore public void testStudent3() { Student3 student = (Student3) ac.getBean("student3"); log.info(student.getAddress()); } @Test //@Ignore public void testStudent4() { Student4 student4 = (Student4) ac.getBean("student4"); log.info(student4.getStudent2().getStudentId()); log.info(student4.getStudent2().getStudentName()); log.info(student4.getStudent3().getAddress()); } }
相关文章推荐
- spring-自动加载配置文件\使用属性文件注入
- spring-自动加载配置文件\使用属性文件注入
- spring-自动加载配置文件\使用属性文件注入
- SSH学习九 依赖注入及加载Spring配置文件的方法
- Spring之配置文件加载方式
- Spring Boot加载配置文件
- Spring中加载配置文件的方式
- spring初步学习---注入配置文件变量
- 在spring配置文件中引入properties文件--引出加载配置文件的对应方式
- spring List,Set,Map,Properties,array的使用配置文件注入实例
- Junit手动/自动加载spring配置文件
- spring-boot 如何加载rsources下面的自定义配置文件
- web.xml文件中配置( listenr, filter,servlet, spring)的加载顺序 研究(转)
- 【SpringMVC】加载自定义目录下的springmvc.xml配置文件(三)
- spring中加载配置文件
- spring配置文件加载流程
- Spring中使用Map、Set、List、数组、属性集合的注入方法配置文件
- spring boot 中手动加载配置文件
- spring配置文件加载
- spring 中加载xml配置文件的方式.