9月5日-Spring的第一个程序
1.Spring的核心API
ApplicationContext
-
作用:Spring提供的
ApplicationContext
这个工厂,用于对象的创建;优点:解耦合
-
ApplicationContext
是接口类型;接口:屏蔽多种实现类的 差异
非Web环境下(main junit):
ClassPathXmlApplicationContext
Web环境下:
XmlWebApplicationContext
-
ApplicationContext
工厂的对象占用大量内存。不会频繁的创建对象,一个应用智慧创建一个工厂对象。
-
ApplicationContext
工厂:一定是线程安全的(多线程并发访问)
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-CvYNd71C-1630909286540)(C:\Users\dancerHoan\AppData\Roaming\Typora\typora-user-images\image-20210905134427289.png)]
2.实现步骤
2.1导入依赖
Spring框架依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.4.RELEASE</version>
</dependency>
测试依赖
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
2.2创建类
public class Person {
}
2.3修改配置文件
在resources文件夹下,创建xml文件applicationContext.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 http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 与工厂配置映射关系K V-->
<bean id="person" class="com.hoan.pojo.Person"></bean>
</beans>
2.4通过工厂类获得对象
public void test1(){
// 1.通过配置文件获取Spring工厂
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("/applicationContext.xml");
// 2.通过工厂类获取对象
Person person = (Person)applicationContext.getBean("person");
System.out.println("person:"+person);
}
3.细节分析
-
名词解释
Spring 工厂创建的对象,叫做 bean 或者 组件 (componet);
4.工厂类的相关方法
getBean()
形参String id,Class aClass
ApplicationContext app = new ClassPathXmlApplicationContext("/applicationContext.Xml");
Person person = app.getBean("person", Person.class);
getBean()
形参Class aClass 此时配置文件ApplicationContext.Xml文件中只有唯一Bean
ApplicationContext app = new ClassPathXmlApplicationContext("/applicationContext.xml");
Person person = app.getBean(Person.class);
getBeanDefinitionNames()
获取配置文件中 所有bean标签的id值
String[] beanDefinitionNames = app.getBeanDefinitionNames();
System.out.println(Arrays.toString(beanDefinitionNames));
getBeanDefinitionCount()
获取配置文件中 bean标签的数量
int beanDefinitionCount = app.getBeanDefinitionCount();
System.out.println(beanDefinitionCount);
getBeanNamesForType(Person.class);
通过类型获取配置文件中的 bean Id
String[] beanNamesForType = app.getBeanNamesForType(Person.class);
System.out.println(Arrays.toString(beanDefinitionNames));
containsBeanDefinition
:用于判断是否存在指定 id 值的 bean
containsBean
:同上
boolean person1 = app.containsBean("person");
boolean person = app.containsBeanDefinition("person");
System.out.println(person1+""+person);
5.配置文件中 需要注意的细节
-
`不配置id属性,只配置class属性``
-
``应用场景`:
如果这个bean只需要使用一次,就可以省略
如果这个bean会使用多次,或者被其他bean引用则需要设置id值
<bean class="com.hoan.pojo.Person"></bean>
String[] beanNamesForType = app.getBeanNamesForType(Person.class);
System.out.println(Arrays.toString(beanDefinitionNames));
// 输出结果是 id:com.hoan.pojo.Person#0 自动生成的
-
name属性
作用:在配置文件中,为bean对象定义别名
name 与 id 的相同点:
- app.getBean(“id”),app.getBean(“name”)都可以创建对象
<bean id="person" class="Person"/>
===<bean name="person" class="Person"/>
name与id的区别:
- id唯一,name可以多个 如下 多个name由 ,逗号 分割
<bean id="person" name="p1,p2,p3" class="com.hoan.pojo.Person"></bean>
-
containsBeanDefinition()不能判断name值,只能判断id值
containsBean(),可以判断id+name
6.Spring工厂的底层实现原理
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-jWLqL7m7-1630909286545)(C:\Users\dancerHoan\AppData\Roaming\Typora\typora-user-images\image-20210905143816920.png)]
6.Spring5.x 与 日志框架 的整合
- Spring 与日志框架进行整合,日志框架就可以在控制台中,输出Spring框架运行过程中的⼀
些重要的信息。 - 好处:便于了解Spring框架的运行过程,利于程序的调试。
6.1Spring5.x 整合 log4j:
- 引入依赖
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.21</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
- 配置文件
log4.properties
### 配置根
log4j.rootLogger = debug,console
### 日志输出到控制台显示
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.Target=System.out
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n`