Spring入门(6)-使用注解装配

Spring入门(6)-使用注解装配

本文介绍如何使用注解装配。

0. 目录

  1. 使用Autowired
  2. 可选的自动装配
  3. 使用Qualifier选择

1. 使用Autowired

package com.chzhao.springtest;

import org.springframework.beans.factory.annotation.Autowired;

public class PersonBll implements IPersonBll {

	public Person getPerson() {
		return person;
	}

	public void setPerson(Person person) {
		this.person = person;
	}

	@Autowired
	private Person person;

	public void show() {
		System.out.println(this.person.getName());
	}
}

注:@Autowired也可以放在setPerson上面。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 	
	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context-3.0.xsd
	"
	>
	<context:annotation-config/>
	<bean name="laoli" class="com.chzhao.springtest.Person">
		<property name="name" value="老李" />
	</bean>
	<bean  name="PersonBll" class="com.chzhao.springtest.PersonBll"/> 
</beans>

2. 可选的自动装配

如果没有定义可装配的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"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 	
	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context-3.0.xsd"
	>
	<context:annotation-config/>
	<bean id="bll" class="com.chzhao.springtest.PersonBll"></bean>
</beans>

如上配置文件中没有定义Bean,程序运行后,会抛出以下异常。

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'bll': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.chzhao.springtest.Person com.chzhao.springtest.PersonBll.person; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.chzhao.springtest.Person] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations:

只需要增加required=false即可,当然程序会抛出空指针异常。

package com.chzhao.springtest;

import org.springframework.beans.factory.annotation.Autowired;

public class PersonBll implements IPersonBll {

	public Person getPerson() {
		return person;
	}

	public void setPerson(Person person) {
		this.person = person;
	}

	@Autowired(required=false)
	private Person person;

	public void show() {
		System.out.println(this.person.getName());
	}
}

3. 使用Qualifier选择

如果定义了两个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"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 	
	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context-3.0.xsd"
	>
	<context:annotation-config/>
	<bean id="laowang" class="com.chzhao.springtest.Person">
		<property name="name" value="老王"></property>
	</bean>
	<bean id="laoli" class="com.chzhao.springtest.Person">
		<property name="name" value="老李"></property>
	</bean>
	<bean id="bll" class="com.chzhao.springtest.PersonBll"></bean>
</beans>

如上,定义了两个person,laowang和laoli,自动注入的时候会选择哪个呢?
实际上,会抛出如下异常:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'bll': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.chzhao.springtest.Person com.chzhao.springtest.PersonBll.person; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.chzhao.springtest.Person] is defined: expected single matching bean but found 2: laowang,laoli

通过Qualifier指定装配哪个Bean就可以了。

package com.chzhao.springtest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

public class PersonBll implements IPersonBll {

	public Person getPerson() {
		return person;
	}

	public void setPerson(Person person) {
		this.person = person;
	}

	@Autowired
	@Qualifier("laoli")
	private Person person;

	public void show() {
		System.out.println(this.person.getName());
	}
}
posted @ 2015-01-03 12:33  wardensky  阅读(3675)  评论(0编辑  收藏  举报