Spring工厂类的其他方法:
引入依赖:pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.itheima</groupId>
<artifactId>spring02</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!--spring依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.14.RELEASE</version>
</dependency>
</dependencies>
</project>
先创建Person类:
package com.itheima;
public class Person {
}
创建配置文件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">
<!--
id:名字唯一
class属性:类的全限定名
-->
<bean id="person" class="com.itheima.Person"/>
<bean id="person1" class="com.itheima.Person"/>
<!-- <bean class="com.itheima.Person"/>-->
</beans>
测试类:
package com.itheima;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringTest {
//Spring的第一个程序
@Test
public void test1(){
//1.获得spring的工厂
ApplicationContext ctx=new ClassPathXmlApplicationContext("/applicationContext.xml");
//2.通过工厂类获得对象
Person person =(Person) ctx.getBean("person");
System.out.println("Person="+person);
}
/*
Spring工厂提供的其他方法:
getBean():有多个重载的方法
*/
@Test
public void test2(){
//1.获得spring的工厂
ApplicationContext ctx=new ClassPathXmlApplicationContext("/applicationContext.xml");
//2.通过工厂类获得对象的重载方法2 不清强制类型转换
// Person person = ctx.getBean("person", Person.class);
// System.out.println("Person="+person);
//这个方法,需要Spring工厂配置文件中只能有一个<bean class是Person类型,否则会报异常
// Person bean = ctx.getBean(Person.class);
// System.out.println("person="+bean);
//(2)获得Spring工厂配置文件中,所有的bean标签的id值 person person1
// String[] beanDefinitionNames = ctx.getBeanDefinitionNames();
// for (String beanDefinitionName:beanDefinitionNames){
// System.out.println("beanDefinitionName="+beanDefinitionName);
// }
//(3)获得特定类型获取Spring配置文件中对应的bean的id值
// String[] beanNamesForType = ctx.getBeanNamesForType(Person.class);
// for (String id:beanNamesForType){
// System.out.println("id="+id);
// }
//(4)用于判断是否存在指定id值得bean ,存在放回true,不存在返回false containsBeanDefinition()只能判断id不能用判断name
// boolean person = ctx.containsBeanDefinition("person");
// System.out.println(person);
//(5)用于判断是否存在指定id值得bean containsBean():可以用id判断,也可以判断name属性判断
if (ctx.containsBean("a")){
System.out.println("true="+true);
}else{
System.out.println("false="+false);
}
}
//测试Spring配置文件bean标签没有id属性是否含有默认属性
@Test
public void test3(){
//1.获得spring的工厂
ApplicationContext ctx=new ClassPathXmlApplicationContext("/applicationContext.xml");
String[] beanDefinitionNames = ctx.getBeanDefinitionNames();
for (String beanDefinitionName:beanDefinitionNames){
System.out.println("beanDefinitionName="+beanDefinitionName);
}
}
//用于测试name属性
@Test
public void test4(){
//1.获得spring的工厂
ApplicationContext ctx=new ClassPathXmlApplicationContext("/applicationContext.xml");
//2.通过工厂类获得对象
Person person =(Person) ctx.getBean("p1");
System.out.println("Person="+person);
}
}
第一个测试结果:
第二个方法的测试结果:
1.
2.因为配置问价有2个beanPerson类型的类,所以会报异常
3.
4.
5.
6.配置问价没有a类
方法3测试结果:
需要把配置文件中创建一个没有id的bean标签,并把有id的bean注释:
<?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">
<!--
id:名字唯一
class属性:类的全限定名
-->
<!-- <bean id="person" class="com.itheima.Person"/>-->
<!-- <bean id="person1" class="com.itheima.Person"/>-->
<!-- <bean class="com.itheima.Person"/>-->
<!--beand的name属性-->
<bean id="person" name="p,p1" class="com.itheima.Person"/>
</beans>
测试4