第三章、Spring5.x与日志框架的整合
1.Spring与日志框架进行整合,日志框架就可以在控制台中,输出Spring框架在运行过程中的一些重要信息。
2.好处:便于了解Spring框架运行过程,有利于程序的调试
Spring如何整合日志框架
默认
1.Spring1.x,2.x,3.x 早期都是于commons-logging.jar
2.Spring5.x默认整合的日志框架 logback log4j2
Spring5.x 整合log4j
1.引入log4j jar包
2.引入log4j。properties配置文件
pom:
引入jar包,导入坐标
<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>
log4j.properties
# resources文件夹根目录下
### 配置根
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
第四章、注入(injection)
1.什么是注入
1.通过Spring工厂及配置文件,为所创建对象的成员变量赋值
1.1 为什么需要注入
通过编码的方式,为成员变量进行赋值,存在耦合
用于测试注入
@Test
public void test3(){
ApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext.xml");
Person person =(Person)ctx.getBean("person");person.setId(1);
person.setName("liuleis");
System.out.println("person="+person);
}
1.2 如何进行注入[开发步骤]
类的成员变量提供set get方法
配置Spring的配置文件
<bean id = "person" name = "p,p1" class="com.liulei.Person">
<property name = "id">
<value>10</value>
</property>
<property name="name">
<value>xiaojianren</value>
</property>
</bean>
1.3 注入好处
解耦合
2.Spring注入的原理分析(简易版)
第五章、Set注入详解
Set注入的变量类型:
JDK内置类型
8中基本类型+String、数组类型、set集合、List集合、Map集合、Properties集合
用户自定义类型
1.针对于不同类型的成员变量,在<property>标签,需要嵌套其它标签
<property>
xxxxx
</property>
1.JDK内置类型
1.1 String+8种基本类型
<value>liuleis</value>
1.2 数组类型
<list>
<value>2099286078@qq.com</value>
<value>2099234238@qq.com</value>
<value>2086534078@qq.com</value>
</list>
1.3 Set数组 --无序,不可重复
<property name="tels">
<set>
<value>18888063185</value>
<value>89874643543</value>
</set><set>
<ref bean
<set
</set>
</property>
1.4 List集合 --有序,可重复
<property name="addresses">
<list>
<value>188</value>
<value>898</value>
<list><ref bean
<set
</list>
</list>
</property>
1.5 Map集合
<property name="qqs">
注意:map -- entry -- key有特定的标签 <key></key>值根据对应类型选择对应类型标签
<map>
<entry>
<key>
<value>liuleis</value>
<ref bean
</key>
<value>3568444563</value>
</entry>
</map>
</property>
1.6 Properties
<property name="p">
<props>
<prop key = "key1">value1</prop>
<prop key = "key2">value2</prop>
</props>
</property>
1.7 复杂的JDK类型(Date)
需要程序员自定义类型转换器,处理
2.用户自定义类型
2.1 第一种方式
为成员变量提供set get方法
配置文件中注入(赋值)
<bean id = "userService" class= "xxxx.UserService" >
<property name = "userDAO">
<bean class = "xxxx.UserDAOImpl">
</property>
</bean>
2.2 第二种方式
第一种赋值方式存在问题
1.配置文件代码冗余
2.被注入的对象(userDAO),多次创建,浪费(JVM)内存资源
为成员变量提供set get方法
配置文件中进行配置
<bean id="userDAO" class="com.yusael.dao.UserDAOImpl"></bean>
<bean id="userService" class="com.yusael.service.UserServiceImpl">
<property name="userDAO">
<ref bean="userDAO"/>
</property>
</bean>
Spring4.x 废除了 <ref local=""/> 基本等效 <ref bean=""/>;
3.Set注入的简化方式
3.1基于属性简化
1.JDK类型注入
<property name = "name">
<value>liuleis</value>
</property>简化:
<property name = "name" value = "suns">
注意:value属性 只能简化 8种基本类型+String注入标签用户自定义类型
<property nmae = "userDAO">
<ref bean = "userDAO">
</property>简化:
<property name = "userDAO" ref = "userDAO"/>
3.2 基于p命名空间简化
<property name = "name" class = "xxx.Person">
<value>liuleis</value>
</property>简化:
<bean id = "Person" class = "xxx.Person" p:name = ""/>
注意:value属性 只能简化 8种基本类型+String注入标签用户自定义类型
<property name = "userService" class = "xx.userServiceImpl">
<ref bean = "userDAO">
</property>
</bean>
简化:<bean id = "userService" class = "xxx.userServiceImpl" p:userDAO-ref = "userDAO"/