第一个spring程序
1. 新建maven工程
2. 环境配置:
1. spring的jar包导入
2. spring的配置文件
位置:没有要求,任意位置都可以
命名:没有要求,但建议命名 applicationContext.xml
日后应用spring框架时,需要进行配置文件路径的配置
- pom.xml文件配置
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.22.RELEASE</version>
</dependency>
- spring配置文件创建
3. spring的核心API
-
ApplicationContext
1 作用:用于对象创建 2 好处:解耦合
-
ApplicationContext接口
1 接口 屏蔽实现的差异 2 非web环境 ClassPathXmlApplicationContext 3 web环境 XmlWebAaaplicationContext
-
重量级资源
1 ApplicationContext工厂的对象占用大量内存 2 不会频繁创建该对象,一个应用只会创建一个工厂对象 3 ApplicationContext工厂一定是线程安全的
-
4. 程序开发
1 创建类型
2 配置文件的配置,告知工厂需要创建的对象 applicationContext.xml
<!-- id属性 名字 唯一-->
<!-- class属性 配置全限定类名-->
<bean id="person" class="com.alex2ice.Person"></bean>
3 通过工厂类获取对象 ApplicationContext
5. 细节分析
spring工厂创建的对象,叫做bean或组件
import com.alex2ice.Person;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.time.Period;
/**
* @author Alex2ice
* @version v1.0.0
* @time 2023/2/15 15:30
* @description 测试spring的第一个程序
*/
public class Test1 {
@Test
public void test() {
// 创建spring的工厂
ApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext.xml");
// 通过工厂类,获取对象 getBean()
// 1. 需要强转
// Person person = (Person) ctx.getBean("person");
// 2. 配置中只有一个class是Person全限定名才可以
// No qualifying bean of type 'com.alex2ice.Person' available:
// expected single matching bean but found 2: person,person1
// Person person = ctx.getBean(Person.class);
// 3. 不需要强转,全限定名对应id个数没有限制
Person person = ctx.getBean("person", Person.class);
person.setName("alex");
System.out.println(person);
// 获取所有定义的beans的id值
String[] beanNames = ctx.getBeanDefinitionNames();
for (String name : beanNames) {
System.out.println(name);
}
// 根据类型获取id值
String[] personNames = ctx.getBeanNamesForType(Person.class);
for (String name : personNames) {
System.out.println(name);
}
// 是否存在指定id值的bean
System.out.println(ctx.containsBeanDefinition("person2"));
System.out.println(ctx.containsBean("person2"));
}
}
-
配置文件中注意的细节
1 只配置class属性 <bean class="com.alex2ice.Person"></bean> 有无id com.alex2ice.Person#0 默认id 应用场景 如果这个bean只需要使用一次,就可以省略id值 2 name属性 为bean对象定义别名 相同 getBean(name | id); ->Object 区别 name可以有多个,id只能有一个
import com.alex2ice.Person;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author Alex2ice
* @version v1.0.0
* @time 2023/2/15 17:30
* @description 测试spring配置bean没有id是否可以,测试是否会默认生成配置id
*/
public class Test2 {
@Test
public void test() {
ApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext.xml");
Person person = ctx.getBean(Person.class);
System.out.println(person);
String[] names = ctx.getBeanNamesForType(Person.class);
for (String name : names) {
System.out.println(name);
// com.alex2ice.Person#0 默认id
}
// 是否存在指定id值的bean
// <bean name="person" id="person2" class="com.alex2ice.Person"> </bean>
System.out.println(ctx.containsBeanDefinition("person"));
// 只能判断id名
System.out.println(ctx.containsBean("person"));
// id、name都可以
}
}
6. spring工厂的底层实现原理(简单版)
spring工厂可以调用对象私有构造方法创建对象
- 通过ClassPathXmlApplicationContext对象读取applicationContext.xml文件
- 解析出 bean 标签的相关信息: id 和class 信息
- 通过反射创建对象(会调用无参构造方法)
7. 思考
Q:是不是所有对象都有spring工厂来创建
A: 实体对象是不会交给spring来创建的,它是由持久层框架创建。