博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spring05配置文件之间的关系
阅读量:6480 次
发布时间:2019-06-23

本文共 4995 字,大约阅读时间需要 16 分钟。

一:配置文件包含关系

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.创建配置文件

子配置文件1
子配置文件2
总配置文件

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 + "]";    }                }

测试类

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);    }}

 

转载于:https://www.cnblogs.com/xtdxs/p/7097169.html

你可能感兴趣的文章
给HashMap做个深度剖析手术
查看>>
pip是个好东西
查看>>
pycharm
查看>>
Runtime.getRuntime()
查看>>
Ubuntu 16.04 insatll chrome
查看>>
天兔监控安装
查看>>
python笔记-正则表达式
查看>>
Java二进制指令代码解析
查看>>
我的Python学习记录
查看>>
quzatz --Could not load org.quartz.spi.Trigge...
查看>>
qml实现窗口的拖拽效果
查看>>
Spring 定时器使用
查看>>
STM32 FLASH模拟EEPROM实验
查看>>
ADO.NET中的五个主要对象
查看>>
Java操作excel表格
查看>>
关于String的replace()和replaceAll()的区别
查看>>
MySQL日期时间函数大全
查看>>
UML之用例图的学习笔记
查看>>
antisamy的使用方法
查看>>
屏幕自适应
查看>>