首页 > 编程语言 >java web开发(aop编程)

java web开发(aop编程)

时间:2022-11-23 15:08:04浏览次数:41  
标签:web 劫持 java System aop println org public out



        刚开始看到aop的时候,了解到它是面向切片,觉得还是挺拗口的。不知道应该怎么去理解怎么这么一个概念。后来想了一想,不如先看范例,直接从实例去理解aop或许更容易一点。

  。从实例代码来看,aop更类似于用一种拦截器的方法去实现方法与方法直接调用的解耦。也就是说,方法之间的调用不再用硬编码的形式来实现,而是通过applicationContext.xml脚本配置的形式来完成。参考的实例内容略多,这里整理成一个完整的范例,大家可以逐步调试分析。

1、利用IDEA创建工程,选择“Spring”和“Application”

2、工程命名为Hello,创建完毕后,IDEA会自动下载好spring的jar库

3、创建CustomerService.java文件

public class CustomerService {
private String name;
private String url;

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

public void setUrl(String url) {
this.url = url;
}

public void printName() {
System.out.println("Customer name : " + this.name);
}

public void printURL() {
System.out.println("Customer website : " + this.url);
}

public void printThrowException() {
throw new IllegalArgumentException();
}
}

4、编写Test测试类Test.java,这部分和之前差别不大

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

public class Test {
public static void main(String[] args) {
ApplicationContext appContext = new ClassPathXmlApplicationContext(
new String[] { "applicationContext.xml" });

CustomerService cust = (CustomerService) appContext.getBean("customerServiceProxy");

System.out.println("*************************");
cust.printName();
System.out.println("*************************");
cust.printURL();
System.out.println("*************************");
try {
cust.printThrowException();
} catch (Exception e) {

}
}
}

5、准备各种各样的劫持类,比如事先劫持类、事后劫持类、异常劫持类、全部劫持类等

5.1 事前劫持类,继承自MethodBeforeAdvice

import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;

public class HijackBeforeMethod implements MethodBeforeAdvice
{
@Override
public void before(Method method, Object[] args, Object target)
throws Throwable {
System.out.println("HijackBeforeMethod : Before method hijacked!");
}
}

5.2 事后劫持类,继承自AfterReturningAdvice

import java.lang.reflect.Method;
import org.springframework.aop.AfterReturningAdvice;

public class HijackAfterMethod implements AfterReturningAdvice
{
@Override
public void afterReturning(Object returnValue, Method method,
Object[] args, Object target) throws Throwable {
System.out.println("HijackAfterMethod : After method hijacked!");
}
}

5.3 异常劫持,继承自ThrowsAdvice

import org.springframework.aop.ThrowsAdvice;

public class HijackThrowException implements ThrowsAdvice {

public void afterThrowing(IllegalArgumentException e) throws Throwable {
System.out.println("HijackThrowException : Throw exception hijacked!");
}
}

        注意到,这里的劫持方法没有@Override声明。

5.4 全部劫持,继承自MethodInterceptor

import java.util.Arrays;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

public class HijackAroundMethod implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation methodInvocation) throws Throwable {

System.out.println("Method name : "
+ methodInvocation.getMethod().getName());
System.out.println("Method arguments : "
+ Arrays.toString(methodInvocation.getArguments()));

// same with MethodBeforeAdvice
System.out.println("HijackAroundMethod : Before method hijacked!");

try {
// proceed to original method call
Object result = methodInvocation.proceed();

// same with AfterReturningAdvice
System.out.println("HijackAroundMethod : After method hijacked!");

return result;

} catch (IllegalArgumentException e) {
// same with ThrowsAdvice
System.out.println("HijackAroundMethod : Throw exception hijacked!");
throw e;
}
}
}

        这个劫持方法是功能最完整的劫持方法。此外,在invoke方法中,也需要自己调用methodInvocation.proceed来调用被劫持的函数。此外,对于异常,invoke方法也可以做到劫持访问。

6、准备和创建applicaitonContext.xml文件,方法参照上一篇blog内容

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="customerService" class="CustomerService">
<property name="name" value="baidu" />
<property name="url" value="www.baidu.com" />
</bean>

<bean id="hijackBeforeMethodBean" class="HijackBeforeMethod" />
<bean id="hijackAfterMethodBean" class="HijackAfterMethod" />
<bean id="hijackThrowExceptionBean" class="HijackThrowException" />
<bean id="hijackAroundMethodBean" class="HijackAroundMethod" />

<bean id="customerServiceProxy"
class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="customerService" />
<property name="interceptorNames">
<list>
<value>hijackBeforeMethodBean</value>
<!--<value>hijackAfterMethodBean</value>
<value>hijackThrowExceptionBean</value>
<value>hijackAroundMethodBean</value>-->
</list>
</property>
</bean>
</beans>

7、调试Test.java文件,不妨选择hijackBeforeMethodBean做测试,右击选择“Run Test.main函数”,

不出意外,就可以看到这样的打印结果,

java web开发(aop编程)_servlet

标签:web,劫持,java,System,aop,println,org,public,out
From: https://blog.51cto.com/feixiaoxing/5881285

相关文章

  • java web开发(mysql开发)
        选择了java作为项目开发语言,一般就不再会选择商业数据库了,比如说sqlserver,oracle之类的,除非是项目有特殊需求。大多数情况下,开发者都会选择mysql作为应用数据......
  • java web开发(第一个spring程序)
        提到javaweb编程,好像spring就躲不开了。一般认为,spring有两个特征,分别是ioc、aop。两个英文单词的中文解释都比较拗口,一个称之为控制反转,一个是面向切面。对于......
  • java web开发(servlet传递数据给jsp)
        实际开发中,servlet多用于controller,jsp多用于view。之前,我们谈过了怎么把数据从jsp传递给servlet,即采用form+action的方法来完成这一目标。今天可以继续讨论下,s......
  • java web开发(jsp传递数据给servlet)
        有了servlet,有了jsp,其实就已经可以做很多的事情了。比如说表单的制作等等。在实际项目中,表单、表格都是很常见的输入选项。一个常见的场景,就是客户在输入数据之......
  • java web开发(第一个jsp工程)
        前面两期,我们谈到了怎么进行servlet的编写。一种方法是将传统的java工程改造成servlet工程;另外一种方法就是直接利用maven创建一个webapp工程。事实上呢,如果大......
  • java web开发(maven创建servlet程序)
        之前我们写了一篇文章,主要是说一般情况下怎么开发servlet。其实,用maven创建servlet工程也是非常方便的。网上有一篇文章,地址在,也谈到了怎么实现idea+maven+serv......
  • jave web开发(IDEA中配置maven + 第一个pom包)
        maven是现在java中用的比较多的一个软件。一方面,maven可以单独使用;另外一方面maven也可以和各个IDE进行配合,比如刚刚安装的IDEA,就可以和maven进行联合配置使用......
  • java web开发(注解)
        注解也是蛮有意思的。个人来看,它更多是一种偷懒的处理方法。一种常见的场景是这样的,如果我们希望对某一种类型的数据来做判断,那么就是在每个函数开始的位置添加......
  • java web开发(反射)
        反射是java很重要的一个特点。也是它区别于c、c++、fortan等传统语言的一个重要的语言特征。通过反射可以做很多的事情,比如动态创建类,动态修改变量,动态调用类函......
  • java web开发(IDEA安装 + 第一个java工程)
    发环境是myeclipse。时过境迁,现在大家更多地愿意用IDEA环境来开发。对于新手,尤其是刚入门的同学来说,有一个好的IDE环境还是能够减轻不少负担的。这样,可以让学习的曲线不是......