Spring AOPの使用は全注釈AsppectJに基づいています.
12430 ワード
1.うどん
package com.zghw.spring.demo.demo.aop.aspect;
import java.util.HashMap;
import java.util.Map;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch;
import com.zghw.spring.demo.demo.aop.pojo.Giftcard;
import com.zghw.spring.demo.demo.aop.service.GiftcardService;
/**
*
*
* @author zghw
*
*/
@Aspect
@Component
public class GiftcardAspect {
// giftCard
private final Map<String, Object> cardCache = new HashMap<String, Object>();
@Pointcut("execution(public * com.zghw.spring.demo.demo.aop.service.impl.GiftcardServiceImpl.findByCardNo(String)) && args(cardNo)")
public void findByCardNo(String cardNo) {
}
/**
*
*
* @param cardNo
*/
@Before(value = "findByCardNo(cardNo)")
public void findByCardNoCacheBefore(String cardNo) {
System.out.println(" : :" + cardNo);
if (cardCache.containsKey(cardNo)) {
System.out.println(" " + cardNo);
} else {
System.out.println(" " + cardNo);
}
}
/**
* findByCardNo
*/
// @AfterReturning(pointcut="findByCardNo(cardNo)",returning="retVal")
//
@AfterReturning(value = "execution(public * com.zghw.spring.demo.demo.aop.service.impl.GiftcardServiceImpl.findByCardNo(String)) && args(cardNo)", argNames = "cardNo,retVal", returning = "retVal")
public Giftcard findByCardNoCache(String cardNo, Giftcard retVal) {
if (!cardCache.containsKey(cardNo)) {
System.out.println(" : " + cardNo);
cardCache.put(retVal.getCardNo(), retVal);
}
return retVal;
}
/**
* JoinPoint , 。
*
* @param joinPoint
*/
@After(value = "execution(* com.zghw.spring.demo.demo.aop.*.*.findByCardNo(..))")
public void findByCardNoCacheAfter(JoinPoint joinPoint) {
System.out.println(" , 。");
System.out.println(" JoinPoint ");
Object target = joinPoint.getTarget();
System.out.println(" :" + target);
System.out.println(" :" + target.getClass());
if (target instanceof GiftcardService) {
GiftcardService gs = (GiftcardService) target;
//
System.out.println(gs.findByCardNo("2222222"));
}
Object[] args = joinPoint.getArgs();
System.out.println(" : " + args.length);
int i = 0;
for (Object arg : args) {
System.out.println(" " + (++i) + " : " + arg.getClass());
}
Object proxy = joinPoint.getThis();
System.out.println(" " + proxy);
System.out.println(" " + proxy.getClass());
System.out.println(" GiftcardService == "
+ (proxy instanceof GiftcardService));
System.out.println(" :" + joinPoint.getSignature());
System.out.println(joinPoint.getSourceLocation());
System.out.println(joinPoint.getKind());
System.out.println(joinPoint.getStaticPart().toShortString());
}
/**
* , , 。
* , 。 。
*
* @param giftcard
* @throws Throwable
*/
@Around(value = "findByCardNo(cardNo)")
public Giftcard findByCardNoCacheAround(ProceedingJoinPoint pjp,
String cardNo) throws Throwable {
Giftcard card = null;
System.out.println("Around: : :" + cardNo);
if (cardCache.containsKey(cardNo)) {
System.out.println("Around: " + cardNo);
//
return (Giftcard) cardCache.get(cardNo);
}
card = (Giftcard) pjp.proceed();// 。
if (!cardCache.containsKey(card.getCardNo())) {
System.out.println("Around: : " + cardNo);
cardCache.put(card.getCardNo(), card);
}
return card;
}
/**
*
*
* @param pjp
* @param giftcard
*/
@Around(value = "execution(* com.zghw.spring.demo.demo.aop.*.*.save(com.zghw.spring.demo.demo.aop.pojo.Giftcard)) and args(giftcard)", argNames = "giftcard")
public void saveTime(ProceedingJoinPoint pjp, Giftcard giftcard) {
StopWatch sw = new StopWatch();
sw.start(giftcard.getDescription());
try {
pjp.proceed();
} catch (Throwable e) {
e.printStackTrace();
}
sw.stop();
System.out.println(sw.prettyPrint());
}
}
2.目標クラスpackage com.zghw.spring.demo.demo.aop.pojo;
public class Giftcard {
private String name;
private String cardNo;
private double price;
private String description;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCardNo() {
return cardNo;
}
public void setCardNo(String cardNo) {
this.cardNo = cardNo;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Override
public String toString() {
return "Giftcard [name=" + name + ", cardNo=" + cardNo + ", price="
+ price + ", description=" + description + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((cardNo == null) ? 0 : cardNo.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Giftcard other = (Giftcard) obj;
if (cardNo == null) {
if (other.cardNo != null)
return false;
}
return true;
}
}
package com.zghw.spring.demo.demo.aop.service;
import com.zghw.spring.demo.demo.aop.pojo.Giftcard;
public interface GiftcardService {
public void save(Giftcard giftcard);
public Giftcard findByCardNo(String cardNo);
public Giftcard update(Giftcard giftcard);
}
package com.zghw.spring.demo.demo.aop.service.impl;
import java.util.HashMap;
import java.util.Map;
import org.springframework.stereotype.Component;
import com.zghw.spring.demo.demo.aop.pojo.Giftcard;
import com.zghw.spring.demo.demo.aop.service.GiftcardService;
@Component("giftcardService")
public class GiftcardServiceImpl implements GiftcardService {
private final Map<String, Giftcard> giftcards = new HashMap<String, Giftcard>();
public void save(Giftcard giftcard) {
System.out.println(" 。。。。");
giftcards.put(giftcard.getCardNo(), giftcard);
try {
//
Thread.sleep((int)(Math.random()*100));
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
};
System.out.println(" ");
}
public Giftcard findByCardNo(String cardNo) {
Giftcard giftcard = giftcards.get(cardNo);
System.out.println(" :findByCardNo");
return giftcard;
}
public Giftcard update(Giftcard giftcard) {
Giftcard card = giftcards.put(giftcard.getCardNo(), giftcard);
return card;
}
}
3.試験類package com.zghw.spring.demo.demo.aop;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import com.zghw.spring.demo.demo.aop.pojo.Giftcard;
import com.zghw.spring.demo.demo.aop.service.GiftcardService;
@Configuration
@ComponentScan
// AspectJ
@EnableAspectJAutoProxy
public class ApplicationAnnotation {
public static void main(String[] args) {
ApplicationContext ctx =new AnnotationConfigApplicationContext(ApplicationAnnotation.class);
GiftcardService giftcardService = (GiftcardService) ctx
.getBean("giftcardService");
//
Giftcard card = new Giftcard();
card.setCardNo("11111111");
card.setDescription(" ");
card.setName("zghw");
card.setPrice(1322546.45);
//
giftcardService.save(card);
Giftcard card2 = new Giftcard();
card2.setCardNo("2222222");
card2.setDescription(" ");
card2.setName("dfsdf");
card2.setPrice(467732346.45);
//
giftcardService.save(card2);
//
System.out.println("======= ==========");
giftcardService.findByCardNo("11111111");
System.out.println("========= ========");
giftcardService.findByCardNo("11111111");
System.out.println("========= ========");
giftcardService.findByCardNo("11111111");
}
}
4.出力結果Mar 28, 2016 11:37:24 AM org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@3316f2e6: startup date [Mon Mar 28 11:37:24 CST 2016]; root of context hierarchy
。。。。
StopWatch '': running time (millis) = 63
-----------------------------------------
ms % Task name
-----------------------------------------
00063 100%
。。。。
StopWatch '': running time (millis) = 73
-----------------------------------------
ms % Task name
-----------------------------------------
00073 100%
======= ==========
Around: : :11111111
: :11111111
11111111
:findByCardNo
Around: : 11111111
, 。
JoinPoint
:com.zghw.spring.demo.demo.aop.service.impl.GiftcardServiceImpl@62214c6f
:class com.zghw.spring.demo.demo.aop.service.impl.GiftcardServiceImpl
:findByCardNo
Giftcard [name=dfsdf, cardNo=2222222, price=4.6773234645E8, description= ]
: 1
1 : class java.lang.String
com.zghw.spring.demo.demo.aop.service.impl.GiftcardServiceImpl@62214c6f
class com.sun.proxy.$Proxy16
GiftcardService == true
:Giftcard com.zghw.spring.demo.demo.aop.service.GiftcardService.findByCardNo(String)
org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint$SourceLocationImpl@36db33f6
method-execution
execution(GiftcardService.findByCardNo(..))
========= ========
Around: : :11111111
Around: 11111111
, 。
JoinPoint
:com.zghw.spring.demo.demo.aop.service.impl.GiftcardServiceImpl@62214c6f
:class com.zghw.spring.demo.demo.aop.service.impl.GiftcardServiceImpl
:findByCardNo
Giftcard [name=dfsdf, cardNo=2222222, price=4.6773234645E8, description= ]
: 1
1 : class java.lang.String
com.zghw.spring.demo.demo.aop.service.impl.GiftcardServiceImpl@62214c6f
class com.sun.proxy.$Proxy16
GiftcardService == true
:Giftcard com.zghw.spring.demo.demo.aop.service.GiftcardService.findByCardNo(String)
org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint$SourceLocationImpl@7130779c
method-execution
execution(GiftcardService.findByCardNo(..))
========= ========
Around: : :11111111
Around: 11111111
, 。
JoinPoint
:com.zghw.spring.demo.demo.aop.service.impl.GiftcardServiceImpl@62214c6f
:class com.zghw.spring.demo.demo.aop.service.impl.GiftcardServiceImpl
:findByCardNo
Giftcard [name=dfsdf, cardNo=2222222, price=4.6773234645E8, description= ]
: 1
1 : class java.lang.String
com.zghw.spring.demo.demo.aop.service.impl.GiftcardServiceImpl@62214c6f
class com.sun.proxy.$Proxy16
GiftcardService == true
:Giftcard com.zghw.spring.demo.demo.aop.service.GiftcardService.findByCardNo(String)
org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint$SourceLocationImpl@24fcdcd2
method-execution
execution(GiftcardService.findByCardNo(..))