spring 注解

来源:互联网 发布:数据库的数据模型包含 编辑:程序博客网 时间:2024/06/07 07:39

Spring 中提供了两种方式提供bean配置。一种是基于XML配置,另一种是基于注解的配置。
注解配置是基于java的反射机制实现的,它可以减少开发过程中来回切换xml和bean的次数,将配置和java代码放在一起,提高开发效率。

不使用注解实现bean配置

bean

package com.spring.beans;import org.springframework.stereotype.Service;public class BeanTest {    public void method()    {        System.out.println("bean 方法执行");    }}

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"       xsi:schemaLocation="http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"        >    <bean id="beanTest" class="com.spring.beans.BeanTest" />    </beans>

测试类 :

package com.spring.test;import com.spring.beans.BeanTest;import org.junit.Test;import org.springframework.context.support.ClassPathXmlApplicationContext;public class MainTest {    @Test    public void Test() {        ClassPathXmlApplicationContext context =        new ClassPathXmlApplicationContext("applicationContext.xml");        BeanTest test = (BeanTest) context.getBean("beanTest");        test.method();       context.close();    }}

测试结果

bean 方法执行

改为注解实现

修改xml

<?xml version="1.0" encoding="UTF-8"?><!--增加了context --><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-3.0.xsd        http://www.springframework.org/schema/context        http://www.springframework.org/schema/context/spring-context.xsd"        >    <!--启用注解-->    <context:annotation-config/></beans>

修改bean(添加注解)

package com.spring.beans;import org.springframework.stereotype.Component;@Componentpublic class BeanTest {    public void method()    {        System.out.println("bean 方法执行");    }}

测试结果

bean 方法执行

注解配置详解

使用到的jar包

  • spring-beans-4.0.5.RELEASE.jar
  • spring-aop-4.0.5.RELEASE.jar
  • spring-context-4.0.5.RELEASE.jar
  • spring-core-4.0.5.RELEASE.jar
  • spring-expression-4.0.5.RELEASE.jar
  • commons-logging-1.0.4.jar
  • junit-4.10.jar

配置包注解支持

    <!--支持注解的包-->    <context:component-scan base-package="com.spring.beans"/>

在配置包路径的时候可以灵活配置:

    <context:component-scan base-package="com.spring.*"/>

配置包注解支持之后就不用配置

   <context:annotation-config/>

因为它包含了context:annotation-config 的所有功能。

使用过滤器配置注解支持

在配置注解的时候可以配置具体哪些注解可以使用哪些注解不可以使用。

    <context:component-scan base-package="com.spring.beans">        <!--排除所有的Component注解-->        <context:exclude-filter  type="annotation"              expression="org.springframework.stereotype.Component" />        <!--包含Controller注解-->        <context:include-filter type="annotation"             expression="org.springframework.stereotype.Controller"/>    </context:component-scan>

type 有五个选项 :

  • annotation : 根据注解类型过滤,常用注解的类型有下面几个 :
    • org.springframework.stereotype.Component
    • org.springframework.stereotype.Controller
    • org.springframework.stereotype.Repository
    • org.springframework.stereotype.Service
  • assignable : 指定class或interface的全名
  • regex : 根据正则表达式匹配,使用示例 :
    <context:component-scan base-package="com.spring.beans">        <context:exclude-filter  type="regex"                         expression=".*.stereotype.Service" />    </context:component-scan>
  • aspectj : Aspectj 语法
  • custom :自定义

常用注解

  • @Component : 通用注解,可用于任何bean,如果不指定id,则默认为类名,首字母小写
  • @Controller : 通常用于控制层(MVC)
  • @Service : 通常用于服务层
  • @Repository :通常用于持久层(DAO)
  • @Scope : bean的作用域
  • @Autowired :按照类型自动装配注解,注解在属性或者setter方法上,可以设置required=false,表示不一定需要,如果需要的bean类型有多个实例就会报异常
  • @Qualifier(“xxx”)指定装配名称,和@Autowired 配合使用避免按照类型装配抛异常
  • @Resource 和@Autowired 相似,默认按照name查找,没有则按照type查找,也可以指定name、type
  • @Required : 表示初始化必须的属性,如果初始化的时候没有会报异常
  • @PostConstruct : bean 的初始化方法
  • @PreDestroy : bean 的销毁方法
  • @RequestMapping : request请求匹配路径,当request请求指定路径时将执行注解类的指定方法,注解在bean和方法之上
  • @RequestParam : request 请求参数 ,注解在处理请求的形参上

使用示例

下面是一个登陆的bean,

package com.web.controller;import javax.annotation.PostConstruct;import javax.annotation.PreDestroy;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.context.annotation.Scope;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;@Controller@RequestMapping("/controller")@Scope("prototype")public class ControllerTest {    @Autowired(required = false)    @Qualifier("jack")    private User user;    @PostConstruct    public void init()    {        System.out.println("init the controller");    }    @PreDestroy    public void destory()    {        System.out.println("destory the controller");    }    @RequestMapping("test")    public String controllerTest(            HttpServletRequest request,            HttpServletResponse response,            @RequestParam("username")String username,            @RequestParam("password")String password            )    {        if((user.username).equals(username)&&user.password.equals(password))        {            return "success";        }        else        {            return "fail";          }    }    public User getUser() {        return user;    }    public void setUser(User user) {        this.user = user;    }}
0 0
原创粉丝点击