zoukankan      html  css  js  c++  java
  • 2021-4-12 日报博客

    个人博客

    1.学到的东西

    2. 基于 XML 的 AOP 开发

    2.1 快速入门

    ①导入 AOP 相关坐标

    ②创建目标接口和目标类(内部有切点)

    ③创建切面类(内部有增强方法)

    ④将目标类和切面类的对象创建权交给 spring

    ⑤在 applicationContext.xml 中配置织入关系

    ⑥测试代码

    ①导入 AOP 相关坐标

    <!--导入spring的context坐标,context依赖aop-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.0.5.RELEASE</version>
    </dependency>
    <!-- aspectj的织入 -->
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>1.8.13</version>
    </dependency>
    

    ②创建目标接口和目标类(内部有切点)

    public interface TargetInterface {
        public void method();
    }
    
    public class Target implements TargetInterface {
        @Override
        public void method() {
            System.out.println("Target running....");
        }
    }
    

    ③创建切面类(内部有增强方法)

    public class MyAspect {
        //前置增强方法
        public void before(){
            System.out.println("前置代码增强.....");
        }
    }
    

    ④将目标类和切面类的对象创建权交给 spring

    <!--配置目标类-->
    <bean id="target" class="com.itheima.aop.Target"></bean>
    <!--配置切面类-->
    <bean id="myAspect" class="com.itheima.aop.MyAspect"></bean>
    
    

    ⑤在 applicationContext.xml 中配置织入关系

    导入aop命名空间

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

    ⑤在 applicationContext.xml 中配置织入关系

    配置切点表达式和前置增强的织入关系

    <aop:config>
        <!--引用myAspect的Bean为切面对象-->
        <aop:aspect ref="myAspect">
            <!--配置Target的method方法执行时要进行myAspect的before方法前置增强-->
            <aop:before method="before" pointcut="execution(public void com.itheima.aop.Target.method())"></aop:before>
        </aop:aspect>
    </aop:config>
    

    ⑥测试代码

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration("classpath:applicationContext.xml")
    public class AopTest {
        @Autowired
        private TargetInterface target;
        @Test
        public void test1(){
            target.method();
        }
    }
    

    ⑦测试结果

    2.明日计划

    XML 配置 AOP

    3.遇到的问题

  • 相关阅读:
    POJ3693 Maximum repetition substring —— 后缀数组 重复次数最多的连续重复子串
    SPOJ
    POJ2774 Long Long Message —— 后缀数组 两字符串的最长公共子串
    POJ3261 Milk Patterns —— 后缀数组 出现k次且可重叠的最长子串
    POJ1743 Musical Theme —— 后缀数组 重复出现且不重叠的最长子串
    SPOJ
    AC自动机小结
    HDU3247 Resource Archiver —— AC自动机 + BFS最短路 + 状压DP
    POJ1625 Censored! —— AC自动机 + DP + 大数
    Herding
  • 原文地址:https://www.cnblogs.com/gongyunlong-blogs/p/14910614.html
Copyright © 2011-2022 走看看