一:配置文件包含关系

1.创建对应的实体类

public class Student {   //学生实体类

    private  String   name;  //姓名
private Integer age; //年龄
private Grade grade; //年级 @Override
public String toString() {
return "Student [name=" + name + ", age=" + age + ", grade=" + grade
+ "]";
} public Student() {
super();
}
public Student(String name, Integer age, Grade grade) {
super();
this.name = name;
this.age = age;
this.grade = grade;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Grade getGrade() {
return grade;
}
public void setGrade(Grade grade) {
this.grade = grade;
}
}

Student实体类

public class Grade {   //年级实体类
private String name; //年级名称 @Override
public String toString() {
return "Grade [name=" + name + "]";
} public Grade() {
super();
} public Grade(String name) {
super();
this.name = name;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} }

Grade实体类

2.创建配置文件

<?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"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--年级的Bean -->
<bean id="grade" class="cn.bdqn.bean.Grade" p:name="1年级"/> </beans>

子配置文件1

<?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"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--学生的Bean 属性grade 在这个容器中没有对应的bean -->
<bean id="student" class="cn.bdqn.bean.Student" p:name="小马哥"
p:age="50" p:grade-ref="grade"/> </beans>

子配置文件2

<?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"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 01.把其他的子文件包含进来
<import resource="spring-grade.xml"/>
<import resource="spring-student.xml"/> -->
<!-- 02.把其他的子文件包含进来 当前的这个主的配置文件不能命名成spring-* 这种格式 -->
<import resource="spring-*.xml"/>
</beans>

总配置文件

3.创建对应的测试类

public class StudentTest {

    //配置文件的包含关系
@Test
public void test01(){
ApplicationContext context=
new ClassPathXmlApplicationContext("applicationContext.xml");
Student student=(Student) context.getBean("student");
System.out.println("student信息:"+student);
} }

测试类

效果图如下

二:配置文件平级关系

1.删除上面练习中的总配置文件,只剩下两个平级的xml文件

2.书写测试类

public class StudentTest {

    //01.配置文件的平级关系
@Test
public void test01(){ //推荐使用 保证配置文件名称格式统一
ApplicationContext context=
new ClassPathXmlApplicationContext("spring-*.xml");
Student student=(Student) context.getBean("student");
System.out.println("student信息:"+student);
}
//02.配置文件的平级关系
@Test
public void test02(){
ApplicationContext context=
new ClassPathXmlApplicationContext("spring-student.xml","spring-grade.xml");
Student student=(Student) context.getBean("student");
System.out.println("student信息:"+student);
}
//03.配置文件的平级关系
@Test
public void test03(){
String resource1="spring-student.xml";
String resource2="spring-grade.xml";
String [] resources={resource1,resource2};
ApplicationContext context=
new ClassPathXmlApplicationContext(resources);
Student student=(Student) context.getBean("student");
System.out.println("student信息:"+student);
} }

测试类

注解配置!引入需要的aop.jar

/**
* 学生类
*/
@Component("student")
public class Student {
@Value("999")
private Integer age;
@Value("小黑")
private String name;
/**
* 01.
* @Autowired:
* 默认是按照byType 类型匹配
* @Autowired
@Qualifier("grades") 按照byName进行匹配 02.
@Resource
默认是按照byType 类型匹配
@Resource(name="grades")
*/
private Grade grade; public Student(Integer ages, String names, Grade grades) {
this.age = ages;
this.name = names;
this.grade = grades;
} public Student(Integer age, String name) {
this.age = age;
this.name = name;
} public Student() { } @Override
public String toString() {
return "Student [age=" + age + ", name=" + name + ", grade=" + grade
+ "]";
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Grade getGrade() {
return grade;
}
public void setGrade(Grade grade) {
this.grade = grade;
} }
/**
* 年级类
*/
@Component("grades")
public class Grade {
@Value("9年级")
private String name; //年级名称 public String getName() {
return name;
} //DI 依赖注入
public void setName(String name) {
this.name = name;
} public Grade(String name) {
super();
this.name = name;
} public Grade() {
super();
} @Override
public String toString() {
return "Grade [name=" + name + "]";
} }
<?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"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
"> <!-- 查询所有注解的类 -->
<context:component-scan base-package="cn.bdqn"/><!--扫描本包和其子包下面的所有文件 -->
<!--<context:component-scan base-package="cn.bdqn.*"/>扫描子包下面的所有文件 -->
</beans>

测试类

public class StudentTest {

    ApplicationContext context=null;
@Before
public void before(){
context=new ClassPathXmlApplicationContext("applicationContext.xml");
} @Test
public void test01(){
Student student=(Student) context.getBean("student");
System.out.println(student);
} }

spring05配置文件之间的关系的更多相关文章

  1. Spring(六)--Spring配置文件之间的关系

    Spring配置文件之间的关系 1.需要的实体类 2.需要的xml文件 3.测试类 未完待续!!!

  2. php CGI、Fastcgi、PHP-FPM的详细介绍与之间的关系

    以下PHP CGI.Fastcgi.PHP-FPM的一些信息归纳和汇总----->详细介绍与之间的关系 一:CGI是干嘛的?CGI是为了保证web server传递过来的数据是标准格式的 web ...

  3. [原创]java WEB学习笔记99:Spring学习---Spring Bean配置:自动装配,配置bean之间的关系(继承/依赖),bean的作用域(singleton,prototype,web环境作用域),使用外部属性文件

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  4. Spring基础——在 IOC 容器中 Bean 之间的关系

    一.在 Spring IOC 容器中 Bean 之间存在继承和依赖关系. 需要注意的是,这个继承和依赖指的是 bean 的配置之间的关系,而不是指实际意义上类与类之间的继承与依赖,它们不是一个概念. ...

  5. servlet和web容器之间的关系

    Java是一种动态加载和运行的语言.也就是说当应用程序持有一个类的地址(CLASSPATH)和名称(包名和类名)的情况下,可以在程序运行期 间任何时候加载这个类,并创建和使用该类的对象.Servlet ...

  6. UML类图及类与类之间的关系

    原文地址:http://www.uml.org.cn/oobject/201211231.asp 类图用于描述系统中所包含的类以及它们之间的相互关系,帮助人们简化对系统的理解,它是系统分析和设计阶段的 ...

  7. Python3 freetds.conf odbcinst.ini odbc.ini 之间的关系

    Python3 freetds.conf odbcinst.ini odbc.ini 之间的关系 三者分别是FreeTDS和UnixODBC的配置文件: 1,FreeTDS中的freetds.conf ...

  8. Spring(九):Spring配置Bean(二)自动装配的模式、Bean之间的关系

    XML配置里的Bean自动装配 Spring IOC容器可以自动装配Bean,需要做的仅仅是在<bean>的autowire属性里指定自动装配的模式,模式包含:byType,byName, ...

  9. 峰Spring4学习(5)bean之间的关系和bean的作用范围

    一.bean之间的关系: 1)继承: People.java实体类: package com.cy.entity; public class People { private int id; priv ...

随机推荐

  1. backbone学习笔记(一)

    因为工作的需要,从今天起对backbone的学习过程做下记录. 学习计划: 1.1周看基本知识(2014/1/18-2014/1/25) 2.基本知识总结(2014/1/26) 3.半周按教程写hel ...

  2. (转)Android Studio系列教程一下载与安装 背景Android Studio VS Eclipse准备下载创建HelloWorld项目

    背景 相信大家对Android Studio已经不陌生了,Android Studio是Google于2013 I/O大会针对Android开发推出的新的开发工具,目前很多开源项目都已经在采用,Goo ...

  3. JavaScript-学习一字符串

    字符串可以存储一系列字符,如 "John Doe". 字符串可以是插入到引号中的任何字符.你可以使用单引号或双引号: 用于字符串的 + 运算符 + 运算符用于把文本值或字符串变量加 ...

  4. 137 Single Number II(找唯一数Medium)

    题目意思:一个int数组,有一个数只出现一次,其他数均出现三次,找到这个唯一数 思路: 1.将所有数用2进制表示,计算每一位的数字和  1*3*n1+0*3*n2+c   唯一数对应位的数字(0或者1 ...

  5. 编写可维护的javascript代码---开篇(介绍自动报错的插件)

    文章开篇主要推荐了2款检测编程风格的工具: JSLint和JSHint: jsLint是由Douglas Crockford创建的.这是一个通用的javascript代码质量检测工具,最开始JSLin ...

  6. mysql存储过程的权限 definer

    mysql中用户对存储过程的权限有: ALTER ROUTINE 编辑或删除存储过程 CREATE ROUTINE 创建存储过程 EXECUTE运行存储过程 存储过程的创建者拥有存储过程的ALTER. ...

  7. python里的Join函数

    用法是将数组里的每个元素用字符连接起来 import string string.join(["aaaa", "bbb"]) 或者: from string i ...

  8. 在Qt中将函数发送到主线程执行

    考虑这样一种需求,使用Qt的线程类QThread在后台执行操作(比如说拷贝文件)的时候发生了错误,产生了一个错误信息需要提醒给用户,在后台输出很显然是不够的,因为用户可能根据就没有任何控制台可供程序输 ...

  9. 【HDOJ】2416 Treasure of the Chimp Island

    bfs().题目的数据乱码.应该如下: *****#********* *.......$...* *..***.......* *....*****..* *....******37A *****. ...

  10. 自己动手学TCP/IP–http协议(http报文头)

    在前面的一篇文章中,简单了介绍了HTTP报文格式,详情参考http://www.firefoxbug.net/?cat=47. 这里大概介绍下基本的,常见的HTTP包头格式. POST /report ...