注解实现AOP/IOC

本文详细介绍了如何在Spring框架中使用AOP注解实现切面编程,包括前置通知、最终通知、后置通知和异常通知,同时展示了如何结合IoC注解进行依赖注入。通过实例演示了`@Aspect`, `@Before`, `@After`, `@Around`等注解的使用,并涉及`@Component`和`@Autowired`在组件扫描中的应用。
摘要由CSDN通过智能技术生成

Annotation注解

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"
       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">

    <bean id="userDao" class="com.atstudy.dao.impl.UserDaoImpl"/>

    <bean id="userService" class="com.atstudy.service.impl.UserServiceImpl">
        <property name="userDao" ref="userDao"></property>
    </bean>

    <bean id="controller" class="com.atstudy.controller.UserController">
        <property name="userService" ref="userService"></property>
    </bean>

    <bean id="log" class="com.atstudy.log.Log"></bean>

    <!--使用Spring的annotation完成AOP-->
    <aop:aspectj-autoproxy/>

</beans>

 Log.java

package com.atstudy.log;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;

/**
 * @Aspect使用Spring的annotation完成AOP
 */
@Aspect
public class Log
{

    /**
     * 用于前置通知的方法
     */
    @Before("execution(* com.atstudy.controller.*.*(..))")
    public void before()
    {
        System.out.println("方法调用了...");
    }

    /**
     * 用于最终通知的方法
     */
    @After("execution(* com.atstudy.controller.*.*(..))")
    public void after()
    {
        System.out.println("方法执行完了...");
    }

    /**
     * 用于后置通知
     * 此处的String value可以获取有返回值方法的返回值,此处的参数类型要和切入点
     * 方法返回值的类型一致
     */
    @AfterReturning(pointcut = "execution(* com.atstudy.controller.*.*(..))",returning = "value")
    public void afterReturning(String value)
    {
        System.out.println("后置通知..."+value);
    }

    /**
     * 用于异常通知---只有出现异常,异常通知才会执行
     * @param throwValue 该属性接收的是一个异常对象
     */
    @AfterThrowing(pointcut = "execution(* com.atstudy.controller.*.*(..))",throwing = "throwValue")
    public  void  afterThrowing(Exception throwValue)
    {
        System.out.println("异常通知..."+throwValue);
    }

    /**
     * 环绕通知
     * @param pjp
     * @return
     * @throws Throwable
     */
    @Around("execution(* com.atstudy.controller.*.*(..))")
    public Object around(ProceedingJoinPoint pjp)throws  Throwable
    {
        System.out.println("环绕前---");
        System.out.println(pjp.getSignature()+"方法被调用了");
        //执行目标方法
        Object result = pjp.proceed();
        System.out.println("环绕后---");
        return result;
    }

}

IOC的注解实现

<?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">

    <!--在xml中配置指定使用Annotation方式完成IOC/DI-->
    <context:annotation-config/>

    <!--使用Spring的annotation完成AOP-->
    <aop:aspectj-autoproxy/>

    <!--指定从哪个包开始搜索/扫描IOC/DI的Annotation-->
    <context:component-scan base-package="com.atstudy"/>

</beans>

Log.java

package com.atstudy.log;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

/**
 * 不使用接口完成AOP,很大概率Spring使用的是cglib动态代理
 * @Aspect使用Spring的annotation完成AOP
 * @Component该注解等价于spring.xml中的<bean></bean>标签
 */
@Component
@Aspect
public class Log
{

    /**
     * 用于前置通知的方法
     */
    @Before("execution(* com.atstudy.controller.*.*(..))")
    public void before()
    {
        System.out.println("方法调用了...");
    }

    /**
     * 用于最终通知的方法
     */
    @After("execution(* com.atstudy.controller.*.*(..))")
    public void after()
    {
        System.out.println("方法执行完了...");
    }

    /**
     * 用于后置通知
     * 此处的String value可以获取有返回值方法的返回值,此处的参数类型要和切入点
     * 方法返回值的类型一致
     */
    @AfterReturning(pointcut = "execution(* com.atstudy.controller.*.*(..))",returning = "value")
    public void afterReturning(String value)
    {
        System.out.println("后置通知..."+value);
    }

    /**
     * 用于异常通知---只有出现异常,异常通知才会执行
     * @param throwValue 该属性接收的是一个异常对象
     */
    @AfterThrowing(pointcut = "execution(* com.atstudy.controller.*.*(..))",throwing = "throwValue")
    public  void  afterThrowing(Exception throwValue)
    {
        System.out.println("异常通知..."+throwValue);
    }

    /**
     * 环绕通知
     * @param pjp
     * @return
     * @throws Throwable
     */
    @Around("execution(* com.atstudy.controller.*.*(..))")
    public Object around(ProceedingJoinPoint pjp)throws  Throwable
    {
        System.out.println("环绕前---");
        System.out.println(pjp.getSignature()+"方法被调用了");
        //执行目标方法
        Object result = pjp.proceed();
        System.out.println("环绕后---");
        return result;
    }

}

UserDaoImpl.java

package com.atstudy.dao.impl;

import com.atstudy.dao.UserDao;
import org.springframework.stereotype.Component;

/**
 * @Component("userDao")此处的userDao相当于spring.xml中bean的id或name属性
 */
@Component("userDao")
public class UserDaoImpl  implements UserDao
{
    @Override
    public void insert() {
        System.out.println("insert()添加信息成功...");
    }

    @Override
    public void delete() {
        System.out.println("delete()删除信息成功...");
    }

    @Override
    public void query() {
        System.out.println("query()查询信息成功...");
    }

    @Override
    public void update() {
        System.out.println("update()修改/更新信息成功...");
    }
}

UserServiceImpl.java

package com.atstudy.service.impl;

import com.atstudy.dao.UserDao;
import com.atstudy.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component("userService")
public class UserServiceImpl implements UserService
{
    /*
    * 自动注入---根据@Component("userDao")中的id的字符串是userDao的查找,
    * 如果不指定userDao的字符串ID则按照类型去查找,如果此时有多个相同类型的
    * @Component则会出现异常,此时必须使用id字符串指定
    * 默认情况下是按照类型注入,如果出现两个类型相同的,就会根据@Component指定的id注入
    * */
    @Autowired
    private UserDao userDao;

    @Override
    public void insert() {
        userDao.insert();
    }

    @Override
    public void delete() {
        userDao.delete();
    }

    @Override
    public void query() {
        userDao.query();
    }

    @Override
    public void update() {
        userDao.update();
    }
}

UserController.java

package com.atstudy.controller;

import com.atstudy.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component("controller")
public class UserController
{
    @Autowired
    private UserService   userService;


    public  void insert()
    {
        userService.insert();
    }

    /**
     * 出现异常的方法
     */
    public  void  delete()
    {
        userService.delete();
        int a = 6;
        int b = 0;
        int c = a/b;
    }

    public  void  query()
    {
        userService.query();
    }

    public  String  update()
    {
        userService.update();

        return  "update()返回的值---";
    }


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一剑封喉の

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值