`
v5browser
  • 浏览: 1137423 次
社区版块
存档分类
最新评论

Spring继承注入与加载多个配置文件

 
阅读更多

一.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中

<!-- 公共类必须定义成 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.net框架

    我们的Factory就是利用这种方式根据配置文件动态加载程序集,动态创建对象并设置属性的。有了这个Factory,MainApp中的内容就很简单了: using System; namespace IocInCSharp { public class MainApp { public ...

    Spring.3.x企业应用开发实战(完整版).part2

    4.7 整合多个配置文件 4.8 Bean作用域 4.8.1 singleton作用域 4.8.2 prototype作用域 4.8.3 Web应用环境相关的Bean作用域 4.8.4 作用域依赖问题 4.9 FactoryBean 4.10 基于注解的配置 4.10.1 使用注解定义Bean ...

    Spring面试题

    ☆ Spring 上下文:Spring 上下文是一个配置文件,向 Spring 框架提供上下文信息。Spring 上下文包括企业服务,例如 JNDI、EJB、电子邮件、国际化、校验和调度功能。 ☆ Spring AOP:通过配置管理特性,Spring AOP ...

    Spring Boot中文文档.rar

    多个档案的YAML文件 24.7.4.YAML缺点 24.8.类型安全的配置属性 24.8.1.第三方配置 24.8.2.轻松绑定 24.8.3.合并复杂类型 24.8.4.属性转换 转换持续时间 转换数据大小 24.8.5.@...

    Spring中文帮助文档

    6.8.4. 在Spring应用中使用AspectJ加载时织入(LTW) 6.9. 更多资源 7. Spring AOP APIs 7.1. 简介 7.2. Spring中的切入点API 7.2.1. 概念 7.2.2. 切入点运算 7.2.3. AspectJ切入点表达式 7.2.4. 便利的切入...

    Spring攻略(第二版 中文高清版).part1

    1.13 继承Bean配置 47 1.13.1 问题 47 1.13.2 解决方案 47 1.13.3 工作原理 48 1.14 从Classpath中扫描组件 50 1.14.1 问题 50 1.14.2 解决方案 51 1.14.3 工作原理 51 1.15 小结 56 第2章 高级...

    Spring API

    6.8.4. 在Spring应用中使用AspectJ加载时织入(LTW) 6.9. 更多资源 7. Spring AOP APIs 7.1. 简介 7.2. Spring中的切入点API 7.2.1. 概念 7.2.2. 切入点运算 7.2.3. AspectJ切入点表达式 7.2.4. 便利的切入...

    Spring攻略(第二版 中文高清版).part2

    1.13 继承Bean配置 47 1.13.1 问题 47 1.13.2 解决方案 47 1.13.3 工作原理 48 1.14 从Classpath中扫描组件 50 1.14.1 问题 50 1.14.2 解决方案 51 1.14.3 工作原理 51 1.15 小结 56 第2章 高级...

    Spring3.x企业应用开发实战(完整版) part1

    4.7 整合多个配置文件 4.8 Bean作用域 4.8.1 singleton作用域 4.8.2 prototype作用域 4.8.3 Web应用环境相关的Bean作用域 4.8.4 作用域依赖问题 4.9 FactoryBean 4.10 基于注解的配置 4.10.1 使用注解定义Bean ...

    ssh(structs,spring,hibernate)框架中的上传下载

     由于Spring通过代理Hibernate完成数据层的操作,所以原Hibernate的配置文件hibernate.cfg.xml的信息也转移到Spring的配置文件中:  代码 4 Spring中有关Hibernate的配置信息 1. 2. !-- 数据源的配置 //--> 3. ...

    springboot参考指南

    改变应用程序外部配置文件的位置 iii. 63.3. 使用'short'命令行参数 iv. 63.4. 使用YAML配置外部属性 v. 63.5. 设置生效的Spring profiles vi. 63.6. 根据环境改变配置 vii. 63.7. 发现外部属性的内置选项 iii. 64....

    java面试题

    答:Servlet与CGI的区别在于Servlet处于服务器进程中,它通过多线程方式允许其service方法,一个实例可以服务于多个请求,并且其实例一般不会被销毁,而CGI对每个请求都产生新的进程,服务完后就销毁,所以效率上...

    java面试宝典

    42、一个“.java”源文件中是否可以包含多个类(不是内部类)?有什么限制? 12 43、说出一些常用的类,包,接口,请各举5 个。 12 44、Anonymous Inner Class (匿名内部类) 是否可以extends(继承)其它类?是否可以...

    千方百计笔试题大全

    42、一个“.java”源文件中是否可以包含多个类(不是内部类)?有什么限制? 12 43、说出一些常用的类,包,接口,请各举5 个。 12 44、Anonymous Inner Class (匿名内部类) 是否可以extends(继承)其它类?是否可以...

    Grails 中文参考手册

    2.2 创建一个Grails应用 2.3 Hello World示例 2.4 使用IDE 2.5 规约配置 2.6 运行Grails应用 2.7 测试Grails应用 2.8 部署Grails应用 2.9 所支持的Java EE容器 2.10 创建工件 2.11 生成Grails应用 3. 配置 3.1 基本...

    Java面试宝典2010版

    给一个 Bean 的 message 属性, 字符串类型, 注入值为 "Hello" 的 XML 配置文件该怎么写? 19、Jdo是什么? 20、什么是spring的IOC AOP 21、STRUTS的工作流程! 22、spring 与EJB的区别!! 八. 软件工程与设计...

    最新Java面试宝典pdf版

    给一个 Bean 的 message 属性, 字符串类型, 注入值为 "Hello" 的 XML 配置文件该怎么写? 125 19、Jdo是什么? 125 20、什么是spring的IOC AOP 126 21、STRUTS的工作流程! 126 22、spring 与EJB的区别!! 126 八. ...

    领域驱动设计与模式实战

    8.2.5 使用一个还是多个资源管理器 8.2.6 其他因素 8.2.7 选择和前进 8.3 方法 8.3.1 自定义手工编码 8.3.2 自定义代码的代码生成 8.3.3 元数据映射(对象关系(O/R)映射工具) 8.3.4 再次选择 8.4 分类 8.4.1 领域...

Global site tag (gtag.js) - Google Analytics