系列文章目录
创建类
java类
animal类
实现 狗 2 》猫 3 》鸭 4 》 鸡 9 名字和年龄的顺序
//animal 名字分别为狗 2 》猫 3 》鸭 4 》 鸡 9
public class Animals {
private String name;
private Integer age;
private Animals next;
public Animals() {
}
public Animals(String name, Integer age) {
this.name = name;
this.age = age;
}
public Animals getNext() {
return next;
}
public void setNext(Animals next) {
this.next = next;
}
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;
}
@Override
public String toString() {
return "Animals{" +
"name='" + name + '\'' +
", age=" + age +
", next=" + next +
'}';
}
}
将类中的数据以链表的形式放到bean容器中
public class Animals1 {
private String name;
private Integer age;
public Animals1() {
}
public Animals1(String name, Integer age) {
this.name = name;
this.age = age;
}
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;
}
@Override
public String toString() {
return "Animals1{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
Zoo链表
import java.util.List;
public class Zoo {
private List<Animals1> animalsList;
public List<Animals1> getAnimalsList() {
return animalsList;
}
public void setAnimalsList(List<Animals1> animalsList) {
this.animalsList = animalsList;
}
@Override
public String toString() {
return "Zoo{" +
"animalsList=" + animalsList +
'}';
}
}
applications.xml
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!--通过bean标签定义bean对象,Spring Bean 容器是通过id来进行管理的,id相当于bean的名称,Spring可以通过id找到bean对象
如果没有提供id,默认以类名首字母小写作为id
默认是单例模式-->
<!--通过无参的构造方法创建一个对象, class,如果该类型没有无参的构造方法,就会报错-->
<!--通过ref链接到下一个对象-->
<bean id = "a1" class="Animals">
<property name="name" value="狗"/>
<property name="age" value="2"/>
<property name="next" ref="a2"/>
</bean>
<bean id = "a2" class="Animals">
<property name="name" value="猫"/>
<property name="age" value="3"/>
<property name="next" ref="a3"/>
</bean>
<bean id = "a3" class="Animals">
<property name="name" value="鸭"/>
<property name="age" value="4"/>
<property name="next" ref="a4"/>
</bean>
<bean id = "a4" class="Animals">
<property name="name" value="鸡"/>
<property name="age" value="9"/>
</bean>
<!--构造对象放入bean容器中,再放到Bean的list中-->
<bean id = "b1" class="Animals1">
<property name="name" value="狗"/>
<property name="age" value="2"/>
</bean>
<bean id = "b2" class="Animals1">
<property name="name" value="猫"/>
<property name="age" value="3"/>
</bean>
<bean id = "b3" class="Animals1">
<property name="name" value="鸭"/>
<property name="age" value="4"/>
</bean>
<bean id = "b4" class="Animals1">
<property name="name" value="鸡"/>
<property name="age" value="9"/>
</bean>
<!--使用List将animals中的动物放到ZOO中以链表形式打印-->
<bean id="zoo" class="Zoo">
<property name="animalsList">
<list>
<ref bean="b1"/>
<ref bean="b2"/>
<ref bean="b3"/>
<ref bean="b4"/>
</list>
</property>
</bean>
</beans>
pom.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>untitled</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!-- Spring 需要的依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.3.RELEASE</version>
</dependency>
<!-- 日志需要的依赖 -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
main函数
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.zip.GZIPOutputStream;
public class Main {
public static void main(String[] args) {
/**
* Spring 开启容器的方式: ApplicationContext 应用上下文(可以配置#营理Bean对象。及其他的工作)
* ClassPathXmLApplicationContext 根据classpath路径,指定一个xml 文件(配置文件)。
*并根据配置文件完成配置工作(Bean的实例化)
*/
ApplicationContext context = new
ClassPathXmlApplicationContext("applications.xml");
//通过bean的名称获取bean对象,bean名称就是xml中bean的id
Animals a1 = (Animals) context.getBean("a1");
System.out.println(a1);
Zoo zoo = (Zoo)context.getBean("zoo");
System.out.println(zoo);
}
}
运行结果
21:30:49.601 [main] DEBUG org.springframework.context.support.ClassPathXmlApplicationContext - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@6504e3b2
21:30:49.814 [main] DEBUG org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loaded 9 bean definitions from class path resource [applications.xml]
21:30:49.863 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'a1'
21:30:49.934 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'a2'
21:30:49.934 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'a3'
21:30:49.934 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'a4'
21:30:49.937 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'b1'
21:30:49.939 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'b2'
21:30:49.940 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'b3'
21:30:49.940 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'b4'
21:30:49.940 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'zoo'
Animals{name='狗', age=2, next=Animals{name='猫', age=3, next=Animals{name='鸭', age=4, next=Animals{name='鸡', age=9, next=null}}}}
Zoo{animalsList=[Animals1{name='狗', age=2}, Animals1{name='猫', age=3}, Animals1{name='鸭', age=4}, Animals1{name='鸡', age=9}]}