Spring jar包
org.springframework spring-context 5.1.4.RELEASE
Spring的配置文件
- 配件文件位置没有要求配置文件名称没有要求以后需要主动告诉Spring
ApplicationContext
作用:创建对象,解耦合
它是一个接口,屏蔽具体的实现细节
- 非web环境: ClassPathXmlApplicationContextweb环境: xmlWebApplicationContext
重量级资源
对象占用大量内存
不会频繁的创建对象: 一个应用只会创建一个对象,面临着多线程问题
ApplicationContext一定是线程安全的(多线程并发访问)
3.程序开发1.创建类型 2.配置文件 applicationContext4.细节分析3.通过工厂创建对象 ApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext.xml"); Person person = (Person) ctx.getBean("person");
Spring创建的对象也叫做bean或者是component
常用方法
Person person0 = (Person)ctx.getBean("person");
Person person1 = ctx.getBean("person", Person.class);
// 使用这种方式获取对象,配置文件中只能有一个bean标签的类型是Person
Person person2 = ctx.getBean(Person.class);
// 获取Spring配置文件中所有bean的id值
String[] names = ctx.getBeanDefinitionNames();
// 根据类型获取Bean的id值
String[] namesForType = ctx.getBeanNamesForType(Person.class);
// 判断配置文件中是否含有id == person
boolean containsBeanDefinition = ctx.containsBeanDefinition("person");
// 判断配置文件中的 name == person ||id == person
boolean containsBean = ctx.containsBean("person");
配置文件细节
1.只配置class属性5.整合日志框架Spring默认id值:com.xia.basic.Person#0 若只使用一次 但是若使用多次且会被其他bean引用那么就需要起id 2.name属性相当于是bean的小名可以有多个,id是大名是唯一的
pom
log4j log4j 1.2.17 org.slf4j slf4j-log4j12 1.7.25
log4j.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



