使用AspectJ LTW(Load Time Weaving)

    在Java 语言中,从织入切面的方式上来看,存在三种织入方式:编译期织入、类加载期织入和运行期织入。编译期织入是指在Java编译期,采用特殊的编译器,将切面织入到Java类中;而类加载期织入则指通过特殊的类加载器,在类字节码加载到JVM时,织入切面;运行期织入则是采用CGLib工具或JDK动态代理进行切面的织入。
    AspectJ采用编译期织入和类加载期织入的方式织入切面,是语言级的AOP实现,提供了完备的AOP支持。它用AspectJ语言定义切面,在编译期或类加载期将切面织入到Java类中。
    AspectJ提供了两种切面织入方式,第一种通过特殊编译器,在编译期,将AspectJ语言编写的切面类织入到Java类中,可以通过一个Ant或Maven任务来完成这个操作;第二种方式是类加载期织入,也简称为LTW(Load Time Weaving)。
    使用AspectJ LTW有两个主要步骤,第一,通过JVM的-javaagent参数设置LTW的织入器类包,以代理JVM默认的类加载器;第二,LTW织入器需要一个 aop.xml文件,在该文件中指定切面类和需要进行切面织入的目标类。

    设置-javaagent JVM参数的方法:
(1)在Eclipse下的设置:
运行类->右键单击->Run As->Run...,可以在弹出的Run设置窗口设置该类的各项运行属性,切换到Arguments Tab页,在VM arguments中通过-javaagent指定AspectJ 织入器类包。-javaagent:E:\workspace\lib\spring2.5\aspectjweaver.jar
(2)在Tomcat下的设置
打开<Tomcat_Home>\bin\catalina.bat,在该批处理文件头部添加以下的设置:
set JAVA_OPTS=-javaagent:E:\workspace\lib\spring2.5\aspectjweaver.jar

一、配置LTW织入器的aop.xml配置文件
    LTW织入器在工作时,首先会查找类路径下META-INF /aop.xml的配置文件,并根据配置文件的设置进行织入的操作。
<?xml version="1.0" encoding="UTF-8"?>
<aspectj>
	<aspects>
		<!-- ①切面类 -->
		<aspect name="quickstart.aspectj.Monitor" />
	</aspects>
	<weaver
		options="-showWeaveInfo -XmessageHandlerClass:org.springframework.aop.aspectj.AspectJWeaverMessageHandler">
		<!-- ② 指定需要进行织入操作的目标类范围 -->
		<include within="quickstart.service..*" />
	</weaver>
</aspectj>

二、切面织入的目标类和切面实例
package quickstart.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import quickstart.dao.PersonDao;
import quickstart.model.Person;
import quickstart.service.PeopleService;

public class PeopleServiceImpl implements PeopleService{
	
	//通过类型(byType为默认)的自动连接可能会有多个候选,
	//通过使用@Qualifier注解,使用名称(byName)来限定
	@Autowired
	@Qualifier("personDao")
	private PersonDao personDao;
	
	@Transactional(propagation = Propagation.REQUIRED)
	public void savePerson(Person person) {
		personDao.save(person);
	}

	@Transactional(readOnly = true, propagation = Propagation.REQUIRED)
	public Person findPersonById(Integer id) {
		return personDao.findById(id);
	}

	@Transactional(readOnly = true, propagation = Propagation.REQUIRED)
	public List findPersonByJPQL(String jpql){
		return personDao.findByJPQL(jpql);
	}

package quickstart.aspectj;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
public class Monitor {

	@Pointcut("execution(* quickstart.service.impl.*.*(..))")
	public void anyService(){}

	
	@Before("quickstart.aspectj.Monitor.anyService()")
	public void log(JoinPoint joinPoint){
		System.out.println("Service Method " + joinPoint.getSignature().getName() + " Invocation!");
	}
}

三、Spring配置文件applicationContext.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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">

	<!-- 需要指定factory-method="aspectOf"属性,以便确保Spring从AspectJ获取切面实例,而非自己创建该实例。 -->
	<bean id="momitor" class="quickstart.aspectj.Monitor"  factory-method="aspectOf"/>
	
	<!-- 自动装配注解Bean后置处理器 -->
	<bean 
		class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
    
    <!-- JPA注解Bean后置处理器 -->
    <bean
        class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

	<!-- 利用Spring的实体管理器工厂来创建JPA实体管理器 -->
    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="jpaVendorAdapter">
            <bean
                class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="database" value="MYSQL" />
                <property name="showSql" value="true" />
                <!-- <property name="generateDdl" value="true" /> -->
            </bean>
        </property>
    </bean>

    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost/test?useUnicode=true&amp;characterEncoding=UTF-8" />
        <property name="username" value="root" />
        <property name="password" value="root" />
    </bean>
	
	<!-- 声明一个Spring提供的JPA事务管理器,传入的参数是Spring中的实体管理器工厂 --> 
    <bean id="transactionManager"
        class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>

	<!-- 开启Spring提供的基于注解的声明式事务管理 -->
    <tx:annotation-driven transaction-manager="transactionManager" />
 	
 	<!-- 直接使用Spring的 JpaTemplate -->
	<bean id="jpaTemplate" class="org.springframework.orm.jpa.JpaTemplate">
		<property name="entityManagerFactory" ref="entityManagerFactory" />
	</bean>

	<bean id="personDao" class="quickstart.dao.impl.PersonDaoImpl" autowire="byName"/>
	
	<bean id="peopleService" class="quickstart.service.impl.PeopleServiceImpl"/>
</beans>

四、测试
package logcd.test;

import java.util.List;

import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import quickstart.model.Address;
import quickstart.model.Person;
import quickstart.service.PeopleService;


public class JpaServiceTest {

	private static ApplicationContext appContext;
	private static PeopleService peopleService;
	
	@BeforeClass  
	public static void Init(){   
		appContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		peopleService = (PeopleService)appContext.getBean("peopleService");
	} 
	
	@Test
	public void findById(){
		Person person = peopleService.findPersonById(new Integer(2));
		System.out.println(person.getFirstName()+person.getLastName());
		//System.out.println(person.getAddress().getStreetName());
	}
	
	@Test
	public void findByJPQL(){
		StringBuilder jpql = new StringBuilder();
		jpql.append(" FROM Person p JOIN FETCH p.address");
		List list = peopleService.findPersonByJPQL(jpql.toString());
		for(Object obj : list){
			Person person = (Person)obj;
			System.out.println(person.getFirstName() + person.getLastName());
			System.out.println(person.getAddress().getStreetName());
		}
	}
}

你可能感兴趣的:(spring,AOP,jvm,jpa,配置管理)