一:配置文件包含关系
1.创建对应的实体类
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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; } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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; } }
2.创建配置文件
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?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>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?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>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?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.创建对应的测试类
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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.书写测试类
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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); } }