Spring入门2___对三种签名方式的理解

以HelloWorld为例开始我们的Spring征程:

首先创建我们的HelloWorld类:

package cn.zhang.hello;

public class HelloWorld {	
	private String username;	
	public void setUsername(String username) {
		this.username = username;
	}
	public void sayHello() {
		System.out.println("HELLO,WORLD"+username);
	}
}

接下来创建我们的Test类:

package cn.zhang.hello;

import static org.junit.jupiter.api.Assertions.*;
import java.beans.BeanInfo;
import java.lang.reflect.Constructor;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import com.sun.jmx.mbeanserver.Introspector;

public class HelloWorldTest {

	//传统方式,正控;
	@Test
	void testOld() throws Exception {
		HelloWorld world = new HelloWorld();
		world.setUsername("zhang");
		world.sayHello();		
	}
	
	//使用Spring5之后
	@Test
	void TestIoc() throws Exception {		
			HelloWorld world = null;
			//1.从classpath路径去寻找配置文件,创建资源对象
			Resource resource = new ClassPathResource("applicationContext.xml");
			//2.根据资源对象,创建Spring Ioc容器对象	
			BeanFactory factory = new XmlBeanFactory(resource);
			//3.从spring ioc容器中获取指定名称的(helloworld)对象
			
			//签名1:Object getBean(String name) //根据bean对象在容器中名称来获取
			//world = (HelloWorld) factory.getBean("helloWorld");
			
			//签名2:<T> T getBean(Class<T> requiredType)//根据类型去寻找
			//world = factory.getBean(HelloWorld.class);
			
			//签名3:Object getBean(String name, Object... args)//根据名称加类型
		    world = factory.getBean("helloWorld", HelloWorld.class);    
			world.sayHello();  		
	}

对于其中的配置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
        https://www.springframework.org/schema/beans/spring-beans.xsd">   
    <!-- 引入其他的spring配置文件 -->
    <import resource="classpath:cn/zhang/hello/hello.xml"/>
</beans>

此处为了锻炼我们对在xml中如何引用其他xml文件使用的理解,我们在applicationContext.xml中使用了import resource来调用所需要的hello.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
        https://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="helloWorld" class="cn.zhang.hello.HelloWorld">   
        <property name="username" value="zhangjiayu"></property>
    </bean>    
 </beans>

运行结果:

HELLO,WORLDzhang
HELLO,WORLDzhangjiayu

&&三种签名方式中较为常见错误:

签名1:Object getBean(String name) ;
//根据bean对象在容器中名称来获取

world = (HelloWorld) factory.getBean("helloWorld");

&若bean对象不在Spring容器中(修改参数为其他),则会出现:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'helloWorldzzzzzz' available

签名2: T getBean(Class requiredType)
//根据类型在容器中去寻找

world = factory.getBean(HelloWorld.class);

&若增加xml中的HelloWorld类型一个的bean,则出现:

NoUniqueBeanDefinitionException: No qualifying bean of type 'cn.zhang.hello.HelloWorld' available: expected single matching bean but found 2: helloWorld,helloWorld222

&若在xml文件中,多个bean的名称是helloworld,则会出现:

BeanDefinitionParsingException: Configuration problem: Bean name 'helloWorld' is already used in this <beans> element
Offending resource: class path resource [cn/zhang/hello/hello.xml]

签名3:Object getBean(String name, Object… args)
//根据名称加类型

world = factory.getBean("helloWorld", HelloWorld.class);    

在以后的使用中推荐此签名方式;

发布了11 篇原创文章 · 获赞 0 · 访问量 112

猜你喜欢

转载自blog.csdn.net/qq_43395576/article/details/103946453