建立简单的spring项目,实现容器创建对象
只要由类就可以创建对象
- 新建maven项目:以maven-archetype-quickstart为模板删除多余的配置
- 加入spring依赖、junit依赖
- 创建类(含接口)
- 创建spring配置文件:beans.xml,声明类的信息,由spring创建和管理
- 测试类,测试声明对象
项目结构
pom文件
<?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>com.phy</groupId>
<artifactId>quickstart</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- spring 依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.6.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
bean.xml
<?xml version="1.0" encoding="UTF-8"?>
<!--
spring的配置文件
1、beans:是跟标签,spring把Java对象称为bean
2、spring-beans.xsd:是约束文件,和mybatis指定的dtd是一样的
-->
<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">
<!--
告诉spring声明对象
id:对象的自定义名称,唯一值,spring通过这个名称找到对象
class:类的全限定名称(不能是接口,因为spring通过反射机制创建对象,必须使用类)
一个bean标签声明一个对象;
通过该配置就完成 Dog dog = new Dog()的创建;
并把创建好的对象放进到map中,该map是spring框架用来存放对象的
相当于 map.put("dog","new Dog()")
-->
<bean id="dog" class="com.phy.service.impl.Dog"></bean>
<!--
spring创建一个非自定义的对象
-->
<bean id="mydata" class="java.util.Date"></bean>
</beans>
测试类
package com.phy.service.impl;
import com.phy.service.Animal;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class DogTest {
@Test
public void run(){
Animal dog = new Dog();
dog.run();
}
//使用spring容器创建的对象
@Test
public void run1(){
//指定bean配置文件
String beanConfig = "beans.xml";
//创建ApplicationContext spring容器对象,通过它获取dog对象
//ClassPathXmlApplicationContext:表示从类路径中加载spring的配置文件,是ApplicationContext的子类
ApplicationContext ac = new ClassPathXmlApplicationContext(beanConfig);
//从容器中获取对象,getBean("配置文件中的id");
Dog dog = (Dog) ac.getBean("dog");
dog.run();
}
}
对象的创建过程
- ClassPathXmlApplicationContext类读取bean配置文件,遇到bean标签根据class类名运用反射创建名为id的对象,并存到spring的map中
- ClassPathXmlApplicationContext类通过getBean(id)来获取创建的对象
spring默认创建对象的时间
在创建spring的容器时,会创建配置文件中所有的对象。
获取spring容器中Java对象的一些信息
@Test
public void getInfo(){
//指定bean配置文件
String beanConfig = "beans.xml";
//创建ApplicationContext spring容器对象,通过它获取dog对象
//ClassPathXmlApplicationContext:表示从类路径中加载spring的配置文件
ApplicationContext ac = new ClassPathXmlApplicationContext(beanConfig);
//获得容器中定义对象的数量
int beanDefinitionCount = ac.getBeanDefinitionCount();
//获得容器中每个对象的名称
String[] beanDefinitionNames = ac.getBeanDefinitionNames();
}