1. 程式人生 > >zbb20180824 spring ioc aop 例子

zbb20180824 spring ioc aop 例子

nature release build index bean result method 控制 prope

技術分享圖片

zbb_ioc_aop

https://pan.baidu.com/disk/home#/all?vmode=list&path=%2Fcode%2Fspring

pom.xml

<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/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.zbb</groupId>
	<artifactId>zbb_test01</artifactId>
	<packaging>war</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<name>zbb_test01 Maven Webapp</name>
	<url>http://maven.apache.org</url>
	<dependencies>
		<!-- https://mvnrepository.com/artifact/junit/junit -->
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.12</version>
			<scope>test</scope>
		</dependency>

		<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>4.0.1</version>
			<scope>provided</scope>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>5.0.8.RELEASE</version>
		</dependency>
		<!-- aop aspect註解導包 -->
		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjrt</artifactId>
			<version>1.8.6</version>
		</dependency>
		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjweaver</artifactId>
			<version>1.8.9</version>
		</dependency>

	</dependencies>
	<build>
		<finalName>zbb_test01</finalName>
	</build>
</project>

  web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
	id="WebApp_ID" version="3.0">
	<display-name>springMVC</display-name>
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
	<!-- spring 配置 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
			classpath:applicationContext.xml
		</param-value>
	</context-param>

	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<!-- spring-mvc 配置 -->
	<servlet>
		<servlet-name>spring-mvc</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:spring-mvc.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>


	<servlet-mapping>
		<servlet-name>spring-mvc</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

</web-app>

  

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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:security="http://www.springframework.org/schema/security"
xsi:schemaLocation="
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.2.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">

<!-- Activates annotation-based bean configuration -->
<context:annotation-config />

<!-- Scans for application @Components to deploy -->
<context:component-scan base-package="com.zbb" />

</beans>

  


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

 

 

<!-- 掃描控制器類 -->
<context:component-scan base-package="com.zbb" />
<!-- 啟動AspectJ支持 只對掃描過的bean有效 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>

<!--開啟註解 -->
<mvc:annotation-driven />

<!-- jsp視圖解析器 -->
<bean id="jspViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>


</beans>

  

LoggerAspect.java

package com.zbb.aspect;

import java.util.Arrays;
import java.util.List;

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.AfterThrowing;
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.core.annotation.Order;
import org.springframework.stereotype.Component;

@Aspect // 該標簽把LoggerAspect類聲明為一個切面
@Order(1) // 設置切面的優先級:如果有多個切面,可通過設置優先級控制切面的執行順序(數值越小,優先級越高)
@Component // 該標簽把LoggerAspect類放到IOC容器中
public class LoggerAspect {

	/**
	 * 定義一個方法,用於聲明切入點表達式,方法中一般不需要添加其他代碼 使用@Pointcut聲明切入點表達式
	 * 後面的通知直接使用方法名來引用當前的切點表達式;如果是其他類使用,加上包名即可
	 */
	@Pointcut("execution(public * com.zbb.controller.*.*(..))")
	public void declearJoinPointExpression() {
	}
	@Pointcut("execution(public * com.zbb.service.*.*(..))")
	public void declearJoinPointExpression2() {
	}

	/**
	 * 前置通知
	 * 
	 * @param joinPoint
	 */
	@Before("declearJoinPointExpression()") // 該標簽聲明次方法是一個前置通知:在目標方法開始之前執行
	public void beforMethod(JoinPoint joinPoint) {
		String methodName = joinPoint.getSignature().getName();
		List<Object> args = Arrays.asList(joinPoint.getArgs());
		System.out.println("this method " + methodName + " begin. param<" + args + ">");
	}

	/**
	 * 後置通知(無論方法是否發生異常都會執行,所以訪問不到方法的返回值)
	 * 
	 * @param joinPoint
	 */
	@After("declearJoinPointExpression()")
	public void afterMethod(JoinPoint joinPoint) {
		String methodName = joinPoint.getSignature().getName();
		System.out.println("this method " + methodName + " end.");
	}

	/**
	 * 返回通知(在方法正常結束執行的代碼) 返回通知可以訪問到方法的返回值!
	 * 
	 * @param joinPoit
	 */
	@AfterReturning(value = "declearJoinPointExpression()", returning = "result")
	public void afterReturnMethod(JoinPoint joinPoint, Object result) {
		String methodName = joinPoint.getSignature().getName();
		System.out.println("this method " + methodName + " end.result<" + result + ">");
	}

	/**
	 * 異常通知(方法發生異常執行的代碼) 可以訪問到異常對象;且可以指定在出現特定異常時執行的代碼
	 * 
	 * @param joinPoint
	 * @param ex
	 */
	@AfterThrowing(value = "declearJoinPointExpression()", throwing = "ex")
	public void afterThrowingMethod(JoinPoint joinPoint, Exception ex) {
		String methodName = joinPoint.getSignature().getName();
		System.out.println("this method " + methodName + " end.ex message<" + ex + ">");
	}

	/**
	 * 環繞通知(需要攜帶類型為ProceedingJoinPoint類型的參數)
	 * 環繞通知包含前置、後置、返回、異常通知;ProceedingJoinPoin 類型的參數可以決定是否執行目標方法
	 * 且環繞通知必須有返回值,返回值即目標方法的返回值
	 * 
	 * @param joinPoint
	 */
	@Around(value = "declearJoinPointExpression()")
	public Object aroundMethod(ProceedingJoinPoint point) {

		Object result = null;
		String methodName = point.getSignature().getName();
		try {
			// 前置通知
			System.out.println("The method " + methodName + " start. param<" + Arrays.asList(point.getArgs()) + ">");
			// 執行目標方法
			result = point.proceed();
			// 返回通知
			System.out.println("The method " + methodName + " end. result<" + result + ">");
		} catch (Throwable e) {
			// 異常通知
			System.out.println("this method " + methodName + " end.ex message<" + e + ">");
			throw new RuntimeException(e);
		}
		// 後置通知
		System.out.println("The method " + methodName + " end.");
		return result;
	}
	
	
	/**
	 * 前置通知
	 * 
	 * @param joinPoint
	 */
	@Before("declearJoinPointExpression2()") // 該標簽聲明次方法是一個前置通知:在目標方法開始之前執行
	public void beforMethod2(JoinPoint joinPoint) {
		String methodName = joinPoint.getSignature().getName();
		List<Object> args = Arrays.asList(joinPoint.getArgs());
		System.out.println("this method " + methodName + " begin. param<" + args + ">");
	}

	/**
	 * 後置通知(無論方法是否發生異常都會執行,所以訪問不到方法的返回值)
	 * 
	 * @param joinPoint
	 */
	@After("declearJoinPointExpression2()")
	public void afterMethod2(JoinPoint joinPoint) {
		String methodName = joinPoint.getSignature().getName();
		System.out.println("this method " + methodName + " end.");
	}

	/**
	 * 返回通知(在方法正常結束執行的代碼) 返回通知可以訪問到方法的返回值!
	 * 
	 * @param joinPoit
	 */
	@AfterReturning(value = "declearJoinPointExpression2()", returning = "result")
	public void afterReturnMethod2(JoinPoint joinPoint, Object result) {
		String methodName = joinPoint.getSignature().getName();
		System.out.println("this method " + methodName + " end.result<" + result + ">");
	}

	/**
	 * 異常通知(方法發生異常執行的代碼) 可以訪問到異常對象;且可以指定在出現特定異常時執行的代碼
	 * 
	 * @param joinPoint
	 * @param ex
	 */
	@AfterThrowing(value = "declearJoinPointExpression2()", throwing = "ex")
	public void afterThrowingMethod2(JoinPoint joinPoint, Exception ex) {
		String methodName = joinPoint.getSignature().getName();
		System.out.println("this method " + methodName + " end.ex message<" + ex + ">");
	}

	/**
	 * 環繞通知(需要攜帶類型為ProceedingJoinPoint類型的參數)
	 * 環繞通知包含前置、後置、返回、異常通知;ProceedingJoinPoin 類型的參數可以決定是否執行目標方法
	 * 且環繞通知必須有返回值,返回值即目標方法的返回值
	 * 
	 * @param joinPoint
	 */
	@Around(value = "declearJoinPointExpression2()")
	public Object aroundMethod2(ProceedingJoinPoint point) {

		Object result = null;
		String methodName = point.getSignature().getName();
		try {
			// 前置通知
			System.out.println("The method " + methodName + " start. param<" + Arrays.asList(point.getArgs()) + ">");
			// 執行目標方法
			result = point.proceed();
			// 返回通知
			System.out.println("The method " + methodName + " end. result<" + result + ">");
		} catch (Throwable e) {
			// 異常通知
			System.out.println("this method " + methodName + " end.ex message<" + e + ">");
			throw new RuntimeException(e);
		}
		// 後置通知
		System.out.println("The method " + methodName + " end.");
		return result;
	}
}

  TestController.java

package com.zbb.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.zbb.service.TestService;

@RestController
@RequestMapping("test")
public class TestController {
	@Autowired
	TestService testService;
	@RequestMapping("t1")
	public void t1(){
		testService.t2();
	}
}

  TestService.java

package com.zbb.service;

import org.springframework.stereotype.Service;

@Service
public class TestService {
public void t2(){
	System.out.println("asdfafasdf");
}
}

  

zbb20180824 spring ioc aop 例子