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: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

只需要增加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());
	}
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM