Spring2.5 Annotations

阅读更多

完成setXxxx功能,即配置文件的 可省略, eg:

public class Boss {
    //required = false 代表 xml 文件可以不配置 car bean,如不定义且没有 car bean,则跑出异常
    @Autowired(required = false)
    private Car car;
    
    // @Qualifier("office2")代表当 xml 文件有多个 office 类型bean时,选择 id="office2" 的bean
    //@Autowired
    // public void setOffice(@Qualifier("office2")Office office) {
    @Autowired
    @Qualifier("office2")
    private Office office;

    
    @Override
    public String toString() {
        return "Car :" + car + "\noffice: " + office;
    }

}

XML配置:
   
   
       
   

   
       
   
    
   
       
       
   

@Resource

类似 @Autowired,只不过 Resource 是  byName的,且默认可以不需要 属性
public class Boss {
    
    //定义类型
    @Resource(type=Car.class)
    private Car car;
    
    // 多个同一类型bean时,指定特定 bean,相当于 @Autowried + @Qualifier("office1")
    @Resource(name="office1")
    private Office office;

    public Car getCar() {
        return car;
    }

    public Office getOffice() {
        return office;
    }

    @Override
    public String toString() {
        return "car:" + car + "\n" + "office:" + office;
    }

}

@PostConstruct, @PreDestroy

Bean在初始化后和销毁前做的工作
public class Boss {

    @Resource
    private Car car;

    @Resource(name = "office")
    private Office office;

    public Car getCar() {
        return car;
    }

    public Office getOffice() {
        return office;
    }

    @Override
    public String toString() {
        return "car:" + car + "\n" + "office:" + office;
    }

    @PostConstruct
    public void postConstruct1() {
        System.out.println("postConstruct1");
    }
    
    //初始化后,功能同
    @PostConstruct
    public void postConstruct2() {
        System.out.println("postConstruct2");
    }
    
    //销毁前,功能同
    @PreDestroy
    public void preDestroy1() {
        System.out.println("preDestroy1");
    }

}

Main 函数:
public static void main(String[] args) {
        // TODO Auto-generated method stub
        String[] locations = { "annoations5/beans.xml" };
         ClassPathXmlApplicationContext ctx =  new ClassPathXmlApplicationContext(locations);

//        ApplicationContext ctx = new ClassPathXmlApplicationContext(locations);
        Boss boss = (Boss) ctx.getBean("boss");
        System.out.println(boss);
        
        ctx.destroy();
        

    }

测试结果:
postConstruct1
postConstruct2
car:brand:  红旗 CA72, price: 2000.0
office:Office No: 002
preDestroy1

@Component

某个属性类不需要在 XML 中定义对应的 bean

@Component
public class Car {
    
    private String brand;
    private double price;
    
    @Override
    public String toString() {
        return "brand: " + brand + ", price: " + price;
    }
...

@Scope("prototype")
@Component("boss")
public class Boss {

	private Car car;
	
	@Resource(name="office2")
	private Office office;
	
	@Override
	public String toString() {
		return "Car :" + car + "\noffice: " + office;
	}
...

XML配置
    
     
     
     
   



@Repository@Service 和 @Controller 在目前的 Spring 版本中,这 3 个注释和 @Component 是等效的

详见参考文档

spring2.5 context配置如下:

    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context-2.5.xsd">

      

         

你可能感兴趣的:(Spring2.5 Annotations)