标签:调用 spring System public 18 println new class out
带参数绑定的通知方法调用
点击查看代码
@Aspect
static class MyAspect {
@Before("execution(* foo(..))") // 静态通知调用,不带参数绑定,执行时不需要切点
public void before1() {
System.out.println("before1");
}
@Before("execution(* foo(..)) && args(x)") // 动态通知调用,需要参数绑定,执行时还需要切点对象
public void before2(int x) {
System.out.printf("before2(%d)%n", x);
}
}
static class Target {
public void foo(int x) {
System.out.printf("target foo(%d)%n", x);
}
}
@Configuration
static class MyConfig {
@Bean
AnnotationAwareAspectJAutoProxyCreator proxyCreator() {
return new AnnotationAwareAspectJAutoProxyCreator();
}
@Bean
public MyAspect myAspect() {
return new MyAspect();
}
}
public static void main(String[] args) throws Throwable {
GenericApplicationContext context = new GenericApplicationContext();
context.registerBean(ConfigurationClassPostProcessor.class);
context.registerBean(MyConfig.class);
context.refresh();
AnnotationAwareAspectJAutoProxyCreator creator = context.getBean(AnnotationAwareAspectJAutoProxyCreator.class);
List<Advisor> list = creator.findEligibleAdvisors(Target.class, "target");
Target target = new Target();
ProxyFactory factory = new ProxyFactory();
factory.setTarget(target);
factory.addAdvisors(list);
Object proxy = factory.getProxy(); // 获取代理
List<Object> interceptorList =
factory.getInterceptorsAndDynamicInterceptionAdvice(Target.class.getMethod(
"foo", int.class), Target.class);
for (Object o : interceptorList) {
showDetail(o);
}
System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>");
ReflectiveMethodInvocation invocation = new ReflectiveMethodInvocation(
proxy, target, Target.class.getMethod("foo", int.class),
new Object[]{100}, Target.class, interceptorList
) {}; // spring 内部可以创建匿名子类来创建构造器 protected 的对象
invocation.proceed();
/*
学到了什么
a. 有参数绑定的通知调用时还需要切点,对参数进行匹配及绑定
b. 复杂程度高, 性能比无参数绑定的通知调用低
*/
}
public static void showDetail(Object o) {
try {
Class<?> clazz = Class.forName("org.springframework.aop.framework.InterceptorAndDynamicMethodMatcher");
if (clazz.isInstance(o)) {
Field methodMatcher = clazz.getDeclaredField("methodMatcher");
methodMatcher.setAccessible(true);
Field methodInterceptor = clazz.getDeclaredField("interceptor");
methodInterceptor.setAccessible(true);
System.out.println("环绕通知和切点:" + o);
System.out.println("\t切点为:" + methodMatcher.get(o));
System.out.println("\t通知为:" + methodInterceptor.get(o));
} else {
System.out.println("普通环绕通知:" + o);
}
} catch (Exception e) {
e.printStackTrace();
}
}
- 通过 proxyFactory 的 getInterceptorsAndDynamicInterceptionAdvice() 将其他通知统一转换为 MethodInterceptor 环绕通知
- 所谓动态通知,体现在上面方法的 DynamicInterceptionAdvice 部分,这些通知调用时因为要为通知方法绑定参数,还需再次利用切点表达式
- 动态通知调用复杂程度高,性能较低
”匿名子类写法“:
可参考:https://blog.csdn.net/weixin_39570751/article/details/121253167
点击查看代码
class S18Bean1 {
public S18Bean1() {
}
public void add() {
}
}
public class S18Bean2 {
public static void main(String[] args) {
S18Bean1 s18Bean3 = new S18Bean1() {
};
S18Bean1 s18Bean2 = new S18Bean1() {
};
System.out.println(s18Bean3.getClass().getName());
System.out.println(s18Bean2.getClass().getName());
}
}
点击查看代码
一个包下的字包不属于当前包,当前包中的 protect 成员不能被子包访问。
如果里面有个类有个 protect 修饰的构造器 在子包中访问不了,spring 内部就是通过这个办法创建它的子类调用
标签:调用,spring,System,public,18,println,new,class,out 来源: https://www.cnblogs.com/xy7112/p/16411112.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。