Spring annotation深入分析

Spring的依赖注入配置,除了利用xml配置文件以外,在Spring2.5以上版本后,鼓励
使用以@符号嵌入Java代码内部的annotaton注释

我们先来看一个传统的完全使用xml配置的例子

Office.java
package spring3.basic.annotation.compare;

public class Office {
	private  String officeNo = "001";    
	
	public String getOfficeNo() {
		return officeNo;
	}
	
	public void setOfficeNo(String officeNo) {
		this.officeNo = officeNo;
	}

	@Override    
	public String toString() {    
		return "Office No:" + officeNo;    
	}
}


Car.java:
package spring3.basic.annotation.compare;

public class Car {
	private String brand; 
	private double price;   
	
	public String getBrand() {
		return brand;
	}
	
	public void setBrand(String brand) {
		this.brand = brand;
	}

	public double getPrice() {
		return price;
	}

	public void setPrice(double price) {
		this.price = price;
	}
	
	@Override    
	public  String toString() {    
		return   "brand:" + brand +  "," + "price:" + price;    
	}
}


Boss.java(需要被注入的组合业务类)
package spring3.basic.annotation.compare;

public class Boss {
	private Car car; 
	private Office office;  
	
	public Car getCar() {
		return car;
	}

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

	public Office getOffice() {
		return office;
	}

	public void setOffice(Office office) {
		this.office = office;
	}
	
	@Override    
	public String toString() {    
		return "Car:" + car + "\n" + "Office:" + office;    
	}
}


接下来,我们能需要通过配置文件来实例化我们的主类和辅助的注入类
classicContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
		
	<bean id="boss" class="spring3.basic.annotation.compare.Boss" >    
		<property name="car" ref="car" />    
		<property name="office" ref="office" />    
	</bean>
	
	
	<bean id="office" class="spring3.basic.annotation.compare.Office">    
		<property name= "officeNo"  value= "002" />    
	</bean>    

	<bean id= "car" class = "spring3.basic.annotation.compare.Car">    
		<property name="brand" value="BMW X700" />    
		<property name="price" value="2000" />    
	</bean>    
</beans>    


编写测试类
ClassicBossDemo.java
package spring3.basic.annotation.compare;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ClassicBossDemo {
	public static void main(String[] args) {    
		String[] locations = { "annotation/classicContext.xml" };
		ApplicationContext ctx = new ClassPathXmlApplicationContext(locations); 
		Boss boss = (Boss)ctx.getBean("boss");    
		System.out.println(boss);    
	}
}



运行结果:
Car:brand:BMW X700,price:2000.0
Office:Office No:002


现在来看一下如果利用注释怎么做
首先介绍一下@Autowired注释
需要被注入的业务类的属性,一般是通过设置set方法后,再通过spring的配置文件
进行注入
如果设置了@Autowired自动绑定后,spring会自动根据ByType的形式去寻找对应类型下的类
在配置文件中,我们需要做的是
1. 指定spring通过annotation来注入
<context:annotation-config />

2. 指定注入类的搜索包
<context:component-scan base-package="spring3.basic.annotation.compare" />

如果同一个类有两个实例,需要去注入,怎么办呢?
    我们可以通过@Qualifier关键字,把默认的ByType注入改为ByName注入

@component关键字
  可以让我们省去在配置文件中实例化bean,可以把它和配置文件结合使用
  因为,默认使用@component实例化类,类的成员属性是不赋值的,如果需要初始化
  属性,还得使用配置文件

说了那么多了,下面来看一下代码

Farm.java
package spring3.basic.annotation.compare;

import  org.springframework.stereotype.Component;   

public class Farm {
	private  String farmNo;

	public String getFarmNo() {
		return farmNo;
	}

	public void setFarmNo(String farmNo) {
		this.farmNo = farmNo;
	}

	@Override    
	public String toString() {    
		return "Farm No:" + farmNo;    
	}
}


Cow.java
package spring3.basic.annotation.compare;

import  org.springframework.stereotype.Component;    

public class Cow {
	private String color; 
	private double price;   
	
	public String getColor() {
		return color;
	}

	public void setColor(String color) {
		this.color = color;
	}

	public double getPrice() {
		return price;
	}

	public void setPrice(double price) {
		this.price = price;
	}
	
	@Override    
	public  String toString() {    
		return   "Color: " + color +  "," + "Price: " + price;    
	}
}



Peasant.java(业务主类)
package spring3.basic.annotation.compare;

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

@Component
public class Peasant {
	@Autowired
	@Qualifier("whitecow")
	private Cow cow; 
	
	@Autowired 
	private Farm farm;  
	
	public Cow getCow() {
		return cow;
	}
	
	public Farm getFarm() {
		return farm;
	}
	
	@Override    
	public String toString() {    
		return "Cow: " + cow + "\n" + "Farm: " + farm;    
	}
}


annotationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
	
	<context:annotation-config />
	<context:component-scan base-package="spring3.basic.annotation.compare" />
	
	<bean id="farm" class="spring3.basic.annotation.compare.Farm">    
		<property name= "farmNo" value= "f001" />    
	</bean> 
	
	<bean id= "whitecow" class = "spring3.basic.annotation.compare.Cow">    
		<property name="color" value="white" />    
		<property name="price" value="1000" />    
	</bean> 
	
	<bean id= "blackcow" class = "spring3.basic.annotation.compare.Cow">    
		<property name="color" value="black" />    
		<property name="price" value="1200" />    
	</bean>
</beans>    


编写测试类
AnnotationPeasantDemo.java
package spring3.basic.annotation.compare;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AnnotationPeasantDemo {
	
	public static void main(String[] args) {    
		ApplicationContext ctx =     
				new ClassPathXmlApplicationContext("annotation/annotationContext.xml");    
		Peasant peasant = (Peasant)ctx.getBean("peasant");    
		System.out.println(peasant);  
		
	}
}


运行结果:
Cow: Color: white,Price: 1000.0
Farm: Farm No:f001




猜你喜欢

转载自kelvingjy.iteye.com/blog/1789450