还学spring(三)---注解实现的注入

       想看什么是依赖注入,可以看这个http://709002341.iteye.com/blog/2276992.

       本文只说如何实现注解方式的依赖注入。

       不明白注解是神马的童鞋建议看看这几篇:http://709002341.iteye.com/admin/blogs/2263016;

http://709002341.iteye.com/admin/blogs/2263265http://709002341.iteye.com/admin/blogs/2263585

       首先,我们应该有个观念,交给spring管理的bean(控制反转,依赖注入都是将bean交给spring管理,其实就是交给IOC容器管理,再说白点就是交给xml管理),都需要在xml文件里面注册bean的。这样就导致一个问题:xml文件越来越大,不容易管理,也显得混乱。

       spring3开始支持用注解的方式对bean进行管理。spring的注解系统很复杂,参数很多,本文只说最简单的,也是用的最多的两个注解,方便学习spring。

       首先是@Autowired。 做过spring项目的人不可能没见过这个注解吧。。  我现在的项目工程里,这个关键字是最多的。他相当于xml里面配置的property参数,负责将已经注册的bean注入到@Autowired注解修饰的变量里。

       还是看代码:

        xml文件:

<context:annotation-config/>
	<!-- 注册javabean -->
	<bean id="userBean" class="testSpring.business.bean.MyBean" />
<!-- 注册javabean -->
	<bean id="printBean2" class="testSpring.business.impl.Print2" />

 注意<context:annotation-config/>语句负责打开spring的注入配置,让你的系统能识别相应的注解。

   我的客户端bean类:

package testSpring.business.impl;

import java.util.Date;

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

import testSpring.business.iface.IPrint;

/**  
 *  Print2 : 测试按注解注入
 * @author xuejupo  [email protected] 
 * create in 2016-2-16 上午11:16:15    
 */
public class Print2 {
	//需要打印的bean,注入的入口(需要注入的对象)
	@Autowired @Qualifier("userBean")
	private IPrint printBean;
	public void print(){
		this.printBean.printObject();
	}
	@Autowired
	public void test(){
		System.out.println("这只是一个方法");
	}
}

 userBean太简单我就不写了。需要注意的是@Autowired默认是按类型匹配,如果xml文件里有多个IPrint接口的实现的话,就会出错,这时候就需要@Qualifier参数了,他负责将注入按名称匹配(就是id)。@Autowired注解也可以修饰方法,他修饰的方法,在bean被注册的时候就会调用(再具体我暂时不清楚了,了解一下吧)。比如上面代码的测试代码为:

//读取配置文件(将配置文件中的bean加载进内存)
		ApplicationContext ctx = new ClassPathXmlApplicationContext("/testSpring/resources/applicationContext.xml");
		//获取的实例  
		Print2 bean=(Print2)ctx.getBean("printBean2");  
		//调用方法  
		bean.print();

 结果:

这只是一个方法
打印对象MyBean:

 然后第二个个人认为最重要的注解,是@Service。当spring中的bean过多的时候,就会造成xml文件的臃肿,所以不仅注入我们希望用注解注入,对bean的注册我们也希望利用注解。@Service相当于xml文件里面的<bean/>语句,负责向spring注册bean。

       首先,需要在xml里增加语句<context:component-scan base-package="testSpring"></context:component-scan>,表示希望spring的xml文件在启动的时候加载包testSpring以及子包中所有的,被注解@Service("name")修饰的类(其实还有@Component、@Repository、@Service和@Controller,有兴趣的童鞋可以搜一下,暂时没发现他们有什么区别,本文不做讨论)。当找到后,作用就像在xml里写了这么一个语句:<bean id="name" class="testSpring.xx.xx">一样,完成了对bean的注册。

         比如,上面的例子里,xml只需这样写:

<context:component-scan base-package="testSpring"></context:component-scan>

        然后两个类都用注解注册:

package testSpring.business.impl;

import java.util.Date;

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

import testSpring.business.iface.IPrint;

/**  
 *  Print2 : 测试按注解注入
 * @author xuejupo  [email protected] 
 * create in 2016-2-16 上午11:16:15    
 */
//注释式注册
@Service("printBean2")
public class Print2 {
	//需要打印的bean,注入的入口(需要注入的对象)
	@Autowired @Qualifier("myBean")
	private IPrint printBean;
	public void print(){
		this.printBean.printObject();
	}
	@Autowired
	public void test(){
		System.out.println("这只是一个方法");
	}
}
package testSpring.business.bean;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Service;

import testSpring.business.iface.IPrint;

/**  
 *  MyBean : 
 * @author xuejupo  [email protected] 
 * create in 2016-2-16 上午10:11:43    
 */
@Service("myBean")
public class MyBean implements IPrint{

	@Override
	public String printObject() {
		// TODO Auto-generated method stub
		System.out.println("打印对象MyBean:");
		return null;
	}
}

测试代码和打印结果和xml均一致,我就不贴了。。

      小结:    

       上面只是很简单,很简单,很简单的spring通过注解注册bean的例子。只是为了让我们在学习spring的时候,看到这些注解大概知道是啥意思。相信肯定有大牛对此不屑一顾。。 我只是个接触spring时间不长的菜鸟,如果理解错什么了,欢迎大家留言喷我。。。

      再小结:

       个人觉得注解形式更好,看着心里更爽,维护起来感觉也痛快(我是特别膈应看xml文件的),所幸公司项目也基本上都是注解形式。

       最后小结:

       至于注解和xml的加载顺序,不清楚。。。 网上没搜到可信的答案,我自己试了试,注解依赖xml还是xml依赖注解都通过了。。。。   深入了解spring之后再去看这个吧,对学spring影响不大。spring的依赖注入本文和上一个博客基本上就学完了,其实也不难,不过在系统得学习spring之前,对什么是依赖注入一定要有个自己的理解。

猜你喜欢

转载自709002341.iteye.com/blog/2277256