一:配置文件包含关系

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. java web 学习(1)

    java web 应用的核心技术包括以下几个方面: jsp:进行输入和输出的基本手段 javabean:完成功能的处理 servlet:对应用的流程进行控制 jdbc:是与数据库进行交互不可缺少的技术 ...

  2. 打包静默安装参数(nsis,msi,InstallShield,InnoSetup)[转]

    有时我们在安装程序的时候,希望是静默安装的,不显示下一步下一步,这编访问来教大家如何来操作,现在常用的制作安装程序的软件有,  Microsoft Windows Installer  , Windo ...

  3. table 表格隔行换色实现

    table 表格隔行换色实现 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "htt ...

  4. C语言对数组取地址

    #include <stdio.h> main() { ] = {,,,,}; printf("a=%p\n" , a); printf("a=%p\n&qu ...

  5. 关于Chrome(谷歌浏览器)对docume,准确获取网页客户区的宽高、滚动条宽高、滚动条Left和Top

    对于document.compatMode,很多朋友可能都根我一样很少接触,知道他的存在却不清楚他的用途.今天在ext中看到 document.compatMode的使用,感觉这个对于我们开发兼容性的 ...

  6. Kafka 集群消息监控系统:Kafka Eagle

    Kafka Eagle 1.概述 在开发工作当中,消费 Kafka 集群中的消息时,数据的变动是我们所关心的,当业务并不复杂的前提下,我们可以使用 Kafka 提供的命令工具,配合 Zookeeper ...

  7. Docker - 通过swarm 管理 docker service

    创建一个 Docker service $ docker service create --replicas 1 --name myhelloworld alpine ping docker.com ...

  8. 『Python』 多线程 端口扫描器

    0x 00 Before Coding 当端口打开时,向端口发送 TCP SYN 请求,会返回一个 ACK 响应: 当端口关闭,返回的是 RST 响应: 0x 01 Coding  可以用 socke ...

  9. GitHub NetFlow

    https://github.com/search?l=Java&p=1&q=netflow&ref=searchresults&type=Repositories&a ...

  10. C#程序设计基础——常量

    C#程序设计基础——常量 常量是在编译时已知,并且在程序的生存期内不发生更改的不可变值.常量使用const修饰符进行声明. 常量必须在声明时初始化,且常量的类型必须为以下类型之一:sbyte/byte ...