Spring AOPの使用はXMLに基づいています.
14986 ワード
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-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd">
<!-- aAspectj <aop:aspectj-autoproxy></aop:aspectj-autoproxy> -->
<!-- IOC -->
<bean id="giftcardService"
class="com.zghw.spring.demo.demo.aop.service.impl.GiftcardServiceImpl"></bean>
<!-- IOC -->
<bean id="giftcardAspect" class="com.zghw.spring.demo.demo.aop.aspect.GiftcardAspect"></bean>
<!-- AOP -->
<aop:config>
<!-- ref -->
<aop:aspect ref="giftcardAspect">
<!-- -->
<!-- : execution( . ( 1 , 2 ,..)) and args( 1 , 2 )
id , -->
<aop:pointcut
expression="execution(public * com.zghw.spring.demo.demo.aop.service.impl.GiftcardServiceImpl.findByCardNo(String)) and args(cardNo)"
id="pointFindByCardNo" />
<!-- arg-names , , , -->
<aop:before method="findByCardNoCacheBefore" pointcut-ref="pointFindByCardNo"
arg-names="cardNo" />
<!-- , returning , findByCardNoCache(GiftCard
retVal) , -->
<aop:after-returning method="findByCardNoCache"
returning="retVal" arg-names="cardNo,retVal" pointcut-ref="pointFindByCardNo" />
<!-- -->
<aop:after method="findByCardNoCacheAfter"
pointcut="execution(* com.zghw.spring.demo.demo.aop.*.*.findByCardNo(..))" />
<!-- -->
<aop:around method="findByCardNoCacheAround" pointcut-ref="pointFindByCardNo"
arg-names="cardNo" />
<!-- -->
<aop:around method="saveTime"
pointcut="execution(* com.zghw.spring.demo.demo.aop.*.*.save(com.zghw.spring.demo.demo.aop.pojo.Giftcard)) and args(giftcard)"
arg-names="giftcard" />
</aop:aspect>
</aop:config>
</beans>
1.ターゲットソース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 com.zghw.spring.demo.demo.aop.pojo.Giftcard;
import com.zghw.spring.demo.demo.aop.service.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;
}
}
2.うどん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.springframework.util.StopWatch;
import com.zghw.spring.demo.demo.aop.pojo.Giftcard;
import com.zghw.spring.demo.demo.aop.service.GiftcardService;
/**
*
*
* @author zghw
*
*/
public class GiftcardAspect {
// giftCard
private final Map<String, Object> cardCache = new HashMap<String, Object>();
/**
*
* @param cardNo
*/
public void findByCardNoCacheBefore(String cardNo){
System.out.println(" : :"+cardNo);
if (cardCache.containsKey(cardNo)) {
System.out.println(" "+cardNo);
}else{
System.out.println(" "+cardNo);
}
}
/**
* findByCardNo
*/
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
*/
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
*/
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
*/
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());
}
}
3.テストpackage com.zghw.spring.demo.demo.aop;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.zghw.spring.demo.demo.aop.pojo.Giftcard;
import com.zghw.spring.demo.demo.aop.service.GiftcardService;
public class ApplicationXml {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"application-aop.xml");
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 10:38:41 AM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@57d62a85: startup date [Mon Mar 28 10:38:41 CST 2016]; root of context hierarchy
Mar 28, 2016 10:38:41 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [application-aop.xml]
。。。。
StopWatch '': running time (millis) = 14
-----------------------------------------
ms % Task name
-----------------------------------------
00014 100%
。。。。
StopWatch '': running time (millis) = 53
-----------------------------------------
ms % Task name
-----------------------------------------
00053 100%
======= ==========
: :11111111
11111111
Around: : :11111111
:findByCardNo
Around: : 11111111
, 。
JoinPoint
:com.zghw.spring.demo.demo.aop.service.impl.GiftcardServiceImpl@3515a84b
: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@3515a84b
class com.sun.proxy.$Proxy2
GiftcardService == true
:Giftcard com.zghw.spring.demo.demo.aop.service.GiftcardService.findByCardNo(String)
org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint$SourceLocationImpl@6a915c8c
method-execution
execution(GiftcardService.findByCardNo(..))
========= ========
: :11111111
11111111
Around: : :11111111
Around: 11111111
, 。
JoinPoint
:com.zghw.spring.demo.demo.aop.service.impl.GiftcardServiceImpl@3515a84b
: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@3515a84b
class com.sun.proxy.$Proxy2
GiftcardService == true
:Giftcard com.zghw.spring.demo.demo.aop.service.GiftcardService.findByCardNo(String)
org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint$SourceLocationImpl@6d5321fc
method-execution
execution(GiftcardService.findByCardNo(..))
========= ========
: :11111111
11111111
Around: : :11111111
Around: 11111111
, 。
JoinPoint
:com.zghw.spring.demo.demo.aop.service.impl.GiftcardServiceImpl@3515a84b
: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@3515a84b
class com.sun.proxy.$Proxy2
GiftcardService == true
:Giftcard com.zghw.spring.demo.demo.aop.service.GiftcardService.findByCardNo(String)
org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint$SourceLocationImpl@6cdc5f76
method-execution
execution(GiftcardService.findByCardNo(..))