一:配置文件包含关系

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. [置顶] Spring的DI依赖实现分析

    DI(依赖注入)是Spring最底层的核心容器要实现的功能之一,利用DI可以实现程序功能的控制反转(控制反转即程序之间之间的依赖关系不再由程序员来负责,而是由Spring容器来负责) 一个简单的例子( ...

  2. 用 javascript 判断 IE 版本号

    原文地址: http://julying.com/blog/determine-the-version-number-of-ie-with-javascript/ var _IE = (functio ...

  3. CentOS 6.5下安装MySql 5.7

    不管您按下面的方法安装成功否,请留个言,把您遇到的问题写上共勉! 包下载http://url.cn/WrNg5S 环境: 1).软硬件:E6420双核CPU,8G内存,1T硬盘 2).虚拟机下 Cen ...

  4. underscorejs-pluck学习

    2.14 pluck 2.14.1 语法: _.pluck(list, key) 2.14.2 说明: pluck方法根据key对list数组中的每个对象进行检索,返回检索成功的属性值,否则返回und ...

  5. phpcms v9自定义表单提交后返回上一页实现方法

    PHPcms v9中提交自定义表单后默认都是回到首页的,是不是感觉很不爽! 接下来,就说下phpcms v9自定义表单提交后返回上一页实现方法. 1.找到这个文件 phpcms\modules\for ...

  6. 移除IOS下按钮的原生样式

    写WAP页面的时候  一定要加上这组样式,以避免在IOS下面按钮被系统原生样式影响 input,textarea {outline-style:none;-webkit-appearance:none ...

  7. history对象属性和方法

    history对象保存着用户上网的历史记录,从窗口被打开的那一刻算起,history是window对象的属性,因此每个浏览器窗口.每个标签页乃至每个框架,都有自 己的history对象和特定的wind ...

  8. codevs 1107 等价表达式

    传送门 题解:第一眼这题好像非常难得样子,简直没有思路.但是这可以用栈带入特殊值来解决.这里用到两个栈,一个是存贮数字,另一个存贮运算符,按优先级进行运算.当读入的运算符比运算符栈的栈顶元素优先级低时 ...

  9. POJ 1287 Networking

    题目链接: poj.org/problem?id=1287 题目大意: 你被分派到去设计一个区域的连接点,给出你每个点对之间的路线,你需要算出连接所有点路线的总长度. 题目输入: 一个数字n  代表有 ...

  10. jquery 选择器 的学习,自己慢慢来

    1//加载所有元素后,执行下列代码 <script type="text/javascript"> $(document).ready(function(){ //选择 ...