Sping--AOP--Annotation

Aspectj 概念:java

1. joinpoint:切入點, 好比@Before, @After, @Aroundnode

2. Pointcut:切入點集合, 好比 @Pointcut("execution(public * com.bjsxt.service..*.*(..))")spring

  public void myMethod(){};  //切入點集合的名字this

3. Aspect:切面邏輯, 切面類, 好比LogInterceptor以前加入的@Aspectspa

4. Advice: 加入點的建議, 相似於Aspect的邏輯, 至關於@Before, 加在切入點的建議.代理

5. Target: 被代理對象, 邏輯織入哪裏...component

6. Weave: 織入xml

AOP的Annotation方式:對象

1. 加上對應的xsd文件sprin-aop.xsdblog

2. beans.xml <aop:aspectj-autoproxy/>

3. 此時就能夠解析對應的annotation了

4. 創建咱們的攔截類

5. 用@Aspect註解這個類

6. 用@Before來註解方法

7. 寫明白切入點(execution...)

8. 讓spring對咱們的攔截器類進行管理 @Component

 

常見的annotation:

1. @Pointcut

2. @Before

3. @AfterReturning

4. @AfterThrowing

5. @After

6. @Around

 

beans.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-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
	<context:annotation-config />
	<context:component-scan base-package="com.bjsxt"/>
 	<aop:aspectj-autoproxy />
</beans>

LogInterceptor.java: 注意@Component

package com.bjsxt.aop;

import org.aspectj.lang.ProceedingJoinPoint;
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;

@Aspect
@Component
public class LogInterceptor {	
	@Before("execution(public * com.bjsxt.dao..*.*(..)))")
	public void before() {
		System.out.println("method before");
	}
	@AfterReturning("execution(public * com.bjsxt.dao..*.*(..)))")
	public void AfterReturning() {
		System.out.println("method after returning");
	}	
}

  

簡化成下面寫法:

package com.bjsxt.aop;

import org.aspectj.lang.ProceedingJoinPoint;
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;

@Aspect
@Component
public class LogInterceptor {
	@Pointcut("execution(public * com.bjsxt.dao..*.*(..))")
	public void myMethod(){};
@Before("myMethod()") public void before() { System.out.println("method before"); }
@AfterReturning("myMethod()") public void AfterReturning() { System.out.println("method after Returning"); } }

  

 

UserServiceTest.java:

package com.bjsxt.service;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.bjsxt.model.User;

//Dependency Injection
//Inverse of Control
public class UserServiceTest {

	@Test 
	public void testAdd() throws Exception {
		ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
		UserService service = (UserService)ctx.getBean("userService");
		System.out.println(service.getClass());
		service.add(new User());		
		ctx.destroy();		
	}
}

UserService.java:

package com.bjsxt.service;
import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.bjsxt.dao.UserDAO;
import com.bjsxt.model.User;


@Component("userService")
public class UserService {
	
	private UserDAO userDAO;  
	
	public void init() {
		System.out.println("init");
	}
	
	public void add(User user) {
		userDAO.save(user);
	}
	public UserDAO getUserDAO() {
		return userDAO;
	}
	
	@Resource(name="u")
	public void setUserDAO( UserDAO userDAO) {
		this.userDAO = userDAO;
	}	
	public void destroy() {
		System.out.println("destroy");
	}
}

UserDAOImpl.java:

package com.bjsxt.dao.impl;

import org.springframework.stereotype.Component;

import com.bjsxt.dao.UserDAO;
import com.bjsxt.model.User;

@Component("u") 
public class UserDAOImpl implements UserDAO {

	public void save(User user) {
		//Hibernate
		//JDBC
		//XML
		//NetWork
		System.out.println("user saved!");
		//throw new RuntimeException("exeption!");
	}

}

UserDAO.java:

package com.bjsxt.dao;
import com.bjsxt.model.User;
public interface UserDAO {
	public void save(User user);
}

User.java:

package com.bjsxt.dao;
import com.bjsxt.model.User;


public interface UserDAO {
	public void save(User user);
}

 

結果:

class com.bjsxt.service.UserService
method before
user saved!
method after Returning

  

LogInterceptor.java的around用法:

package com.bjsxt.aop;

import org.aspectj.lang.ProceedingJoinPoint;
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;

@Aspect
@Component
public class LogInterceptor {
	@Pointcut("execution(public * com.bjsxt.dao..*.*(..))")
	public void myMethod(){};
	
	@Before("myMethod()")
	public void before() {
		System.out.println("method before");
	}
	
	@Around("myMethod()")
	public void aroundMethod(ProceedingJoinPoint pjp) throws Throwable {
		System.out.println("method around start");
		pjp.proceed();
		System.out.println("method around end");
	}
	
}

beans.xml: XML catalog裏引入 aop的 xsd

<?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-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
	<context:annotation-config />
	<context:component-scan base-package="com.bjsxt"/>
 	<aop:aspectj-autoproxy />
</beans>

UserServiceTest.java:

package com.bjsxt.service;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.bjsxt.model.User;

//Dependency Injection
//Inverse of Control
public class UserServiceTest {

	@Test 
	public void testAdd() throws Exception {
		ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
		
		UserService service = (UserService)ctx.getBean("userService");
		System.out.println(service.getClass());
		service.add(new User());
		service.delete();
		ctx.destroy();
		
	}

}

UserService.java:

package com.bjsxt.service;
import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.bjsxt.dao.UserDAO;
import com.bjsxt.model.User;


@Component("userService")
public class UserService {
	
	private UserDAO userDAO;  
	
	public void init() {
		System.out.println("init");
	}
	
	public void add(User user) {
		userDAO.save(user);
	}
	public void delete() {
		userDAO.delete();
	}
	public UserDAO getUserDAO() {
		return userDAO;
	}
	
	@Resource(name="u")
	public void setUserDAO( UserDAO userDAO) {
		this.userDAO = userDAO;
	}	
	public void destroy() {
		System.out.println("destroy");
	}
}

UserDAOImpl.java:

package com.bjsxt.dao.impl;

import org.springframework.stereotype.Component;

import com.bjsxt.dao.UserDAO;
import com.bjsxt.model.User;

@Component("u") 
public class UserDAOImpl implements UserDAO {

	public void save(User user) {
		System.out.println("user saved!");
		//throw new RuntimeException("exeption!");
	}
	public void delete() {		
		System.out.println("user delete!");
		//throw new RuntimeException("exeption!");
	}
}

UserDAO.java  :

package com.bjsxt.dao;
import com.bjsxt.model.User;
public interface UserDAO {
	public void save(User user);
	public void delete();
}

  

結果:

class com.bjsxt.service.UserService$$EnhancerByCGLIB$$b6531e3e
method around start
method before
user saved!
method around end
method around start
method before
user delete!
method around end

  

 

 

 

改爲service的話, 由於沒有實現接口, 因此須要 引入一個包: cglib-nodep-2.1_3.jar

package com.bjsxt.aop;

import org.aspectj.lang.ProceedingJoinPoint;
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;

@Aspect
@Component
public class LogInterceptor {
	@Pointcut("execution(public * com.bjsxt.service..*.add(..))")
	public void myMethod(){};
	
	@Before("myMethod()")
	public void before() {
		System.out.println("method before");
	}
	
	@Around("myMethod()")
	public void aroundMethod(ProceedingJoinPoint pjp) throws Throwable {
		System.out.println("method around start");
		pjp.proceed();
		System.out.println("method around end");
	}
	
}

結果:

class com.bjsxt.service.UserService$$EnhancerByCGLIB$$9160c20d
method around start
method before
user saved!
method around end
相關文章
相關標籤/搜索
本站公眾號
   歡迎關注本站公眾號,獲取更多信息