在传统Spring架构中配置POJOs的基本操作有两种:装配和依赖注入。
- package test;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class Main {
- private BasicService service;
- public BasicService getService() {
- return service;
- }
- public void setService(BasicService service) {
- this.service = service;
- }
- public void print() {
- service.print();
- }
- public static void main(String[] args) {
- String[] locations = { "beans.xml" };
- ApplicationContext ctx =
- new ClassPathXmlApplicationContext(locations);
- Main main = (Main)ctx.getBean("main");
- main.print();
- }
- }
- package test;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class Main {
- private BasicService service;
- public BasicService getService() {
- return service;
- }
- public void setService(BasicService service) {
- this.service = service;
- }
- public void print() {
- service.print();
- }
- public static void main(String[] args) {
- String[] locations = { "beans.xml" };
- ApplicationContext ctx =
- new ClassPathXmlApplicationContext(locations);
- Main main = (Main)ctx.getBean("main");
- main.print();
- }
- }
- <?xml version="1.0" encoding="UTF-8" ?>
- <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns="http://www.springframework.org/schema/beans"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
- <bean id="main" class="test.Main">
- <property name="service">
- <ref bean="service" />
- </property>
- </bean>
- <bean id="service" class="test.BasicService"></bean>
- </beans>
@Autowired
@Autowired可以对成员变量、方法和构造函数进行标注,来完成自动装配的工作。
@Autowired的标注位置不同,它们都会在Spring在初始化这个bean时,自动装配这个属性。要使@Autowired能够工作,还需要在配置文件中加入以下:
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
Spring 将直接采用 Java 反射机制对私有成员变量进行自动注入。所以对成员变量使用 @Autowired 后,您大可将它们的 setter 方法()删除。
Autowired 是根据类型进行自动装配的。
例如,如果当Spring上下文中存在不止一个UserDao类型的bean时,就会抛出 BeanCreationException异常;
如果当Spring上下文中不存在UserDao类型的bean,也会抛出 BeanCreationException异常。
这时我们可以使用@Qualifier配合@Autowired来解决这些问题。如下:
可能存在多个UserDao实例
- @Autowired
- @Qualifier("userServiceImpl") //用@Qualifier()避免异常
- public IUserService userService;
推荐使用它来代替Spring专有的@Autowired注解。@Resource的作用相当于@Autowired,
只不过@Autowired按by Type自动注入,而@Resource默认按by Name自动注入。
@Resource有两个属性是比较重要的,分别是name和type,Spring将 @Resource注解的name属性解析为bean的名字,而type属性则解析为bean的类型。 所以如果使用name属性,则使用byName的自动注入策略,而使用type属性时则使用byType自动注入策略。如果既不指定name也不指定 type属性,这时将通过反射机制使用byName自动注入策略。要使@Autowired能够工作,还需要在配置文件中加入以下:
<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />
@Resource装配顺序a.如果同时指定了name和type,则从Spring上下文中找到唯一匹配的bean进行装配,找不到则抛出异常
b.如果指定了name,则从上下文中查找名称(id)匹配的bean进行装配,找不到则抛出异常
c.如果指定了type,则从上下文中找到类型匹配的唯一bean进行装配,找不到或者找到多个,都会抛出异常
d.如果既没有指定name,又没有指定type,则自动按照byName方式进行装配(见2);如果没有匹配,则回退为一个原始类型(UserDao)进行匹配,如果匹配则自动装配;
@Scope
在使用XML定义Bean时,我们可能还需要通过bean的scope属性来定义一个Bean的作用范围,我们同样可以通过@Scope注解来完成这项工作
eg:
- @Scope("session")
- @Component()
- public class UserSessionBean implements Serializable{
- ... ...
- }
Spring 2.5 在 @Repository 的基础上增加了功能类似的额外三个注解:@Component、@Service、@Constroller,它们分别用于软件系统的不同层次:
@Compoent 是一个泛化的概念,仅仅表示一个组件 (Bean) ,可以作用在任何层次。
@Service 通常作用在业务层,但是目前该功能与 @Component 相同。
@constroller通常作用在控制层,但是目前该功能与 @Component 相同。
通过在类上使用 @Repository、@Component、@Service 和 @Constroller 注解,Spring 会自动创建相应的 BeanDefinition 对象,并注册到 ApplicationContext 中。这些类就成了 Spring 受管组件。这三个注解除了作用于不同软件层次的类,其使用方式与 @Repository 是完全相同的.
//Spring 配置文件中启用注解标签及自动扫描功能
<context:annotation-config />
<context:component-scan base-package="com.revenue"></context:component-scan>