天天看點

Spring注解驅動開發實戰 | 第十三篇:自動裝配[email protected]和@Qualifier和@Primary源碼下載下傳

    1 一個bean

如果容器中隻有一種類型的一個bean預設優先按照類型去容器中找對應的元件:applicationContext.getBean(BookDao.class)

    2 多個bean

如果容器中相同類型的多個bean,并且bean名稱不同,比如@Repository("personDao")注冊了一個PersonDao,@Bean("personDao2") 也注冊了一個PersonDao

       2.1 情況1

在裝配時,可以用屬性名稱來指定需要裝配的bean,如圖:

Spring注解驅動開發實戰 | 第十三篇:自動裝配[email protected]和@Qualifier和@Primary源碼下載下傳

在容器中就按照這個屬性名查找:applicationContext.getBean("personDao"),

      2.2 情況2

在裝配時也可以使用@Qualifier指定需要裝配的元件的id,@Qualifier的優先級高于屬性名稱的優先級

Spring注解驅動開發實戰 | 第十三篇:自動裝配[email protected]和@Qualifier和@Primary源碼下載下傳

在容器中就按照這個id查找:applicationContext.getBean("personDao2")

3

如果容器中相同類型的多個bean,并且bean名稱相同,那麼注冊bean的時候使用@Primary可以讓Spring進行自動裝配的時候,優先使用該bean

@Primary和@Qualifier同時出現時,@Qualifier的優先級最高

如果某個bean沒有注冊進容器,在自動裝配的時候會報錯,如果不想報錯又不想注冊該bean則可以使用@Autowired(required=false)解決

4 案例

在com.wsc.dao、com.wsc.servicer、com.wsc.controller中的類分别注冊進容器

package com.wsc.dao;

import org.springframework.stereotype.Repository;

@Repository("personDao")
public class PersonDao {
	private String lable = "1";

	public String getLable() {
		return lable;
	}

	public void setLable(String lable) {
		this.lable = lable;
	}

	@Override
	public String toString() {
		return "PersonDao [lable=" + lable + "]";
	}
	
}
           
package com.wsc.servicer;

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

import com.wsc.dao.PersonDao;

@Service
public class PersonServicer {
	    @Qualifier("personDao2")
		@Autowired
		private PersonDao personDao;
		
		public void print(){
			System.out.println(personDao);
		}

		@Override
		public String toString() {
			return "PersonServicer [personDao=" + personDao + "]";
		}

		
		
}
           
package com.wsc.controller;

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

import com.wsc.servicer.PersonServicer;

@Controller
public class PersonController {
	@Autowired
	private PersonServicer personServicer;
}
           

在com.wsc.config中建立MainConifgOfAutowired.java

package com.wsc.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import com.wsc.dao.PersonDao;


@Configuration
@ComponentScan({"com.wsc.servicer","com.wsc.dao",
	"com.wsc.controller"})
public class MainConifgOfAutowired {
	
	@Primary
	@Bean("personDao2")
	public PersonDao personDao(){
		PersonDao personDao = new PersonDao();
		personDao.setLable("2");
		return personDao;
	}
	
}
	
           

在com.wsc.test中建立IOCTest_Autowired.java

package com.wsc.test;

import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.wsc.config.MainConifgOfAutowired;
import com.wsc.dao.PersonDao;
import com.wsc.servicer.PersonServicer;

public class IOCTest_Autowired {
	
	@Test
	public void testAutowired(){
		AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConifgOfAutowired.class);
		
		PersonServicer personService = applicationContext.getBean(PersonServicer.class);
		System.out.println(personService);
		
		PersonDao bean = (PersonDao) applicationContext.getBean("personDao");
		System.out.println(bean);
		
		applicationContext.close();
	}

}
           

運作testAutowired得到結果:

Spring注解驅動開發實戰 | 第十三篇:自動裝配[email protected]和@Qualifier和@Primary源碼下載下傳

--------------------------------

源碼下載下傳

繼續閱讀