Spring学习笔记(八)

1、Spring容器中Bean的作用域:

1)、singleton:单例模式,在整个Spring容器中,使用singleton来声明的Bean只会有一个实例。
2)、prototype:原型模式,每次通过getBean方法获取以prototype声明的Bean都会是一个新的Bean实例。
3)、request:对于每次HTTP请求,使用request定义的Bean都会产生一个新的Bean实例。
4)、session:对于每次HTTP Session,使用session定义的Bean都将产生一个新实例。

2、 singleton与prototype区别:

1)、singleton作用域的Bean,每次请求获得都是相同的Bean实例,容器负责跟踪Bean实例的状态,维护它的生命周期。
<bean id="chinese" class="com.sxit.service.Chinese" scope="singleton" />
2)、prototype作用域的Bean,每次getBean获得的都是不同的Bean实例,Spring容器只负责每次请求的时候创建一个新的Bean实例,不会再去跟踪这个实例和维护它的状态,由它自生自灭。
<bean id="chinese" class="com.sxit.service.Chinese" scope="prototype" />
3)、默认情况下是singleton。

 3、BeanFactory与ApplicationContext创建Bean的区别:

1)、BeanFactory创建的时候不会立即创建Bean实例,等需要某个Bean的时候才会去创建它
2)、ApplicationContext则是在创建的时候会把容器中所有singleton范围内的实例一起初始化,虽然一开始创建容器的时候开销会很大,但是后续相应的速度会更快,所以一般推荐使用ApplicationContext作为Spring容器。
3)、如果某个Bean声明了lazy-init="true",也就意味着初始化容器的时候不会初始化这个Bean,延迟加载,直到需要用到的时候才会去实例化这个Bean。

4、 Bean实例属性赋值:

1)、如果属性是一般的基本类型或者字符串类型值,可以这样赋值:
	<bean id="chinese" class="com.sxit.service.Chinese">
		<property name="name" value="傻逼"/>
		<property name="age" value="30"/>
	</bean>
2)、如果属性是另一个Bean实例,可以这样赋值:
	<bean id="otherBeanId" class="com.sxit.service.Chinese2">
	<bean id="chinese" class="com.sxit.service.Chinese">
		<property name="name" ref="otherBeanId"/>
		<property name="age" value="30"/>
	</bean>

5、自动装配:

1)、如果你比较懒,加上有些Bean的属性(依赖Bean)比较多,直接用<property .. ref="..."/>配置过于臃肿,所以可以使用autowire让Bean与Bean之间的依赖关系自动装配。
2)、自动装配减少配置文件的工作量,但降低了依赖关系的清晰性和可读性(读尼玛的配置文件费劲啊!)。
3)、autowire属性值:no,就是不使用自动装配,只能通过ref来显式指定依赖关系,读起来比较清晰有条理,但对于大的复杂的项目这样配置会比较臃肿;
4)、autowire属性值:byName,根据属性名自动装配,BeanFactory会去查找容器中id和属性名一样的Bean来完成注入,如果没找到,则不进行任何注入。
5)、autowire属性值:byType,根据属性类型来自动装配,BeanFactory会查找容器中所有Bean,类型与属性类型一致的则自动注入,如果有多个,则抛出异常,如果没有,则不注入。
6)、autowire属性值:constructor
7)、autowire属性值:default

6、byName:

package com.sxit.service;

public class NewPerson {
	
	private newChinese chinese;
	
	public void setChinese(newChinese chinese) {
		this.chinese = chinese;
	}

	public newChinese getChinese() {
		return chinese;
	}
}



package com.sxit.service;

public class newChinese {
	
	private String name;
	private int age;
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
} 

7、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-3.0.xsd ">

	<bean id="chinese" class="com.sxit.service.newChinese">
		<property name="age" value="30" />
		<property name="name" value="傻逼"/>
	</bean>
	
	<bean id="newPerson" class="com.sxit.service.NewPerson" autowire="byName"/>
	
</beans>

8、byName打印信息:

年龄为:30
姓名为:傻逼

9、byType(与byName一样,配置文件修改一下):

<?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-3.0.xsd ">

	<bean id="chinese" class="com.sxit.service.newChinese">
		<property name="age" value="30" />
		<property name="name" value="傻逼"/>
	</bean>
	
	<bean id="newPerson" class="com.sxit.service.NewPerson" autowire="byType"/>
	
</beans>

10、byType打印信息:

年龄为:30
姓名为:傻逼

11、如果xml中新增一个类型也为newChinese(在容器中找到newChinese类型多于1个,将抛出异常):

<?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-3.0.xsd ">

	<bean id="chinese" class="com.sxit.service.newChinese">
		<property name="age" value="30" />
		<property name="name" value="傻逼"/>
	</bean>
	
	<bean id="chinese2" class="com.sxit.service.newChinese">
		<property name="age" value="300" />
		<property name="name" value="傻逼2"/>
	</bean>
	
	<bean id="newPerson" class="com.sxit.service.NewPerson" autowire="byType"/>
	
</beans>

 12、抛出异常:

Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'newPerson' defined in class path resource [BeanByType.xml]: Unsatisfied dependency expressed through bean property 'chinese': : No unique bean of type [com.sxit.service.newChinese] is defined: expected single matching bean but found 2: [chinese, chinese2]; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.sxit.service.newChinese] is defined: expected single matching bean but found 2: [chinese, chinese2]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireByType(AbstractAutowireCapableBeanFactory.java:1199)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1091)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
	at com.test.TestByType.main(TestByType.java:12)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.sxit.service.newChinese] is defined: expected single matching bean but found 2: [chinese, chinese2]
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:800)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:707)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireByType(AbstractAutowireCapableBeanFactory.java:1184)
	... 13 more

 13、总结:

1)、如果既声明自动装配,有用ref显式指向具体Bean,则ref优先于自动装配。
2)、如果某些Bean不想自动装配被考虑,可以用autowire-candidate="false"。也可以通过<beans>元素中指定default-autowire-candidates="*abc",则以abc结尾的Bean均被排除在自动装配之外。

  

猜你喜欢

转载自luan.iteye.com/blog/1717802