mirror of
https://github.com/Estom/notes.git
synced 2026-02-06 12:04:05 +08:00
Java内容重新整理删除过期的东西
This commit is contained in:
142
Java/JavaDemo/codedemo/aspectj/BusinessService.java
Normal file
142
Java/JavaDemo/codedemo/aspectj/BusinessService.java
Normal file
@@ -0,0 +1,142 @@
|
||||
package cn.aofeng.demo.aspectj;
|
||||
|
||||
/**
|
||||
* 模拟业务方法,将被Aspectj织入代码,增加功能。
|
||||
*
|
||||
* @author <a href="mailto:aofengblog@163.com">聂勇</a>
|
||||
*/
|
||||
public class BusinessService {
|
||||
|
||||
public long add(int a, int b) {
|
||||
return a+b;
|
||||
}
|
||||
|
||||
public long add(int a, int b, int... other) {
|
||||
long result = a + b;
|
||||
for (int i : other) {
|
||||
result += i;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public String join(String first, String... appends) {
|
||||
if (null == first) {
|
||||
throw new IllegalArgumentException("first is null");
|
||||
}
|
||||
StringBuilder buffer = new StringBuilder();
|
||||
buffer.append(first);
|
||||
for (String str : appends) {
|
||||
buffer.append(str);
|
||||
}
|
||||
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
public String addPrefix(String src) {
|
||||
if (null == src) {
|
||||
throw new IllegalArgumentException("src is null");
|
||||
}
|
||||
|
||||
return "-->"+src;
|
||||
}
|
||||
|
||||
public static void printLine(char style) {
|
||||
if ('=' == style) {
|
||||
System.out.println("========================================================================================");
|
||||
} else if ('-' == style) {
|
||||
System.out.println("----------------------------------------------------------------------------------------");
|
||||
} else {
|
||||
System.out.println(" ");
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
final BusinessService bs = new BusinessService();
|
||||
|
||||
System.out.println("1、执行方法add(int, int)");
|
||||
RunMethod rm = new RunMethod() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
long result = bs.add(1, 2);
|
||||
System.out.println(">>> 结果:" + result);
|
||||
}
|
||||
};
|
||||
rm.execute();
|
||||
|
||||
System.out.println("2、执行方法add(int, int, int...)");
|
||||
rm = new RunMethod() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
long result = bs.add(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
|
||||
System.out.println(">>> 结果:" + result);
|
||||
}
|
||||
};
|
||||
rm.execute();
|
||||
|
||||
System.out.println("3、执行方法join(String, String...)");
|
||||
rm = new RunMethod() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
String str = bs.join("first", "-second", "-third");
|
||||
System.out.println(">>> 结果:" + str);
|
||||
}
|
||||
};
|
||||
rm.execute();
|
||||
|
||||
System.out.println("4、执行方法join(String, String...)");
|
||||
rm = new RunMethod() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
String str = bs.join(null, "-second", "-third");
|
||||
System.out.println(">>> 结果:" + str);
|
||||
}
|
||||
};
|
||||
rm.execute();
|
||||
|
||||
System.out.println("5、执行方法addPrefix(String)");
|
||||
rm = new RunMethod() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
String str = bs.addPrefix("原字符串");
|
||||
System.out.println(">>> 结果:" + str);
|
||||
}
|
||||
};
|
||||
rm.execute();
|
||||
|
||||
System.out.println("6、执行方法addPrefix(String)");
|
||||
rm = new RunMethod() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
String str = bs.addPrefix(null);
|
||||
System.out.println(">>> 结果:" + str);
|
||||
}
|
||||
};
|
||||
rm.execute();
|
||||
}
|
||||
|
||||
public static abstract class RunMethod {
|
||||
|
||||
private char _style = '=';
|
||||
|
||||
public void execute() {
|
||||
printLine(_style);
|
||||
try {
|
||||
run();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace(System.err);
|
||||
}
|
||||
printLine(_style);
|
||||
printLine(' ');
|
||||
}
|
||||
|
||||
public abstract void run();
|
||||
}
|
||||
|
||||
}
|
||||
122
Java/JavaDemo/codedemo/aspectj/BusinessServiceInterceptor.java
Normal file
122
Java/JavaDemo/codedemo/aspectj/BusinessServiceInterceptor.java
Normal file
@@ -0,0 +1,122 @@
|
||||
package cn.aofeng.demo.aspectj;
|
||||
|
||||
import org.apache.commons.lang.ArrayUtils;
|
||||
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.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* 拦截器。
|
||||
*
|
||||
* @author <a href="mailto:aofengblog@163.com">聂勇</a>
|
||||
*/
|
||||
@Aspect
|
||||
public class BusinessServiceInterceptor {
|
||||
|
||||
private static Logger _logger = LoggerFactory.getLogger(BusinessServiceInterceptor.class);
|
||||
|
||||
private char _style = '-';
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* Before通知不能修改方法传入的参数。
|
||||
* </pre>
|
||||
*/
|
||||
@Before("execution(public * cn.aofeng.demo.aspectj.BusinessService.add(..))")
|
||||
public void beforeAdd(JoinPoint joinPoint) {
|
||||
_logger.info( String.format("拦截到方法:%s, 传入参数:%s",
|
||||
joinPoint.getSignature().getName(),
|
||||
ArrayUtils.toString(joinPoint.getArgs()) ) );
|
||||
BusinessService.printLine(_style);
|
||||
}
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* After通知不能修改方法的返回值。
|
||||
* 如果被拦截的方法抛出异常,拦截代码仍然正常执行。
|
||||
* </pre>
|
||||
*/
|
||||
@After("execution(public * cn.aofeng.demo.aspectj.BusinessService.join(..))")
|
||||
public void beforeAddSupportMultiArgs(JoinPoint joinPoint) {
|
||||
_logger.info("Signature.name:"+joinPoint.getSignature().getName());
|
||||
_logger.info("Args:" + ArrayUtils.toString(joinPoint.getArgs()));
|
||||
_logger.info("Target:" + ArrayUtils.toString(joinPoint.getTarget()));
|
||||
_logger.info("This:" + ArrayUtils.toString(joinPoint.getThis()));
|
||||
_logger.info("Kind:" + ArrayUtils.toString(joinPoint.getKind()));
|
||||
_logger.info("SourceLocation:" + ArrayUtils.toString(joinPoint.getSourceLocation()));
|
||||
|
||||
// 试图修改传入的参数
|
||||
joinPoint.getArgs()[0] = "100";
|
||||
|
||||
BusinessService.printLine(_style);
|
||||
}
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* AfterReturning通知不能修改方法的返回值。
|
||||
* 如果被拦截的方法抛出异常,拦截代码不再执行。
|
||||
* </pre>
|
||||
*/
|
||||
@AfterReturning(pointcut="execution(public * cn.aofeng.demo.aspectj.BusinessService.join(..))", returning="result")
|
||||
public void afterReturnAdd(JoinPoint joinPoint, Object result) {
|
||||
_logger.info( String.format("拦截到方法:%s, 传入参数:%s, 执行结果:%s",
|
||||
joinPoint.getSignature().getName(),
|
||||
ArrayUtils.toString(joinPoint.getArgs()),
|
||||
result) );
|
||||
|
||||
// 试图修改返回值
|
||||
result = "hello, changed";
|
||||
|
||||
BusinessService.printLine(_style);
|
||||
}
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 只在被拦截的方法抛出异常时才执行。
|
||||
* </pre>
|
||||
*/
|
||||
@AfterThrowing(pointcut="execution(public * cn.aofeng.demo.aspectj.BusinessService.join(..))", throwing="ex")
|
||||
public void afterThrowingAdd(JoinPoint joinPoint, Exception ex) {
|
||||
_logger.info( String.format("拦截到方法:%s, 传入参数:%s",
|
||||
joinPoint.getSignature().getName(),
|
||||
ArrayUtils.toString(joinPoint.getArgs()) ) );
|
||||
if (null != ex) {
|
||||
_logger.info("拦截到异常:", ex);
|
||||
}
|
||||
|
||||
BusinessService.printLine(_style);
|
||||
}
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* {@link ProceedingJoinPoint}只能在Around通知中使用。
|
||||
* Around通知可以修改被拦截方法的传入参数和返回值。
|
||||
* </pre>
|
||||
*/
|
||||
@Around("execution(public * cn.aofeng.demo.aspectj.BusinessService.addPrefix(..))")
|
||||
public Object afterAround(ProceedingJoinPoint joinPoint) throws Throwable {
|
||||
_logger.info( String.format("拦截到方法:%s, 传入参数:%s",
|
||||
joinPoint.getSignature().getName(),
|
||||
ArrayUtils.toString(joinPoint.getArgs()) ) );
|
||||
|
||||
Object result = null;
|
||||
try {
|
||||
result = joinPoint.proceed();
|
||||
_logger.info("执行结果:" + result);
|
||||
} catch (Throwable e) {
|
||||
throw e;
|
||||
} finally {
|
||||
BusinessService.printLine(_style);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
64
Java/JavaDemo/codedemo/aspectj/build.xml
Normal file
64
Java/JavaDemo/codedemo/aspectj/build.xml
Normal file
@@ -0,0 +1,64 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project name="JavaTutorial" default="runNormal" basedir="../../../../../">
|
||||
|
||||
<property name="project.src.dir" value="${basedir}/src" />
|
||||
<property name="project.lib.dir" value="${basedir}/lib" />
|
||||
<property name="project.conf.dir" value="${basedir}/conf" />
|
||||
<property name="project.tmp.dir" value="${basedir}/tmp" />
|
||||
<property name="project.target.dir" value="${basedir}/classes" />
|
||||
|
||||
<path id="project.classpath">
|
||||
<fileset dir="${project.lib.dir}">
|
||||
<include name="*.jar" />
|
||||
</fileset>
|
||||
</path>
|
||||
|
||||
<taskdef resource="org/aspectj/tools/ant/taskdefs/aspectjTaskdefs.properties">
|
||||
<classpath refid="project.classpath" />
|
||||
</taskdef>
|
||||
|
||||
<target name="prepare">
|
||||
<delete dir="${project.target.dir}" />
|
||||
<mkdir dir="${project.target.dir}"/>
|
||||
<copy todir="${project.target.dir}">
|
||||
<fileset dir="${project.conf.dir}">
|
||||
<include name="**/*.properties" />
|
||||
<include name="**/*.xml" />
|
||||
</fileset>
|
||||
</copy>
|
||||
</target>
|
||||
|
||||
<target name="compileWithIajc" depends="prepare">
|
||||
<echo message="compile with iajc" />
|
||||
<iajc destdir="${project.target.dir}" sourceroots="${project.src.dir}" source="1.8" target="1.8" encoding="UTF-8">
|
||||
<classpath refid="project.classpath" />
|
||||
</iajc>
|
||||
</target>
|
||||
|
||||
<!-- 运行AspectJ编译时织入功能的代码 -->
|
||||
<target name="runNormal" depends="compileWithIajc">
|
||||
<java classname="cn.aofeng.demo.aspectj.BusinessService">
|
||||
<classpath refid="project.classpath" />
|
||||
<classpath location="${project.target.dir}" />
|
||||
</java>
|
||||
</target>
|
||||
|
||||
<target name="compileWithJavac" depends="prepare">
|
||||
<echo message="compile with javac" />
|
||||
<javac destdir="${project.target.dir}" srcdir="${project.src.dir}" source="1.8" target="1.8"
|
||||
encoding="UTF-8" debug="true" includeantruntime="false">
|
||||
<classpath refid="project.classpath" />
|
||||
</javac>
|
||||
</target>
|
||||
|
||||
<!-- 载入代码时织入功能(LTW) -->
|
||||
<target name="runLTW" depends="compileWithJavac">
|
||||
<java classname="cn.aofeng.demo.aspectj.BusinessService" fork="true">
|
||||
<jvmarg value="-Daj.weaving.verbose=true" />
|
||||
<jvmarg value="-javaagent:${project.lib.dir}/aspectjweaver-1.8.10.jar"/>
|
||||
<classpath refid="project.classpath" />
|
||||
<classpath location="${project.target.dir}" />
|
||||
</java>
|
||||
</target>
|
||||
|
||||
</project>
|
||||
Reference in New Issue
Block a user