1. 程式人生 > >JavaSpring之Aop應用(java專案)

JavaSpring之Aop應用(java專案)

1、配置所需要的maven依賴pom.xml檔案(這裡為spring核心依賴aop依賴和日誌依賴)

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.iflytek</groupId>
	<artifactId>myaop</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<dependencies>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>4.3.13.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>4.3.13.RELEASE</version>
			<scope>runtime</scope>
		</dependency>
		
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aop</artifactId>
			<version>4.3.13.RELEASE</version>
		</dependency>
		
		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjrt</artifactId>
			<version>1.8.10</version>
		</dependency>

		<dependency>
			<groupId>aopalliance</groupId>
			<artifactId>aopalliance</artifactId>
			<version>1.0</version>
		</dependency>
		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjweaver</artifactId>
			<version>1.8.10</version>
		</dependency>

		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.17</version>
		</dependency>
	</dependencies>
</project>
2、配置檔案ApplicationContext.xml檔案的配置

<aop:aspectj-autoproxy>是aop使用所需要的,對於下面的datasource和factory是關於資料庫的配置,這裡沒有新增依賴,於本例也沒有明顯的關係,所以註釋不實用。

<?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:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
         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
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

	<context:component-scan base-package="com.iflytek"></context:component-scan>
	
	<aop:aspectj-autoproxy />
	
	 <!-- <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property>
		<property name="url" value="jdbc:oracle:thin:@172.16.130.35:1521:orcl"></property>
		<property name="username" value="uuid"></property>
		<property name="password" value="uuid"></property>
	</bean>
	
	<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSource"></property>
		<property name="packagesToScan" value="com.iflytek.domain"></property>
		<property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
                <prop key="hibernate.hbm2ddl.auto">create</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop>
            </props>
		</property>
	</bean>  -->
	
</beans>
3、編寫實體類和對應的service類

Employee類

package com.iflytek.domain;

public class Employee {
	private int id;
	private String name;
	private double salary;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public double getSalary() {
		return salary;
	}

	public void setSalary(double salary) {
		this.salary = salary;
	}

	@Override
	public String toString() {
		return "Employee [id=" + id + ", name=" + name + ", salary=" + salary + "]";
	}

}

EmployeeService類(這裡加@Service註解表示在Spring容器中新增一個小寫字母的employeeService物件)

package com.iflytek.service;

import org.springframework.stereotype.Service;

import com.iflytek.domain.Employee;

@Service
public class EmployeeService {
	
	public void addEmployeeSalary(Employee e,double salary){
		e.setSalary(salary);
		System.out.println("初始化工資");
	}
	
	public void updateEmployeeSalary(double salary){
		System.out.println("更改工資");
	}
	
	public void findEmployeeSalary(double salary){
		System.out.println("查詢工資");
	}
}

4、編寫切入類,這裡用註解配置(@Aspect標明本類是切面類,@Pointcut宣告切點)
package com.iflytek.service;

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import com.iflytek.domain.Employee;

@Aspect
@Component
public class MyInterceptor {
	 @Pointcut("execution (* com.iflytek.service.EmployeeService.add*(..)) and args(param)")
	// @Pointcut( "execution(* com.fishwasser.action.UserAction.*(..)) and args()" )
	 private void anyMethod() {} // 宣告一個切入點,anyMethod為切入點名稱
	 
	 @Before("anyMethod()")
	 public void doAddBefore(){
		 System.out.println("初始化之前的工資:");
	 }
	 
	 @After(value="anyMethod()")
	 public void doAddAfter(){
		 System.out.println("初始化之後的工資:");
	 }
}

5、測試類

package com.iflytek.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.iflytek.domain.Employee;
import com.iflytek.service.EmployeeService;

public class EmployeeTest {

	public static void main(String[] args) {
		ApplicationContext cxt = new ClassPathXmlApplicationContext("applicationContext.xml");
		EmployeeService employeeService = (EmployeeService) cxt.getBean("employeeService");
		
		Employee employee=new Employee();
		employeeService.addEmployeeSalary(employee, 3000.0);
	}
}

可以看到結果在單獨執行add方法的時候,分別執行的切入的before和after方法,說明aop切入成功