Spring中通過註解配置bean(2)--@Autowired

Spring中通過註解配置bean(2)--@Autowired


一、說在前面

1、如果bean和bean之間有引用關係,那麼這時候就需要利用註解建立bean和Bean之間的關係。

2、@Autowired 註解,它可以對類成員變量、方法及構造函數進行標註,完成自動裝配的工作。通過 @Autowired的使用來消除 set ,get方法。
(1)@Autowired註解自動裝配具有兼容類型的單個bean屬性構造器,普通字段(即使是非public),一切具有參數的方法都可以應用@Autowired註解。
(2)默認情況下,有使用 @Autowired 註解的屬性都需要被設置。當Spring找不到匹配的bean裝配屬性時,會拋出異常,若某一屬性允許不被設置,可以設置@Autowired註解的required屬性爲 false 。
(3)默認情況下,當IOC容器裏存在多個類型兼容的bean時,通過類型的自動裝配將無法工作。此時可以在@Qualifier註解裏提供 bean 的名稱。Spring允許對方法的入參標註@Qualifiter 已指定注入bean的名稱。
(4)@Autowired 註解也可以應用在數組類型的屬性上, 此時 Spring 將會把所有匹配的 bean 進行自動裝配。
(5)@Autowired 註解也可以應用在集合屬性上, 此時 Spring 讀取該集合的類型信息, 然後自動裝配所有與之兼容的 bean 。
(6)@Autowired 註解用在 java.util.Map 上時, 若該 Map 的鍵值爲 String, 那麼 Spring 將自動裝配與之 Map 值類型兼容的 bean, 此時 bean 的名稱作爲鍵值。

3、Spring 還支持 @Resource 和 @Inject 註解,這兩個註解和 @Autowired 註解的功用類似。
(1)@Resource 註解要求提供一個 bean 名稱的屬性,若該屬性爲空,則自動採用標註處的變量或方法名作爲 bean 的名稱。
(2)@Inject 和 @Autowired 註解一樣也是按類型匹配注入的 Bean, 但沒有 reqired 屬性。
(3)建議使用 @Autowired 註解。

4、在使用@Autowired之前,我們對一個bean配置起屬性時,使用的是property屬性配置
<property name="屬性名" value=" 屬性值"/>


二、實例演示

1、UserRepository接口和對應的實現類

package com.at.beans.annotation.repository;

public interface UserRepository {

	void save();
}

package com.at.beans.annotation.repository;

import org.springframework.stereotype.Repository;

@Repository("userRepository")
public class UserRepositoryImpl implements UserRepository{

	public void save() {
		System.out.println("UserRepository Save......");
	}

}


2、UserService類

package com.at.beans.annotation.service;

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

import com.at.beans.annotation.repository.UserRepository;

@Service
public class UserService {
	@Autowired
	private UserRepository userRepository;
	
	public void add(){
		System.out.println("UserService add......");
		userRepository.save();
	}
}


3、UserController類

package com.at.beans.annotation.controller;

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

import com.at.beans.annotation.service.UserService;

@Controller
public class UserController {
	@Autowired
	private UserService userService;

	public void execute(){
		System.out.println("UserController execute......");
		userService.add();
	}
}


4、測試函數

		UserController userController = (UserController) ctx.getBean("userController");
		System.out.println(userController);
		userController.execute();

5、運算結果

六月 04, 2017 6:12:24 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@6be3f34c: startup date [Sun Jun 04 18:12:24 CST 2017]; root of context hierarchy
六月 04, 2017 6:12:24 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [beans-annotation.xml]
com.at.beans.annotation.controller.UserController@2e909348
UserController execute......
UserService add......
UserRepository Save......



三、說在後面

1、@Autowired的原理
其實在啓動spring IoC容器的時候,容器自動裝載了一個AutowiredAnnotationBeanPostProcessor後置處理器,當容器掃描到@Autowied、@Resource或@Inject時,就會在IoC容器自動查找需要的bean,並裝配給該對象的屬性

<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/> 


2、使用@Autowired的幾點注意事項
(1)在使用@Autowired時,首先在容器中查詢對應類型的bean,如果查詢結果剛好爲一個,就將該bean裝配給@Autowired指定的數據;
(2)如果查詢的結果不止一個,那麼@Autowired會根據名稱來查找。
(3)如果查詢的結果爲空,那麼會拋出異常。可以通過設置@Autowired(required=false)來解決。


By luoyepiaoxue2014

微博地址:http://weibo.com/luoyepiaoxue2014 點擊打開鏈接







發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章