首页 > 编程语言 > 详细

Spring框架中的注解

时间:2020-03-11 12:29:45      阅读:68      评论:0      收藏:0      [点我收藏+]

一、IOC中的注解

回顾XML中对bean的配置

<bean name="" class="" scope="" init-method="" destory-method="">
    <property name="" value=""|ref="">
        (<list>
            <value></value>
         </list>
        )
    </property>
</bean>

使用注解之前:

1、若使用注解,需要在创建XML文件时,引入context名称的约束。

2、在创建的XML文件中,加上需扫描注解的包。如com.lijie,扫描后,spring容器才会有相应的对象创建。

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

使用注解:

1、用于创建对象的

出现位置:bean的最开头

相当于bean标签的<bean name="" class="" ></bean>

  • @Component:用于把当前对象存入spring容器

  • @Controller:与Component功能一样,是对三层架构表现层的区分

  • @Service:与component功能一样,是对三层架构业务层的区分

  • @Repository:与component功能一样,是对三层架构持久层的区分

    取用时,若无备注id,则默认bean的名称,但首字母小写

    例子:

    @Component
    public class helloSpringImpl implements IhelloSpring {}
2、用于注入数据的

出现位置:在需要注入的变量或方法上。

相当于<property name="" value=""|ref="" ></property>

  • @autowrite:在需要注入的数据前添加该注解,spring自动匹配容器中的values的类型并注入。若存在多个匹配值。则匹配容器中的ID值。

  • @Qualifier:在@autowrite下方指定名称进行注入,无法单独使用。属性:value:用于指定注入Bean的ID

    @Autowired
    @Qualifier(value = "helloImpl1")//在@autowrite下方使用@qualifier指定注入对象
    Ihello helloImpl=null;
  • @Resource:直接按照Bean的id注入,可以单独使用。 属性:name,用于指定注入bean的ID

    @Resource(name = "helloImpl1")
        Ihello helloImpl=null;

上述三个注入注解(@autowrite @Qualifier @Resource)都只能注入bean类型的,无法注入String和基本类型数据,而复杂类型数据,如数组,map只能由XML文件注入

  • @value:用于注入基本类型和String类型,属性:value

3、用于设置作用范围的

相当于scope

  • @scope:用于指定bean的作用范围 属性:value,常用取值singleto(单例)\prototype(多例):默认单例

    @Component
    @Scope("prototype")
    public class helloSpringImpl implements IhelloSpring {
    @Resource(name = "helloImpl1")
4、和生命周期相关的

相当于init-method="" destory-method=""

二、AOP中的注解

基于注解的AOP

同样,在使用注解之前需同时在bean.xml文件中引入aop和注解的约束

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

注解:

  • @Aspect :表示当前类是一个切面类(存在前置、后置、异常、最终等通知的类)

  • @Pointcunt("execute( )")//注解一个切入表达式(需要增强的方法)

@Pointcut("execution(* com.lijie.service.Impl.*(..))")
public void t1(){ }
  • @Before(" "): 前置通知。参数为@Pointcut配置的切入表达式

  • @AfterReturning(" "):后置通知。参数为@Pointcut配置的切入表达式

  • @AfterThrowing(" "):异常通知。参数为@Pointcut配置的切入表达式

  • @After(" "):最终通知。参数为@Pointcut配置的切入表达式

@Aspect//表示当前类是个切面类(存在各类通知方法)
public class logger {
    @Pointcut("execution(* com.lijie.service.Impl.*(..))")
    public void t1(){ }
    @Before("t1()")//前置通知
    public void beorePrinter(){
        System.out.println("开始记录日志了--前置通知");
    }
?
    @AfterReturning("t1()")//后置通知
    public void afterPrinter(){
        System.out.println("记录完成--后置通知");
    }
?
    @AfterThrowing("t1()")//异常通知
    public void expectionPrinter(){
        System.out.println("发生异常--异常通知");
    }
?
    @After("t1()")//最终通知
    public void lastPrinter(){
        System.out.println("最终通知--最终通知");
    }

运行截图:

技术分享图片

 

 注:spring的AOP注解中,存在最终通知与后置通知执行顺序相反的BUG

 



 



Spring框架中的注解

原文:https://www.cnblogs.com/lijie-helloworld/p/12461053.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!