项目结构
项目展示
1.创建Student实体类
package com.it.bao4;
public class Student {
private int sno;
private String sname;
private int sage;
private School school;
public Student(){
System.out.println("spring调用了无参构造方法");
}
public Student(int sno, String sname, int sage, School school) {
this.sno = sno;
this.sname = sname;
this.sage = sage;
this.school = school;
System.out.println("spring调用了有参构造方法!");
}
public School getSchool() {
return school;
}
public void setSchool(School school) {
this.school = school;
}
public int getSno() {
return sno;
}
public void setSno(int sno) {
this.sno = sno;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public int getSage() {
return sage;
}
public void setSage(int sage) {
this.sage = sage;
}
@Override
public String toString() {
return "Student{" +
"sno=" + sno +
", sname='" + sname + ''' +
", sage=" + sage +
", school=" + school +
'}';
}
}
2.创建School实体类
package com.it.bao4;
public class School {
private String schName;
private String schAddress;
public String getSchName() {
return schName;
}
public void setSchName(String schName) {
this.schName = schName;
}
public String getSchAddress() {
return schAddress;
}
public void setSchAddress(String schAddress) {
this.schAddress = schAddress;
}
@Override
public String toString() {
return "School{" +
"schName='" + schName + ''' +
", schAddress='" + schAddress + ''' +
'}';
}
}
3.创建applicationContext.xml文件
4.创建测试文件
package com.it;
import com.it.bao4.Student;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class AppTest4
{
@Test
public void test1(){
String config="bao4/applicationContext.xml";
ApplicationContext ac= new ClassPathXmlApplicationContext(config);
//从容器中获取student对象
Student myStudent = (Student) ac.getBean("myStudent");
System.out.println("student对象:"+myStudent);
}
}
项目结果展示
(
2
)
byType
方式自动注入
使用
byType
方式自动注入,要求:配置文件中被调用者
bean
的
class
属性指定的类,
要与代码中调用者
bean
类的某引用类型属性类型同源。即要么相同,要么有
is-a
关系(子
类,或是实现类)。但这样的同源的被调用
bean
只能有一个。多于一个,容器就不知该匹配
哪一个了。
项目结构
同上
项目展示
1.创建Student实体类
同上 2.创建School实体类 同上 3.创建applicationContext.xml文件4.创建测试文件
package com.it;
import com.it.bao4.Student;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class AppTest4
{
@Test
public void test1(){
String config="bao4/applicationContext.xml";
ApplicationContext ac= new ClassPathXmlApplicationContext(config);
//从容器中获取student对象
Student myStudent = (Student) ac.getBean("myStudent");
System.out.println("student对象:"+myStudent);
}
}
项目结果展示
2.3.3
为应用指定多个
Spring
配置文件
在实际应用里,随着应用规模的增加,系统中
Bean
数量也大量增加,导致配置文件变
得非常庞大、臃肿。为了避免这种情况的产生,提高配置文件的可读性与可维护性,可以将
Spring
配置文件分解成多个配置文件。
包含关系的配置文件:
多个配置文件中有一个总文件,总配置文件将各其它子文件通过
Spring 配置文件:
也可使用通配符 * 。但,此时要求父配置文件名不能满足 * 所能匹配的格式,否则将出现 循环递归包含。就本例而言,父配置文件不能匹配 spring-*.xml 的格式,即 不能起名为 spring-total.xml 。total.xml
项目结构
同上
项目展示
1.创建Student实体类
同上 2.创建School实体类 同上 3.创建spring-student.xml文件4.创建spring-school.xml文件
5.创建total.xml文件
4.创建测试文件 同上 项目结果展示



