zoukankan      html  css  js  c++  java
  • 03、spring Aop注解入门配置

    AOP概念:spring aop称为切面编程,通过编译方式和运行期动态代理实现程序功能应用的一种技术;其目的是降低程序间部件的耦合度,提高可复用性。

    1、导入依赖

    <dependencies>
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.3.7</version>
    </dependency>
    <dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.9.6</version>
    </dependency>
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>5.3.7</version>
    </dependency>
    <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>
    </dependency>
    </dependencies>

    2、创建连接点服务层业务

    服务接口:
    public interface Myservice {
    public abstract void save();
    }

    服务实现类
    @Service
    public class MyServiceImpl implements MyService {

    @Override
    public void save() {
    System.out.println("保存业务");
    }

    }
    
    
    3、创建增强类
    @Component("logger")
    @Aspect
    public class Logger {

    @Pointcut("execution(* com.boat.service..*.*(..))")
    private void pintcut(){ }

    @Before("pintcut()")
    public void befoerLog(){
    System.out.println("前置通知....");
    }

    @AfterReturning("pintcut()")
    public void afterReturningLog(){
    System.out.println("后置通知....");
    }

    @AfterThrowing("pintcut()")
    public void afterThrowing(){
    System.out.println("异常通知....");
    }

    @After("pintcut()")
    public void afterLog(){
    System.out.println("最终通知....");
    }
    }

    4、配置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"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    https://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    https://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/aop
    https://www.springframework.org/schema/aop/spring-aop.xsd">

    <!--启用注解扫描器配置-->
    <context:component-scan base-package="com.boat"></context:component-scan>

    <!--启用aop注解自动配置-->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>

    </beans>

    5、编写测试类

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = "classpath:service.xml")
    public class TestSpring {

    @Autowired()
    private MyService myService;

    @Test
    public void sho(){
    myService.save();
    }

    }
    运行结果为:

     





  • 相关阅读:
    算法训练 数位分离
    算法训练 薪水计算
    算法训练 整除问题
    算法训练 数对
    pom.xml一个简单配置
    MyBatis3.4.0以上的分页插件错误:Could not find method on interface org.apache.ibatis.executor.statement.StatementHandler named prepare. Cause: java.lang.NoSuchMethodException: org.apache.ibatis.executor.stateme
    MyBatis3-实现MyBatis分页
    mybatis 易百练习笔记
    maven pom.xml配置
    Description Resource Path Location Type The superclass "javax.servlet.http.HttpServlet" was not foun
  • 原文地址:https://www.cnblogs.com/M87-A/p/14809963.html
Copyright © 2011-2022 走看看