天天看點

Spring注解驅動開發實戰 | 第十四篇:自動裝配-方法、構造器位置的自動裝配1 标注在方法位置2 标在構造器上 3 @Bean标注的方法建立對象的時候,方法參數的值從容器中擷取源碼下載下傳

1 标注在方法位置

在com.wsc.bean中使用@Component将Car類注冊進容器

package com.wsc.bean;

import org.springframework.stereotype.Component;

@Component
public class Car {
	
	public Car(){
		System.out.println("car constructor...");
	}
	
	public void init(){
		System.out.println("car ... init...");
	}
	
	public void detory(){
		System.out.println("car ... detory...");
	}

}
           

在com.wsc.bean下建立Boss.java,并用@Autowired 标注在setCar方法上

package com.wsc.bean;

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

//預設加在ioc容器中的元件,容器啟動會調用無參構造器建立對象,再進行初始化指派等操作
@Component
public class Boss {
	
	
	private Car car;
	

	public Car getCar() {
		return car;
	}

	@Autowired 
	//标注在方法,Spring容器建立目前對象,就會調用方法,完成指派;
	//方法使用的參數,自定義類型的值從ioc容器中擷取
	public void setCar(Car car) {
		this.car = car;
	}



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

在IOCTest_Autowired.java中添加測試方法testMetheod

@Test
	public void testMetheod(){
		AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConifgOfAutowired.class);
		
		Boss boss = applicationContext.getBean(Boss.class);
		Car car = applicationContext.getBean(Car.class);
		System.out.println("裝配到Boss的bean:"+boss+" 和容器中的bean:"+car);
		applicationContext.close();
	}
           

運作testMetheod,檢視裝配進Boss中的bean和容器中的bean是否一緻

Spring注解驅動開發實戰 | 第十四篇:自動裝配-方法、構造器位置的自動裝配1 标注在方法位置2 标在構造器上 3 @Bean标注的方法建立對象的時候,方法參數的值從容器中擷取源碼下載下傳

2 标在構造器上

在com.wsc.bean下建立Boss.java,并用@Autowired 标注在有參構造器上,如果一個類中隻有一個有參構造器,@Autowired可以省略

package com.wsc.bean;

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

//預設加在ioc容器中的元件,容器啟動會調用無參構造器建立對象,再進行初始化指派等操作
@Component
public class Boss {
	
	@Autowired
	public Boss(Car car){
		this.car = car;
		System.out.println("Boss...有參構造器");
	}


	private Car car;
	
   
	public Car getCar() {
		return car;
	}

	

	public void setCar(Car car) {
		this.car = car;
	}



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

在IOCTest_Autowired.java中運作testMetheod,檢視裝配進Boss中的bean和容器中的bean是否一緻

Spring注解驅動開發實戰 | 第十四篇:自動裝配-方法、構造器位置的自動裝配1 标注在方法位置2 标在構造器上 3 @Bean标注的方法建立對象的時候,方法參數的值從容器中擷取源碼下載下傳

3 @Bean标注的方法建立對象的時候,方法參數的值從容器中擷取

在com.wsc.bean中建立Color.java

package com.wsc.bean;

public class Color {
	private Car car;

	public Car getCar() {
		return car;
	}

	public void setCar(Car car) {
		this.car = car;
	}

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

在MainConifgOfAutowired.java中把Color注冊進容器,此時的@Autowired可以省略,也可以寫在參數前color(@Autowired Car car)

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.bean.Car;
import com.wsc.bean.Color;
import com.wsc.dao.PersonDao;


@Configuration
@ComponentScan({"com.wsc.servicer","com.wsc.dao",
	"com.wsc.controller","com.wsc.bean"})
public class MainConifgOfAutowired {
	
	@Primary
	@Bean("personDao2")
	public PersonDao personDao(){
		PersonDao personDao = new PersonDao();
		personDao.setLable("2");
		return personDao;
	}
	
	/**
	 * @Bean标注的方法建立對象的時候,方法參數的值從容器中擷取
	 * @param car
	 * @return
	 */
	@Bean
	public Color color(Car car){
		Color color = new Color();
		color.setCar(car);
		return color;
	}
	
}
	
           

在IOCTest_Autowired.java中運作testMetheod,檢視裝配進Boss中的bean和容器中的bean是否一緻

Spring注解驅動開發實戰 | 第十四篇:自動裝配-方法、構造器位置的自動裝配1 标注在方法位置2 标在構造器上 3 @Bean标注的方法建立對象的時候,方法參數的值從容器中擷取源碼下載下傳

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

源碼下載下傳

繼續閱讀