Spring初识
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) { = 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;}@Overridepublic 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;}@Overridepublic 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;}@Overridepublic String toString() {return "Zoo{" +"animalsList=" + animalsList +'}';}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""xmlns:xsi=""xsi:schemaLocation="://www.springframework/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>
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns=".0.0"xmlns:xsi=""xsi:schemaLocation=".0.0 .0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId&le</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>
import t.ApplicationContext;
import t.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 = newClassPathXmlApplicationContext(l");//通过bean的名称获取bean对象,bean名称就是xml中bean的idAnimals a1 = (Animals) Bean("a1");System.out.println(a1);Zoo zoo = (Bean("zoo");System.out.println(zoo);}
}
21:30:49.601 [main] DEBUG t.support.ClassPathXmlApplicationContext - Refreshing t.support.ClassPathXmlApplicationContext@6504e3b2
21:30:49.814 [main] DEBUG org.springframework.l.XmlBeanDefinitionReader - Loaded 9 bean definitions from class path resource [l]
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}]}
本文发布于:2024-02-02 16:07:14,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170686123244914.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |