基于注解(annotation)的aop代理

1、创建annotation的模块 添加依赖

1.1、在parent.pom文件中添加依赖
 <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>${spring.version}</version>
  </dependency>

 <dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjrt</artifactId>
    <version>${aspectj.version}</version>
  </dependency>

  <dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>${aspectj.version}</version>
  </dependency>
1.2、在annotation.pom文件中添加
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
    </dependency>

    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
    </dependency>

    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
    </dependency>

2、创建实体类

2.1、创建实体类

package com.mu.aop.annotation.domain;

public class Student {
private Integer id;
private String name;

public Integer getId() {
    return id;
}

public void setId(Integer id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

}

3、创建dao数据访问层、service业务逻辑层、controller控制器、aspect切面

3.1、dao层

package com.mu.aop.annotation.dao;

import com.mu.aop.annotation.domain.Student; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Repository; //<bean id="studentDao" class="com.mu.aop.annotation.service.StudentDao" />

[@Repository](https://my.oschina.net/u/3055569)
@Scope("singleton")
public class StudentDao {
    public void persist(Student s ){
        System.out.println("保存{id: "+s.getId()+", name:"+s.getName()+"}");
    }
}
3.2、service业务逻辑层

package com.mu.aop.annotation.service;

import com.mu.aop.annotation.dao.StudentDao; import com.mu.aop.annotation.domain.Student; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service;

//<bean id="studentService" class="com.mu.aop.annotation.service.StudentService" />

@Service("studentService")
public class StudentService {
	@Autowired
	private StudentDao studentDao=null;
	public void save( Student s){
		studentDao.persist(s);
	}

	public StudentDao getStudentDao() {
		return studentDao;
	}

	public void setStudentDao(StudentDao studentDao) {
		System.out.println("Service------>Dao");
		this.studentDao = studentDao;
	}
}
3.3、controller控制器

package com.mu.aop.annotation.controller;

import com.mu.aop.annotation.domain.Student; import com.mu.aop.annotation.service.StudentService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Controller;

//<bean id="studentController" class="com.mu.aop.annotation.service.StudentController" scope="singleton" />

[@Controller](https://my.oschina.net/u/1774615)
public class StudentController {
	private StudentService studentService=null;
	public void  signUp(Student s){
		studentService.save(s);
	}

	public StudentService getStudentService() {
		return studentService;
	}
@Autowired
@Qualifier("studentService" )
	public void setStudentService(StudentService studentService) {
		System.out.println("Controller---->Service");
		this.studentService = studentService;
	}
}
3.4、aspect切面

package com.mu.aop.annotation.aspect;

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

@Component
@Aspect
public class ServiceAspect {

	@Pointcut( "execution(* com..service.*.save*(..))" )
	private void servicePointcut(){
	}

	@Before( "servicePointcut()" )
	public void before( JoinPoint joinPoint ) {
		System.out.println( "【 " + joinPoint.getSignature().getName() + " 】方法即将执行" );
	}

	@Around( "servicePointcut()" )
	public Object around( ProceedingJoinPoint proceedingJoinPoint ) throws Throwable {

		String  name = proceedingJoinPoint.getSignature().getName();
		System.out.println( "开始为" + name + "计时" );
		long begin = System.nanoTime();

		Object result = proceedingJoinPoint.proceed();

		long end = System.nanoTime();
		System.out.print( "为" + name + "计时结束," );
		System.out.println( "[ " + name + " ]执行耗时 [ " + ( end - begin ) + "ns, ]" );

		return  result ;
	}

	@AfterReturning( pointcut = "servicePointcut()" , returning = "xxx")
	public void afterReturn( JoinPoint joinPoint , Object xxx ) {
		System.out.println("【 " + joinPoint.getSignature().getName() + " 】方法执行后返回了 【 " + xxx +" 】");
	}

	@AfterThrowing( pointcut = "servicePointcut()" , throwing = "ex")
	public void afterThrow( JoinPoint joinPoint , Throwable ex ) {
		System.out.println("【 " + joinPoint.getSignature().getName() + " 】方法执行执行时抛出 【 " + ex +" 】");
	}

	@After( "servicePointcut()" )
	public void after( JoinPoint joinPoint ) {
		System.out.println( "【 " + joinPoint.getSignature().getName() + " 】方法执行结束" );
	}

}

4、配置Spring的文件

4.1、创建annotationBased.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">

<!--启动注解-->
<context:annotation-config />
<!-- 扫描 dao 包中所有的类  -->
<context:component-scan base-package="com.mu.aop.annotation.dao"/>
<!-- 扫描 service包中所有的类  -->
<context:component-scan base-package="com.mu.aop.annotation.service"/>
<!-- 扫描 controller 包中所有的类  -->
<context:component-scan base-package="com.mu.aop.annotation.controller"/>


<!-- 扫描aspect包中的所有类-->
<context:component-scan base-package="com.mu.aop.annotation.aspect"/>

<!-- 告知容器需要为 注解 提供 自动代理支持  -->
<aop:aspectj-autoproxy/>
</beans>

5、测试

5.1、创建Annotation测试类

package com.mu.aop.annotation;

import com.mu.aop.annotation.controller.StudentController; import com.mu.aop.annotation.dao.StudentDao; import com.mu.aop.annotation.domain.Student; import com.mu.aop.annotation.service.StudentService; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AopAnnotationTest {
	public static void main(String[] args) {
		String path = "classpath*:com/mu/aop/annotation/aop-annotation.xml";
		AbstractApplicationContext iocContext= new ClassPathXmlApplicationContext(path);

		StudentService studentService= iocContext.getBean("studentService", StudentService.class);
		System.out.println(studentService);
		Student s=new Student();
		s.setId(123);
		s.setName("杨过");
	   studentService.save(s);

		iocContext.close();
	}
}

转载于:https://my.oschina.net/u/4045827/blog/2987736

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值