Spring注解(零配置)

在使用Spring框架的时候,经常要用到XML文件作为它的配置文件,在xml中配置Bean的依赖、作用域、属性值、自动装配等。但除了xml文件配置方式,Spring还提供了另外一种配置方式——Annotation(注解),来达到“零配置”。

那么,Spring“零配置”存在一个问题,Spring容器如何知道哪些Java类是Spring Bean,进而对其预初始化呢?

这就要用到Annotation(注解)了,Spring通过注解来标注Java类为Spring Bean类。如,在包com.xingnana.controller下创建java类MainController,其中@Controller就是注解,标注一个控制器组件类。然后通过xml配置文件,显示指定Spring搜索指定包下的java类。

package com.xingnana.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class MainController {

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String index(){
        return "index";
        //return "test";
    }

}

同时,在mvc-dispatcher-sevlet.xml中添加<context:component-scan>元素,指定Spring要搜索的包,如下:

<?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"
       xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
       xmlns:tx="http://www.springframework.org/schema/tx"
       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
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc.xsd
       http://www.springframework.org/schema/data/jpa
       http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!--指明 controller 所在包,并扫描其中的注解 自动扫描指定包及其子包下的所有Spring bean类-->
    <context:component-scan base-package="com.xingnana.controller"/>

 <!-- 开启注解 -->
    <mvc:annotation-driven/>

<!-- 静态资源(js、image、css等)的访问 --> <mvc:default-servlet-handler/> </beans>

此外,还可以在<context:component-scan>元素下,添加<include-filter>元素,或者<exclude-filter>元素来指定Spring Bean。<include-filter>元素用于指定满足规则的java类,会被当做Spring Bean。<exclude-filter>元素用于指定满足规则的java类,不会被当做Spring Bean。这两个元素都有属性type和expression。

type:指定过滤器类型,如annotation(注解名),assignable(类名过滤器),regex(正则表达式),aspectj

expression:

Spring常用的注解有如下几个:

1. @Component:标注一个普通的Spring Bean;

2. @Controller:标注一个控制器组件类;

3. @Service:标注一个业务逻辑组件类;

4. @Repository:标注一个DAO组件类;

5. @Scope:指定Bean实例的作用域;如下:

//指定Spring Bean的作用域为prototype
@Scope("prototype")
//指定Student作为Spring Bean
@Controller
public class Student{
String name;
int age;
...
}

6. @Resource:配置依赖,相当于xml配置文件中元素<property>的属性ref,指向一个需要被注入的bean实例。它可以修饰setter方法和类属性,用法:@Resource(name="bean"),name属性可以省略。(name相当于xml配置文件中<bean>元素的id)

public class Student{
@Resource(name="person")//Filed注入,若省略name属性,则name属性默认值与Filed同名
private Person person;
@Resource//省略name属性,则name属性默认值为setPerson去掉set,且Perosn首字母P小写
public void setPerson(Person p){
this.person=p;
}

}

7. @PostConstruct和@PreDestory:用于修饰方法,分别与xml配置文件中<bean>元素的init-method和destory-method属性类似。@PostConstruct修饰init()方法,让Spring在该Bean的依赖关系注入完成之后回调该方法;@PreDestory修饰close()方法,让Spring在销毁该Bean之前回调该方法。

public class Student{
...
@PostConstruct
public void init(){
//初始化操作
}

@PreDestory
public void close(){
//释放资源等
}

}

8. @DependsOn:修饰Spring Bean类和方法,用于强制初始化其他Bean。以下是在初始化Student Bean之前,先强制初始化bena1和bean2。

@DependsOn({"bean1","bean2"})
@Cotroller
public class Student{
}

9. @Lazy:用于修饰Spring Bean类,用于指定该Bean是否预初始化,它有一个boolean的属性,@Lazy(true)。

默认情况下,Spring 会自动搜索注解标注的Java类,并把它们当做Spring Bean处理。


猜你喜欢

转载自blog.csdn.net/nancy50/article/details/79284079
今日推荐