首页 > 其他分享 >spring aop 注解方式

spring aop 注解方式

时间:2023-04-26 22:02:51浏览次数:41  
标签:spring void aop aspectj CLAP org import public 注解


前置、后置、环绕、切面、切点


package com.springinaction.springidol;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
public class Audience {
	@Pointcut("execution(* com.springinaction.springidol.Performer.perform(..))")
	public void performance() {
	}

	@Before("performance()")
	public void takeSeats() {
		System.out.println("The audience is taking their seats.");
	}

	@Before("performance()")
	public void turnOffCellPhones() {
		System.out.println("The audience is turning off their cellphones");
	}

	@AfterReturning("performance()")
	public void applaud() {
		System.out.println("CLAP CLAP CLAP CLAP CLAP");
	}

	@AfterThrowing("performance()")
	public void demandRefund() {
		System.out.println("Boo! We want our money back!");
	}
	
	@Around("performance()")
	public void watchPerformance(ProceedingJoinPoint joinpoint) {
		try {
			long start = System.currentTimeMillis();
			joinpoint.proceed();
			long end = System.currentTimeMillis();
			System.out.println("The performance took " + (end - start) + " milliseconds.");
		} catch (Throwable t) {
			System.out.println("Boo! We want our money back!");
		}
	}
}



引入新方法


package com.springinaction.springidol;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.DeclareParents;

@Aspect
public class ContestantIntroducer {

	@DeclareParents(value = "com.springinaction.springidol.Performer+", defaultImpl = GraciousContestant.class)
	public static Contestant contestant;
}



package com.springinaction.springidol;

public interface Contestant {
	void receiveAward();
}



package com.springinaction.springidol;

public class GraciousContestant implements Contestant {

	public void receiveAward() {
		System.out.println("Why, thank you all very much!");
	}
}



XML配置

spring-idol.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!--<start id="preamble" /> -->
<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">
	<!--<end id="preamble" /> -->

	<bean id="eddie" class="com.springinaction.springidol.Instrumentalist">
		<property name="instrument">
			<bean class="com.springinaction.springidol.Guitar" />
		</property>
	</bean>

	<!--<start id="audience_bean" /> -->
	<bean id="audience" class="com.springinaction.springidol.Audience" />
	<!--<end id="audience_bean" /> -->

	<!--<start id="contestant_introducer" /> -->
	<bean class="com.springinaction.springidol.ContestantIntroducer" />
	<!--<end id="contestant_introducer" /> -->
	
	<bean id="volunteer" class="com.springinaction.springidol.Volunteer" />
	<bean id="magician" class="com.springinaction.springidol.Magician" />

	<!--<start id="aspectj_autoproxy" /> -->
	<aop:aspectj-autoproxy />
	<!--<end id="aspectj_autoproxy" /> -->

</beans>


注意:

<aop:aspectj-autoproxy />




测试类:

package com.springinaction.springidol;

import static org.junit.Assert.assertEquals;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("spring-idol.xml")
public class AspectTest {
	@Autowired
	ApplicationContext context;
	
	@Autowired
	Performer performer;

	@Test
	public void audienceShouldApplaud() throws Exception {
		performer.perform();
	}
	
	Contestant contestant;

	@Test
	public void eddieShouldBeAContestant() {
		contestant = (Contestant)performer;
		contestant.receiveAward();
	}
	
	@Autowired
	MindReader magician;

	@Autowired
	Thinker volunteer;
	
	@Test
	public void magicianShouldReadVolunteersMind() {
		volunteer.thinkOfSomething("Queen of Hearts");

		assertEquals("Queen of Hearts", magician.getThoughts());
	}
}



运行audienceShouldApplaud:

The audience is turning off their cellphones
Strum strum strum
CLAP CLAP CLAP CLAP CLAP
The performance took 0 milliseconds.



运行eddieShouldBeAContestant:

Why, thank you all very much!



运行magicianShouldReadVolunteersMind:

Intercepting volunteer's thoughts : Queen of Hearts





标签:spring,void,aop,aspectj,CLAP,org,import,public,注解
From: https://blog.51cto.com/u_1002776/6229003

相关文章

  • spring aop xml方式
    工程如图:pom文件内容:<?xmlversion="1.0"encoding="UTF-8"?><projectxmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/maven-v4_0_0.xsd" xmlns......
  • spring jdbc 声明式事务
    Spring的声明式事务顾名思义就是采用声明的方式来处理事务。这里所说的声明,就是指在配置文件中申明。用在Spring配置文件中声明式的处理事务来代替代码式的处理事务。这样的好处是,事务管理不侵入开发的组件,具体来说,业务逻辑对象就不会意识到正在事务管理之中,事实上也应该如此,因为事......
  • SpringSecurity从入门到精通:认证成功处理器&认证失败处理器
    认证成功处理器  认证失败处理器  ......
  • [转]前端传嵌套对象参数给spring mvc
    在使用springmvc开发web应用时,感觉springmvc的controller方法能自动将参数注入到方法的参数对象中,极大的方便了开发。但是,在遇到有嵌套对象的时候,比如订单对象有个属性是用户对象,就不好处理了。一种情况是,传递的参数都是作为post方法的请求体,我们可以用RequestBody注解。但是当条......
  • SpringSecurity从入门到精通:其他权限校验方法&自定义权限校验方法
    其他权限校验方法我们前面都是使用@PreAuthorize注解,然后在在其中使用的是hasAuthority方法进行校验。SpringSecurity还为我们提供了其它方法例如:hasAnyAuthority,hasRole,hasAnyRole等。​这里我们先不急着去介绍这些方法,我们先去理解hasAuthority的原理,然后再去学......
  • 5.spring 中的 bean 是线程安全的吗?
    https://www.mianshigee.com/question/10477vxbhttps://www.zhihu.com/pin/1365332853371297792  ......
  • 4.Spring中IOC创建对象的两种方式和创建时机
    https://blog.csdn.net/jike11231/article/details/116357864 创建时机1:默认的在Spring容器启动的时候2:在getBean的时候创建对象(就是拿到配置文件中的标识符的时候创建对象)在Spring的配置文件bean中有一个属性lazy-init=“default/true/false”(1)如果lazy-init为"default/f......
  • spring heapdump泄露 heapdump_tool.jar
    https://github.com/wyzxxz/heapdump_tool heapdump_tool声明:此工具仅用于企业安全人员自查验证自身企业资产的安全风险,或有合法授权的安全测试,请勿用于其他用途,如有,后果自负。下载地址:https://toolaffix.oss-cn-beijing.aliyuncs.com/wyzxxz/20230425/heapdump_tool.jar+20......
  • SpringSecurity从入门到精通:从数据库查询权限信息&自定义失败处理
    从数据库查询权限信息      记得打开redis      自定义失败处理我们还希望在认证失败或者是授权失败的情况下也能和我们的接口一样返回相同结构的json,这样可以让前端能对响应进行统一的处理。要实现这个功能我们需要知道SpringSecurity......
  • springcloud OpenFeign的使用
    在这里只记录OpenFeign关键的使用步骤,A、B两个项目是基于springboot、springcloud实现的,并且在nacos中有服务注册。第一步:导入依赖<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</a......