aspectj

3、Spring-包扫描注解@ComponentScan

我们两清 提交于 2019-12-01 21:29:23
在一个项目开发中,一般会把项目分为 1、DAO层(数据访问层):专门负责数据库交互 CRUD(增查改删),比如在mysql数据库里生成一条订单数据。 2、Service层(业务逻辑层) : 负责业务逻辑的处理,比如购买东西的业务,需要调用DAO层的方法扣减商品记录里的库存数量,然后生成一条订单记录。 3、Controller层(控制层) : 接收页面的请求,调用业务逻辑层去处理,将业务逻辑层处理返回的结果返回给页面。 spring中对每一层都提供了相应的注解进行标识: @Repository 数据访问层 @Service 业务逻辑层 @Controller 控制层 spring还提供了 @Component 组件,没有明确的角色,只是标明该类要注册到spring容器中。 spring可以使用 @ComponentScan 注解扫描指定包下标有上面那些注解的类注册到spring容器中。 1、基础使用 下面配置类使用了包扫描注解@ComponentScan,并指定了扫描的包名为com.suzhe.spring.basic.scan, spring启动的时候就会扫描该包下所有的符合条件的类,并注入到spring容器中。 @Configuration //告诉Spring这是一个配置类 //包扫描:如果没有配置则默认扫描为当前类所在包及其子包 @ComponentScan(value =

SpringBoot基础篇AOP之基本使用姿势小结

末鹿安然 提交于 2019-12-01 19:54:35
原文: 190301-SpringBoot基础篇AOP之基本使用姿势小结 一般来讲,谈到Spring的特性,绕不过去的就是DI(依赖注入)和AOP(切面),在将bean的系列中,说了DI的多种使用姿势;接下来看一下AOP的玩法 <!-- more --> I. 背景知识 在实际使用之前有必要了解一下什么是AOP,以及AOP的几个基本概念 1. advice before: 在方法执行之前被调用 after: 在方法执行之后调用 after returning: 方法执行成功之后 after throwing: 方法抛出异常之后 around: 环绕,自己在内部决定方法的执行时机,因此可以在之前之后做一些业务逻辑 2. join point 连接点,比如方法调用,方法执行,字段设置/获取、异常处理执行、类初始化、甚至是 for 循环中的某个点 但 Spring AOP 目前仅支持方法执行 (method execution) 简单来说,Spring AOP中,PointCut就是那个被拦截的方法 3. pointcut 切点,用来描述满足什么规则的方法会被拦截 正则表达式 : @Before("execution(public * com.git.hui.demo.base.bean.*.*(..))") 注解拦截方式 : @Around("@annotation

Spring Aspect not executed when defined in other JAR

给你一囗甜甜゛ 提交于 2019-12-01 17:23:51
I have a project consisting of two subprojects which are both Spring projects and have an applicationContext.xml each. One is a framework project (which ends up as a JAR) and one is the actual application (which ends up as a WAR and depends on the JAR and imports the JAR's applicationContext.xml into it's own applicationContext.xml). In the framework project, I've defined an aspect for all public methods. @Aspect @Configurable public class MyAspect { @Autowired private SomeBean mBean; @Pointcut("execution(public * *(..))") public void anyPublicMethod() { } @Before("anyPublicMethod()") public

No matching factory method found: factory method 'aspectOf()'

不羁岁月 提交于 2019-12-01 16:54:14
问题 I have the following aspect: package trc.suivi.aspects; import java.util.Date; import org.apache.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import trc.suivi.domain.EvenementPli; import trc.suivi.domain.Pli; import trc.suivi.domain.TypeEvenement; import trc.suivi.repository.EvenementPliRepository; public aspect PliEventManagerAspect { private static final Logger log = Logger.getLogger(PliEventManagerAspect.class); @Autowired private EvenementPliRepository

Spring Aspect not executed when defined in other JAR

白昼怎懂夜的黑 提交于 2019-12-01 16:10:51
问题 I have a project consisting of two subprojects which are both Spring projects and have an applicationContext.xml each. One is a framework project (which ends up as a JAR) and one is the actual application (which ends up as a WAR and depends on the JAR and imports the JAR's applicationContext.xml into it's own applicationContext.xml). In the framework project, I've defined an aspect for all public methods. @Aspect @Configurable public class MyAspect { @Autowired private SomeBean mBean;

Using @Autowired with AspectJ and Springboot

可紊 提交于 2019-12-01 15:38:19
I want to use @Autowired annotation into an "Aspect". I want to inject a repository in my aspect but when I try to call a method of my autowired class a NullPointException occurs. @Aspect public class AspectSecurity { @Autowired private UserRepository userRepository; @After("execution(public * dash.*.*Controller.*(..))") public void authentication(JoinPoint jp) throws UnauthorizedException { System.out.println("SECURITY !"); System.out.println(userRepository.findAll().toString()); } } I have already tried to add @Component above my aspect Class but I have the same error. If I don't use an

How to force compile error with aspectJ pointcut mismatch

若如初见. 提交于 2019-12-01 13:19:18
I have the following aspectJ pointcut: @Around(value="execution(* *(*,Map<String, Object>)) && @annotation(com.xxx.annotations.MyCustomAnnotation)") As you can see, this pointcut only matches methods, annotated with com.xxx.annotations.MyCustomAnnotation, which have 2 arguments - the first one is arbitrary and the second one must be of type Map<String, Object> . Is there a way to configure the aspectj-maven-plugin to force compilation errors if it find methods that are annotated with com.xxx.annotations.MyCustomAnnotation, but don't match the signature * *(*,Map<String, Object>) ? Or in other

How to get the caller method information from Around advise

那年仲夏 提交于 2019-12-01 12:32:18
ThisJoinPoint can only get the current method information, anyway to get the caller method information? You can try the special variable thisEnclosingJoinPointStaticPart which holds the static part of the enclosing JoinPoint. Mentioned here (example) and here (docs) Or if using annotation-based AspectJ, pass following to the advice method's parameters, e.g.: @Before("call( /* your pointcut definition */ )") public void myCall(JoinPoint.EnclosingStaticPart thisEnclosingJoinPointStaticPart) { // ... } Mentioned here @Aspect public class LoggingAspect { @Before(value = "execution(public * findAll

Spring Aspectj @Before all rest method

帅比萌擦擦* 提交于 2019-12-01 12:03:40
Before spring introduce @GetMapping , there only one annotation we care about @RequestMapping , so, this aspect works @Before("within(aa.bb.*.rest..*) && execution(public * *(..)) && @within(org.springframework.web.bind.annotation.RestController) && @annotation(org.springframework.web.bind.annotation.RequestMapping)") But after we can use @GetMapping , @PostMapping , this point not works, but these annotations have a meta annotation @RequestMapping . Is there any way to easily intercept all @RequestMapping / @{Get,Post,Put,Patch,..}Mapping ? wener I found this syntax here works for me!

Hibernate Validator method or constructor validation

房东的猫 提交于 2019-12-01 12:00:58
How can I use Hibernate validator to validate the arguments inside the constructor or method? I want the validation to occur before the ValueObject creation so I can throw an exception and not create the object unless all parameters are valid. Basically I'm trying to use annotations instead of doing something like this if possible: public class ConditionalPerson { private String name; private String surname; private int age; public ConditionalPerson(String name, String surname, int age){ if (name == null || surname == null || age < 1) { throw new IllegalArgumentException(); } this.name = name;