aspectj

Java Web-Spring框架(二)

你说的曾经没有我的故事 提交于 2020-01-19 00:07:39
1、后处理 bean 对一个bean生效 2、注解和xml混合使用 a、将所有的 bean 都配置在 xml 中,<bean id="" class="" b、将所有的依赖都使用注解,@Autowired,默认不生效。为了生效,需要在xml中配置:<context:annotation-config,如图: 上图中,两个注解一般不一起使用,“注解1”扫描含有注解(@Component等)类,注入注解也会自动生效。“注解2”只在xml和注解(注入)混合使用时,使注入注解生效。 3、AOP 3.1、AOP介绍 AOP(Aspect Oriented Programming),面向切面编程。通过预编译方式和运动期动态代理实现程序功能的统一维护的一种技术。是OOP(面向对象编程)的延续,采取 横向抽取 机制,取代了传统 纵向继承 体系重复性代码。如图: 经典应用:事务管理、性能监视、安全检查、缓存、日志等。 Spring AOP使用纯Java实现,不需要专门的编译过程和类加载器,在运行期通过代理方式向目标类织入增强代码。 AOP实现原理 AOP底层将采用代理机制进行实现。 接口 + 实现类:spring采用 jdk的 动态代理 Proxy 实现类:spring采用 cglib字节码增强 AOP术语【掌握】 target:目标类,需要被代理的类。例如:UserService

AspectJ - Load-time weaving, privileged aspect and pointcut on private method of JRE class

老子叫甜甜 提交于 2020-01-17 09:29:46
问题 I'm trying to set pointcut on private method java.net.AbstractSocketImpl.connectToAddress(..) and I want to use load-time weaving. This is my test code: public class Main { public static void main(String args[]) throws Throwable { new java.net.Socket("localhost", 10111); } } and this is my privileged aspect: privileged aspect PrivAspect { before() : call(* java.net.AbstractPlainSocketImpl.connectToAddress(..)) { System.out.println("It works"); } } and this is META-INF/aop.xml <aspectj>

Spring AspectJ loadtimeweaving not invoked

元气小坏坏 提交于 2020-01-17 06:42:35
问题 The Spring AspectJ loadtime weaving configuration is building and loading server without any errors, but the aspect is not getting invoked. Here is the list of configuration 1) JDK 8 2) Server Jetty @Configuration @ComponentScan(basePackages = {..}) @EnableSpringConfigured @EnableLoadTimeWeaving(aspectjWeaving=AspectJWeaving.ENABLED) @PropertySource(...) @ImportResource(value = { "classpath:META-INF/aop.xml", ..}) class config { ... } aop.xml <aspectj> <weaver options="-Xlint:ignore -Xset

AspectJ pointcuts and advice

我的梦境 提交于 2020-01-17 03:16:04
问题 I have to enforce a policy issuing a warning if items not belonging to a particular category are being added, apart from the three which are allowed and disallowing such additions..... So far i am able to find the items and issue warning.... but not sure how to stop them from being added.... For Eg. Allowed categories Shoes and socks but if i try and add a vegetable item to the inventory it should give me a warning saying "category not allowed../nItem will not be added to inventory"..... and

How to write an aspect or annotation for a method that cannot update any instance variables?

て烟熏妆下的殇ゞ 提交于 2020-01-16 19:01:37
问题 How to write an aspect or annotation for a method that cannot update any instance variables? Say for example I have the following java class public class Foo { String name; int id; public getName() { return name; } } Now say I want to write an aspect or in general, some annotation call it say @readOnly which can enforce getName() method to not modify either name or id so if do public class Foo { String name; int id; @readOnly public getName() { name = "hello world"; id = 7564; return name; }

How to write an aspect or annotation for a method that cannot update any instance variables?

半城伤御伤魂 提交于 2020-01-16 19:00:10
问题 How to write an aspect or annotation for a method that cannot update any instance variables? Say for example I have the following java class public class Foo { String name; int id; public getName() { return name; } } Now say I want to write an aspect or in general, some annotation call it say @readOnly which can enforce getName() method to not modify either name or id so if do public class Foo { String name; int id; @readOnly public getName() { name = "hello world"; id = 7564; return name; }

Help with pointcut - AspectJ

假如想象 提交于 2020-01-16 14:03:19
问题 I'm just a bit confused with the parameters in a pointcut would appreciate if anyone could explain it to me... import Java.util.logging.*; import org.aspect j.lang.*; public aspect TraceAspect { private Logger _logger = Logger.getLogger("trace"); TraceAspectV2() { _logger.setLevel(Level.ALL); } pointcut traceMethods() (execution(* Account.*(..)) || execution(*.new(..))) && !within(TraceAspect); before () : traceMethods() { if (_logger.isLoggable(Level.INFO)) { Signature sig =

Capture setters inside mapper method using AspectJ in Spring

不打扰是莪最后的温柔 提交于 2020-01-16 08:58:35
问题 I have java classes like this : @Data public class Lead { private A a; ... } @Data public class A { private B b; private String c; private List<Integer> d; } @Data public class B { private String e; private String f; } I have a mapper method with annotation like this : @FieldPermissionAnnotation("a") public A fetchA(//Some DB Entities) { A a = new A(); ... a.setB(fetchB()); ... a.setC(fetchC()); ... a.setD(fetchD()); } My FieldPermissionAspect fetches the permission-field mapping from db for

AspectJ - Static inter-type declaration on classes implementing a given interface

醉酒当歌 提交于 2020-01-15 11:55:06
问题 I would like to know if it is possible (and how if it is...) to make a static inter-type declaration that works on all classes that implements a given interface. In my use case, I have an empty interface: public interface Delegate {} and two classes implementing it: public class DelegateA implements Delegate {...} public class DelegateB implements Delegate {...} And I want an aspect to declare a static member on DelegateA and DelegateB... and all the future classes that will implements my

Conditional behavior of Spring-AOP Before Advice

南楼画角 提交于 2020-01-15 10:13:16
问题 I'm a little new to AOP, and got confused about the problem I'm facing. I have the Annotation @AuthorizeUser which acts on methods, on Presentation Layer. I need to check if User is authorized to execute that method or not. Here is the code for AuthorizeUserAspect : @Aspect public class AuthorizeUserAspect { @AuthoWired private UserService service; @Before(value = "@annotation(com.company.annotation.AuthorizeUser)") public void isAuthorized(JoinPoint jp) { // Check if the user has permission