代码演示
- Person类
package Listen;
/**
-
Created with IntelliJ IDEA.
-
Description: If you don’t work hard, you will a loser.
-
User: Listen-Y.
-
Date: 2020-08-10
-
Time: 20:54
*/
public class Person {
private String name;
private int age;
public Person() {
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return “Person{” +
“name='” + name + ‘’’ +
“, age=” + age +
‘}’;
}
}
- Teacher类
package Listen;
/**
-
Created with IntelliJ IDEA.
-
Description: If you don’t work hard, you will a loser.
-
User: Listen-Y.
-
Date: 2020-08-11
-
Time: 15:25
*/
public class Teacher {
private Person person;
private String teacherId;
public Teacher() {
}
public Teacher(Person person, String teacherId) {
this.person = person;
this.teacherId = teacherId;
}
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
public String getTeacherId() {
return teacherId;
}
public void setTeacherId(String teacherId) {
this.teacherId = teacherId;
}
@Override
public String toString() {
return “Teacher{” +
“person=” + person +
“, teacherId='” + teacherId + ‘’’ +
‘}’;
}
}
- School类
package Listen;
import java.util.List;
/**
-
Created with IntelliJ IDEA.
-
Description: If you don’t work hard, you will a loser.
-
User: Listen-Y.
-
Date: 2020-08-11
-
Time: 15:26
*/
public class School {
private List teachers;
private String schoolName;
public School() {
}
public School(List teachers, String schoolName) {
this.teachers = teachers;
this.schoolName = schoolName;
}
public List getTeachers() {
return teachers;
}
public void setTeachers(List teachers) {
this.teachers = teachers;
}
public String getSchoolName() {
return schoolName;
}
public void setSchoolName(String schoolName) {
this.schoolName = schoolName;
}
@Override
public String toString() {
return “School{” +
“teachers=” + teachers +
“, schoolName='” + schoolName + ‘’’ +
‘}’;
}
}
- 配置文件中的配置项的每个bean对象
<beans xmlns=“http://www.springframework.org/schema/beans”
xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
- 根据容器获取bean并且使用bean对象
import Listen.Person;
import Listen.School;
import Listen.Teacher;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
//加载配置文件
//Spring开启容器的方式就是应用上下文(可以配置管理bean对象 及其他工作)
//根据classpath路径 指定一个配置文件
//根据配置文件完成配置工作(如bean的实例化)
ApplicationContext context = new
ClassPathXmlApplicationContext(“applications.xml”);
//通过bean的名称获取bean对象
String love = (String)context.getBean(“love”);
System.out.println(love);
//通过类型获得bean对象 如果该类型有多个对象 就会报错 只支持一个该类型的对象 返回的是一个泛型对象可以忽略强转
String love1 = context.getBean(String.class);
System.out.println(love1);
System.out.println(“测试自定义类”);
Person person = (Person) context.getBean(“person”);
System.out.println(person);
Person person2 = (Person)context.getBean(“person2”);
System.out.println(person2);
Person person3 = (Person)context.getBean(“person3”);
System.out.println(person3);
Teacher teacher1 = (Teacher)context.getBean(“teacher1”);
System.out.println(teacher1);
Teacher teacher2 = (Teacher)context.getBean(“teacher2”);
System.out.println(teacher2);
Teacher teacher3 = (Teacher)context.getBean(“teacher3”);
System.out.println(teacher3);
School school = (School)context.getBean(“school”);
System.out.println(school);
}
}
- 运行结果
16:15:44.050 [main] DEBUG org.springframework.context.support.ClassPathXmlApplicationContext - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@6504e3b2
16:15:44.337 [main] DEBUG org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loaded 8 bean definitions from class path resource [applications.xml]
16:15:44.403 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean ‘love’
16:15:44.495 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean ‘person’
16:15:44.530 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean ‘person2’
16:15:44.533 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean ‘person3’
16:15:44.534 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean ‘teacher1’
16:15:44.544 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean ‘teacher2’
16:15:44.547 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean ‘teacher3’
16:15:44.548 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean ‘school’
love
love
测试自定义类
Person{name=‘listen’, age=21}
Person{name=‘frank’, age=20}
Person{name=‘jake’, age=22}
Teacher{person=Person{name=‘listen’, age=21}, teacherId=‘5201314’}
Teacher{person=Person{name=‘frank’, age=20}, teacherId=‘7654321’}
Teacher{person=Person{name=‘jake’, age=22}, teacherId=‘1234567’}
School{teachers=[Teacher{person=Person{name=‘listen’, age=21}, teacherId=‘5201314’}, Teacher{person=Person{name=‘frank’, age=20}, teacherId=‘7654321’}, Teacher{person=Person{name=‘jake’, age=22}, teacherId=‘1234567’}], schoolName=‘HappySchool’}
Process finished with exit code 0
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=“http://www.springframework.org/schema/beans”
xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
西游记
水浒传
三国演义
听歌
看电影
跑步
LOL
CF
最后
小编精心为大家准备了一手资料
以上Java高级架构资料、源码、笔记、视频。Dubbo、Redis、设计模式、Netty、zookeeper、Spring cloud、分布式、高并发等架构技术
【附】架构书籍
- BAT面试的20道高频数据库问题解析
- Java面试宝典
- Netty实战
- 算法
BATJ面试要点及Java架构师进阶资料
ame=“cards”>
LOL
CF
最后
小编精心为大家准备了一手资料
[外链图片转存中…(img-VyYi9jSL-1723296847269)]
[外链图片转存中…(img-3pR2nHvq-1723296847269)]
以上Java高级架构资料、源码、笔记、视频。Dubbo、Redis、设计模式、Netty、zookeeper、Spring cloud、分布式、高并发等架构技术
【附】架构书籍
- BAT面试的20道高频数据库问题解析
- Java面试宝典
- Netty实战
- 算法
[外链图片转存中…(img-VJ3Ya0xk-1723296847270)]
BATJ面试要点及Java架构师进阶资料
[外链图片转存中…(img-EXDTYfqh-1723296847270)]