1 package com.asm; 2 3 4 import java.util.ArrayList; 5 import java.util.HashMap; 6 import java.util.List; 7 import java.util.Map; 8 import java.util.Properties; 9 10 import org.springframework.beans.BeansException; 11 import org.springframework.beans.factory.BeanFactory; 12 import org.springframework.beans.factory.BeanFactoryAware; 13 import org.springframework.beans.factory.BeanNameAware; 14 import org.springframework.beans.factory.DisposableBean; 15 import org.springframework.beans.factory.InitializingBean; 16 import org.springframework.context.annotation.Bean; 17 import org.springframework.context.annotation.Configuration; 18 import org.springframework.context.annotation.Scope; 19 import org.springframework.stereotype.Component; 20 21 @Configuration 22 public class Car { 23 private String brand; 24 private String color; 25 private int maxSpeed; 26 27 private List abc = new ArrayList(); 28 private Map map = new HashMap(); 29 private Properties mail = new Properties(); 30 31 private Boss boss; 32 @Scope("prototype") 33 @Bean 34 public Boss getBoss() { 35 return boss; 36 } 37 public void setBoss(Boss boss) { 38 this.boss = boss; 39 } 40 public Properties getMail() { 41 return mail; 42 } 43 public void setMail(Properties mail) { 44 this.mail = mail; 45 } 46 public Map getMap() { 47 return map; 48 } 49 public void setMap(Map map) { 50 this.map = map; 51 } 52 public List getAbc() { 53 return abc; 54 } 55 public void setAbc(List abc) { 56 this.abc = abc; 57 } 58 public String getBrand() { 59 return brand; 60 } 61 public void setBrand(String brand) { 62 this.brand = brand; 63 } 64 public String getColor() { 65 return color; 66 } 67 public void setColor(String color) { 68 this.color = color; 69 } 70 public int getMaxSpeed() { 71 return maxSpeed; 72 } 73 public void setMaxSpeed(int maxSpeed) { 74 this.maxSpeed = maxSpeed; 75 } 76 77 78 }
@Configuration 相当于 @@Component
@Scope 定义bean的作用域
用
AnnotationConfigApplicationContext启动注解类
1 ApplicationContext ctx = new AnnotationConfigApplicationContext(Car.class); 2 Car car = ctx.getBean(Car.class);
ctx.register(Boss.class); //注册多个@Configuration的配置类
3 System.out.println(car.getBoss());