Spring04

使用注解的方式进行代理

步骤1:在com.hello.dao包下新建一个CarDao接口以及他的实现类CarDaoImpl,具体代码如下:

CarDao接口:

package com.hello.dao;

public interface CarDao {

    public void play();

}

CarDaoImpl实现类:

package com.hello.dao;

@Component("CarDao")     //注解

public class CarDaoImpl implements CarDao {

        @Override

        public void play() {

                System.out.print("我能跑120km/h");

        }

}

步骤2:新建一个com.hello.plus包并在包下新建一个名为CarPlus的类

package com.hello.plus;

import org.aspectj.lang.annotation.After;

import org.aspectj.lang.annotation.Aspect;

@Aspect    //注解

public class CarPlus {

@After(value="execution(* *..*.*Impl.play(..))")        //注解

public void carplus(){

System.out.println("我能飞上天");

}

}

这上面两个地方使用了注解

步骤3:在src下新建一个xml文件applicationContext.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

        http://www.springframework.org/schema/beans/spring-beans.xsd

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

<!-- 加载自动代理 -->

<aop:aspectj-autoproxy/>

<!-- 注解扫描 -->

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

<!-- 将 MyPlus 类交给 Spring 管理 -->

<bean id="CarPlus" class="com.hello.plus.CarPlus"></bean>

<!-- 切面 = 切入点 + 通知

表达式优化:

1)省略 public

2)返回值,不能省略,但可以使用 * 占位

3)如果中间的路径太多的话,则可以使用 *..*

4)如果有多个 类/方法 的 前/后缀 是相同的话,也可以使用 前/后缀 作为过滤条件

5)如果方法中有0 - 多个参数(不确定个数的参数),则可以直接使用两个点表示

-->

<!-- <aop:config>

<aop:aspect ref="carPlus">

<aop:after method="carplus" pointcut="execution(* *..*.*Impl.play(..))"/>

</aop:aspect>

</aop:config> -->

</beans>

步骤4:接下来就是测试了,新建一个测试类TestClass

package com.hello.test;

import org.junit.Test;

import org.junit.runner.RunWith;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.test.context.ContextConfiguration;

import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.hello.dao.CarDao;

//执行xml文件

@ContextConfiguration("classpath:applicationContext.xml")

@RunWith(SpringJUnit4ClassRunner.class)

public class TestClass {

@Autowired

private CarDao cd;

@Test

public void test(){

    cd.play();

}

}

执行applicationContext.xml之后就能通过注解的方式进行代理加强了

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,001评论 19 139
  • 通过之前的两篇我们能在本地搭建单一和集群两种方式的dubbo服务,这篇我们来看 springmvc+spring+...
    安琪拉_4b7e阅读 2,203评论 0 6
  • ©著作权归作者所有:来自51CTO博客作者优秀android的原创作品,如需转载,请注明出处,否则将追究法律责任 ...
    传奇内服号阅读 1,115评论 0 9
  • 对于java中的思考的方向,1必须要看前端的页面,对于前端的页面基本的逻辑,如果能理解最好,不理解也要知道几点。 ...
    神尤鲁道夫阅读 842评论 0 0
  • *Html参考手册 一、HTML中的一些标签 1、select:中的每一项用option表示。如果size属性设置...
    liujf阅读 377评论 0 9