接上一篇笔记,本篇记一记Spring工厂获取bean以及配置文件bean标签的细节问题。
Spring工厂获取bean相关方法
context.getBean("person")
:根据bean标签的id属性获取context.getBean("person",Person.class)
:获取时指定类型,不用强制转换context.getBean(Person.class)
:直接使用类的类型,但是在xml文件中只能存在一个同类型的bean,不然就会报异常(为了测试,配置了person1):expected single matching bean but found 2: person,person1
context.getBeanDefinitionNames()
:获取的是spring配置文件中所有bean的id值context.getBeanNamesForType(Person.class)
:根据类型获取配置文件中对应的id值
context.containsBean("person")
:用于判断是否存在指定id值的beancontext.containsBeanDefinition("person")
:用于判断是否存在指定id值的bean,二者异同请看下方id和name的异同
Person person = context.getBean("person",Person.class);
Person person = context.getBean(Person.class);
System.out.println(person);
String[] beanDefinitionNames = context.getBeanDefinitionNames();
for (String s :beanDefinitionNames) {
System.out.println(s);
}
String[] beanNamesForType = context.getBeanNamesForType(Person.class);
for (String s :beanNamesForType) {
System.out.println(s);
}
//用于判断是否存在指定id值的bean
//System.out.println(context.containsBeanDefinition("person"));
System.out.println(context.containsBean("person"));
bean标签id,name,class
id: bean的唯一标识
name: bean的别名
class: bean对应的类
在applicationContext.xml文件中新添加以下内容:
<bean class="zyc.stu.bean.Person"/>
<bean id="person" name="p,p1" class="zyc.stu.bean.Person"/>
测试:
@Test
public void test3(){
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
/*只有class值,没有配置id
输出结果 zyc.stu.bean.Person#0
应用场景 如果这个bean只使用一次,就可以省略id值
如果这个bean会使用多次,或者被其他bean引用则需要设置id,ref=""
*/
String[] beanDefinitionNames = context.getBeanDefinitionNames();
for (String s :beanDefinitionNames) {
System.out.println(s);
}
System.out.println(context.getBean("p"));
}
id和name的异同:
相同点:
1.获取实例对象:getBean(id | name)
2.<bean id="" class=""/> 等效于 <bean name="" class=""/>
不同点:
1.别名可以定义多个,引号内用逗号隔开
2.XML的id属性的值,命名要求:必须以字母开头,可以跟字母、数字、下划线、连字符;
name属性的值,不存在命名要求,应用在特殊命名的场景下(例如spring+struts开发)
但是XML发展到了现在:ID属性的限制已经不存在了
3.判断bean是否存在的两个方法出现了差别:
containsBeanDefinition:同时配置了id和name,只能判断id,不能判断name
containsBean:id和name都能判断
Spring与log4j整合
首先引入两个依赖
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.30</version>
<scope>test</scope>
</dependency>
然后创建日志的配置文件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
测试:
@Test
public void test4(){
//整合日志框架:
//spring1,2,3默认都是commons-logging.jar
//spring5.x默认整合的是logback log4j2
//例:整合熟悉的log4j
//1.引入log4j jar包
//2.log4j.properties配置文件
//在pom文件中引入依赖:
// log4j
// slf4j-log4j12:顶替默认的日志文件
ApplicationContext context = new ClassPathXmlApplicationContext("/applicationContext.xml");
Person person = (Person) context.getBean("person");
System.out.println(person);
}
然后就能在控制台看到Spring的运行信息啦