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是否一緻
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsIyZuBnL5EjN1EDMwcTM0EjMwkTMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
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是否一緻
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsIyZuBnL5EjN1EDMwcTM0EjMwkTMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
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是否一緻
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsIyZuBnL5EjN1EDMwcTM0EjMwkTMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
--------------------------------