Spring annotation 、Spring @Autowired 注解解析

原创
2016/09/27 14:10
阅读数 48

官方文档地址:

http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#beans-annotation-config

注解:提供大量的上下文声明, 配置更简短。

XML:擅长连接组件不触碰自己的源代码或重新编译它们。

@Autowired

@Autowired可以对属性、方法和构造函数进行标注,完成自动装配的工作 。
@Autowired是根据类型进行自动装配的,如果需要按名称进行装配,则需要配合 @Qualifier 使用

注解构造函数

public class MovieRecommender {
    private final CustomerPreferenceDao customerPreferenceDao;

    @Autowired
    public MovieRecommender(CustomerPreferenceDao customerPreferenceDao) {
        this.customerPreferenceDao = customerPreferenceDao;
    }
}

注解方法

public class SimpleMovieLister {
    private MovieFinder movieFinder;

    @Autowired
    public void setMovieFinder(MovieFinder movieFinder) {
        this.movieFinder = movieFinder;
    } 
}
public class MovieRecommender {
    private MovieCatalog movieCatalog;

    private CustomerPreferenceDao customerPreferenceDao;

    @Autowired
    public void prepare(MovieCatalog movieCatalog,
            CustomerPreferenceDao customerPreferenceDao) {
        this.movieCatalog = movieCatalog;
        this.customerPreferenceDao = customerPreferenceDao;
    }  
}

注解属性:
成员变量添加@Autowired,Spring会直接将该类型的唯一一个bean赋值给这个成员变量;

成员变量set方法添加@Autowired,Spring会调用set方法将该类型的唯一一个bean装配到这个属性。

public class MovieRecommender {
    @Autowired
    private MovieCatalog[] movieCatalogs; 

    private Set<MovieCatalog> movieCatalogs;

    @Autowired
    public void setMovieCatalogs(Set<MovieCatalog> movieCatalogs) {
        this.movieCatalogs = movieCatalogs;
    }
}

 

 

创建 AutowiredDome 类:添加 ad2 属性变量并添加 @Autowired

package com.fengzhi.spring;

import org.springframework.beans.factory.annotation.Autowired;

public class AutowiredDome {

	@Autowired
	public AutowiredDome2 ad2;
	
}

创建 AutowiredDome2 类:创建id、name属性,并复制。

package com.fengzhi.spring;

public class AutowiredDome2 {

	private int id = 2;
	private String name = "luffy";

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

}

创建autowiredDome.xml,添加 autowiredDome、ad2 bean

<?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:context="http://www.springframework.org/schema/context"
	
	xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

	<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
	<!-- <context:annotation-config/> -->

	<bean id="autowiredDome" class="com.fengzhi.spring.AutowiredDome">
	</bean>
	
	<bean id="ad2" class="com.fengzhi.spring.AutowiredDome2">
	</bean>
</beans>

AutowiredAnnotationBeanPostProcessor @Autowired的处理器,查询所有带@Autowired的属性、方法、函数,自动添加对应 bean。

在spring配置文件中可以通过 <context:annotation-config /> 自动注册 AutowiredAnnotationBeanPostProcessor

创建测试类:TestAutowired

package com.fengzhi.test;

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

import com.fengzhi.spring.AutowiredDome;
 
public class TestAutowired {

	@Test
	public void testAutowired() {
		ApplicationContext context = new ClassPathXmlApplicationContext("autowiredDome.xml");

		AutowiredDome rd = (AutowiredDome) context.getBean("autowiredDome");

		System.out.println("Id : " + rd.ad2.getId());
		System.out.println("Name : " + rd.ad2.getName());
	}
}

运行结果:

@Autowired 自动将ad2对象实例化 ,所以可以获取到 AutowiredDome2 里面的数据

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部