org.springframework.aop.support.annotation.AnnotationMatchingPointcut Java Examples

The following examples show how to use org.springframework.aop.support.annotation.AnnotationMatchingPointcut. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ReactivePersistenceExceptionTranslationPostProcessor.java    From sdn-rx with Apache License 2.0 6 votes vote down vote up
/**
 * Create a new PersistenceExceptionTranslationAdvisor.
 *
 * @param beanFactory              the ListableBeanFactory to obtaining all
 *                                 PersistenceExceptionTranslators from
 * @param repositoryAnnotationType the annotation type to check for
 */
ReactivePersistenceExceptionTranslationAdvisor(ListableBeanFactory beanFactory,
	Class<? extends Annotation> repositoryAnnotationType) {

	this.advice = new ReactivePersistenceExceptionTranslationInterceptor(beanFactory);
	this.pointcut = new AnnotationMatchingPointcut(repositoryAnnotationType, true) {
		@Override
		public MethodMatcher getMethodMatcher() {
			return new StaticMethodMatcher() {

				@Override public boolean matches(Method method, Class<?> targetClass) {
					Class<?> returnType = method.getReturnType();
					return returnType == Mono.class || returnType == Flux.class;
				}
			};
		}
	};
}
 
Example #2
Source File: CglibProxyControllerTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@SuppressWarnings("serial")
private void initServlet(final Class<?> controllerClass) throws ServletException {
	servlet = new DispatcherServlet() {
		@Override
		protected WebApplicationContext createWebApplicationContext(@Nullable WebApplicationContext parent) {
			GenericWebApplicationContext wac = new GenericWebApplicationContext();
			wac.registerBeanDefinition("controller", new RootBeanDefinition(controllerClass));
			DefaultAdvisorAutoProxyCreator autoProxyCreator = new DefaultAdvisorAutoProxyCreator();
			autoProxyCreator.setProxyTargetClass(true);
			autoProxyCreator.setBeanFactory(wac.getBeanFactory());
			wac.getBeanFactory().addBeanPostProcessor(autoProxyCreator);
			Pointcut pointcut = new AnnotationMatchingPointcut(Controller.class);
			DefaultPointcutAdvisor advisor = new DefaultPointcutAdvisor(pointcut, new SimpleTraceInterceptor(true));
			wac.getBeanFactory().registerSingleton("advisor", advisor);
			wac.refresh();
			return wac;
		}
	};
	servlet.init(new MockServletConfig());
}
 
Example #3
Source File: CglibProxyControllerTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@SuppressWarnings("serial")
private void initServlet(final Class<?> controllerClass) throws ServletException {
	servlet = new DispatcherServlet() {
		@Override
		protected WebApplicationContext createWebApplicationContext(@Nullable WebApplicationContext parent) {
			GenericWebApplicationContext wac = new GenericWebApplicationContext();
			wac.registerBeanDefinition("controller", new RootBeanDefinition(controllerClass));
			DefaultAdvisorAutoProxyCreator autoProxyCreator = new DefaultAdvisorAutoProxyCreator();
			autoProxyCreator.setProxyTargetClass(true);
			autoProxyCreator.setBeanFactory(wac.getBeanFactory());
			wac.getBeanFactory().addBeanPostProcessor(autoProxyCreator);
			Pointcut pointcut = new AnnotationMatchingPointcut(Controller.class);
			DefaultPointcutAdvisor advisor = new DefaultPointcutAdvisor(pointcut, new SimpleTraceInterceptor(true));
			wac.getBeanFactory().registerSingleton("advisor", advisor);
			wac.refresh();
			return wac;
		}
	};
	servlet.init(new MockServletConfig());
}
 
Example #4
Source File: ApiBootDataSourceSwitchAdvisor.java    From beihu-boot with Apache License 2.0 5 votes vote down vote up
/**
 * build pointcut instance
 */
private Pointcut buildPointcut() {
    // class
    Pointcut cpc = new AnnotationMatchingPointcut(DataSourceSwitch.class, true);
    // method
    Pointcut mpc = AnnotationMatchingPointcut.forMethodAnnotation(DataSourceSwitch.class);

    //union
    ComposablePointcut pointcut = new ComposablePointcut(cpc);
    return pointcut.union(mpc);
}
 
Example #5
Source File: JavaMelodyAutoConfiguration.java    From cat-boot with Apache License 2.0 5 votes vote down vote up
@ConditionalOnProperty(name = "javamelody.enableSpringControllerMonitoring", havingValue = "true")
@Bean
public MonitoringSpringAdvisor springControllerMonitoringAdvisor() {
    final MonitoringSpringAdvisor interceptor = new MonitoringSpringAdvisor();
    interceptor.setPointcut(new AnnotationMatchingPointcut(Controller.class));
    return interceptor;
}
 
Example #6
Source File: JavaMelodyAutoConfiguration.java    From cat-boot with Apache License 2.0 5 votes vote down vote up
@ConditionalOnProperty(name = "javamelody.enableSpringServiceMonitoring", havingValue = "true")
@Bean
public MonitoringSpringAdvisor springServiceMonitoringAdvisor() {
    final MonitoringSpringAdvisor interceptor = new MonitoringSpringAdvisor();
    interceptor.setPointcut(new AnnotationMatchingPointcut(Service.class));
    return interceptor;
}
 
Example #7
Source File: PersistenceExceptionTranslationAdvisor.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Create a new PersistenceExceptionTranslationAdvisor.
 * @param beanFactory the ListableBeanFactory to obtaining all
 * PersistenceExceptionTranslators from
 * @param repositoryAnnotationType the annotation type to check for
 */
PersistenceExceptionTranslationAdvisor(
		ListableBeanFactory beanFactory, Class<? extends Annotation> repositoryAnnotationType) {

	this.advice = new PersistenceExceptionTranslationInterceptor(beanFactory);
	this.pointcut = new AnnotationMatchingPointcut(repositoryAnnotationType, true);
}
 
Example #8
Source File: PersistenceExceptionTranslationAdvisor.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Create a new PersistenceExceptionTranslationAdvisor.
 * @param persistenceExceptionTranslator the PersistenceExceptionTranslator to use
 * @param repositoryAnnotationType the annotation type to check for
 */
public PersistenceExceptionTranslationAdvisor(
		PersistenceExceptionTranslator persistenceExceptionTranslator,
		Class<? extends Annotation> repositoryAnnotationType) {

	this.advice = new PersistenceExceptionTranslationInterceptor(persistenceExceptionTranslator);
	this.pointcut = new AnnotationMatchingPointcut(repositoryAnnotationType, true);
}
 
Example #9
Source File: ShardingTransactionTypeAdvisor.java    From shardingsphere with Apache License 2.0 5 votes vote down vote up
ShardingTransactionTypeAdvisor() {
    Pointcut classPointcut = new ComposablePointcut(AnnotationMatchingPointcut.forClassAnnotation(ShardingTransactionType.class));
    Pointcut methodPointcut = new ComposablePointcut(AnnotationMatchingPointcut.forMethodAnnotation(ShardingTransactionType.class));
    transactionTypePointcut = new ComposablePointcut(classPointcut).union(methodPointcut);
    transactionTypeInterceptor = new ShardingTransactionTypeInterceptor();
    setOrder(Ordered.LOWEST_PRECEDENCE - 1);
}
 
Example #10
Source File: AsyncAnnotationAdvisor.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Calculate a pointcut for the given async annotation types, if any.
 * @param asyncAnnotationTypes the async annotation types to introspect
 * @return the applicable Pointcut object, or {@code null} if none
 */
protected Pointcut buildPointcut(Set<Class<? extends Annotation>> asyncAnnotationTypes) {
	ComposablePointcut result = null;
	for (Class<? extends Annotation> asyncAnnotationType : asyncAnnotationTypes) {
		Pointcut cpc = new AnnotationMatchingPointcut(asyncAnnotationType, true);
		Pointcut mpc = AnnotationMatchingPointcut.forMethodAnnotation(asyncAnnotationType);
		if (result == null) {
			result = new ComposablePointcut(cpc).union(mpc);
		}
		else {
			result.union(cpc).union(mpc);
		}
	}
	return result;
}
 
Example #11
Source File: PersistenceExceptionTranslationAdvisor.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create a new PersistenceExceptionTranslationAdvisor.
 * @param beanFactory the ListableBeanFactory to obtaining all
 * PersistenceExceptionTranslators from
 * @param repositoryAnnotationType the annotation type to check for
 */
PersistenceExceptionTranslationAdvisor(
		ListableBeanFactory beanFactory, Class<? extends Annotation> repositoryAnnotationType) {

	this.advice = new PersistenceExceptionTranslationInterceptor(beanFactory);
	this.pointcut = new AnnotationMatchingPointcut(repositoryAnnotationType, true);
}
 
Example #12
Source File: PersistenceExceptionTranslationAdvisor.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create a new PersistenceExceptionTranslationAdvisor.
 * @param persistenceExceptionTranslator the PersistenceExceptionTranslator to use
 * @param repositoryAnnotationType the annotation type to check for
 */
public PersistenceExceptionTranslationAdvisor(
		PersistenceExceptionTranslator persistenceExceptionTranslator,
		Class<? extends Annotation> repositoryAnnotationType) {

	this.advice = new PersistenceExceptionTranslationInterceptor(persistenceExceptionTranslator);
	this.pointcut = new AnnotationMatchingPointcut(repositoryAnnotationType, true);
}
 
Example #13
Source File: CubaMethodValidationPostProcessor.java    From cuba with Apache License 2.0 5 votes vote down vote up
@Override
public void afterPropertiesSet() {
    Pointcut pointcut = new AnnotationMatchingPointcut(Service.class, Validated.class, true);
    DefaultPointcutAdvisor advisor = new DefaultPointcutAdvisor(pointcut, createMethodValidationAdvice(this.beanValidation));
    advisor.setOrder(2);
    this.advisor = advisor;
}
 
Example #14
Source File: LockBeanPostProcessor.java    From distributed-lock with MIT License 5 votes vote down vote up
@Override
public void afterPropertiesSet() {
  final var pointcut = new AnnotationMatchingPointcut(null, Locked.class, true);
  final var interceptor = new LockMethodInterceptor(keyGenerator, lockTypeResolver, intervalConverter, retriableLockFactory, taskScheduler);

  this.advisor = new DefaultPointcutAdvisor(pointcut, interceptor);
}
 
Example #15
Source File: MethodProxyScheduledLockAdvisor.java    From ShedLock with Apache License 2.0 5 votes vote down vote up
@NonNull
private static AnnotationMatchingPointcut methodPointcutFor(Class<? extends Annotation> methodAnnotationType) {
    return new AnnotationMatchingPointcut(
        null,
        methodAnnotationType,
        true
    );
}
 
Example #16
Source File: ApiBootDataSourceSwitchAdvisor.java    From api-boot with Apache License 2.0 5 votes vote down vote up
/**
 * build pointcut instance
 * use {@link DataSourceSwitch} as a {@link Pointcut}
 * scope:
 * 1. {@link DataSourceSwitch} on the class
 * 2. {@link DataSourceSwitch} on the method
 */
private Pointcut buildPointcut() {
    // class
    Pointcut cpc = new AnnotationMatchingPointcut(DataSourceSwitch.class, true);
    // method
    Pointcut mpc = AnnotationMatchingPointcut.forMethodAnnotation(DataSourceSwitch.class);

    //union
    ComposablePointcut pointcut = new ComposablePointcut(cpc);
    return pointcut.union(mpc);
}
 
Example #17
Source File: JavaMelodyAutoConfiguration.java    From javamelody with Apache License 2.0 5 votes vote down vote up
/**
 * Monitoring of beans having the {@link Service} annotation.
 * @return MonitoringSpringAdvisor
 */
@Bean
@ConditionalOnProperty(prefix = JavaMelodyConfigurationProperties.PREFIX, name = "spring-monitoring-enabled", matchIfMissing = true)
public MonitoringSpringAdvisor monitoringSpringServiceAdvisor() {
	return createMonitoringSpringAdvisorWithExclusions(
			new AnnotationMatchingPointcut(Service.class),
			monitoredWithSpringAnnotationPointcut, asyncAnnotationPointcut,
			scheduledAnnotationPointcut);
}
 
Example #18
Source File: JavaMelodyAutoConfiguration.java    From javamelody with Apache License 2.0 5 votes vote down vote up
/**
 * Monitoring of beans having the {@link Controller} annotation.
 * @return MonitoringSpringAdvisor
 */
@Bean
@ConditionalOnProperty(prefix = JavaMelodyConfigurationProperties.PREFIX, name = "spring-monitoring-enabled", matchIfMissing = true)
public MonitoringSpringAdvisor monitoringSpringControllerAdvisor() {
	return createMonitoringSpringAdvisorWithExclusions(
			new AnnotationMatchingPointcut(Controller.class),
			monitoredWithSpringAnnotationPointcut, asyncAnnotationPointcut,
			scheduledAnnotationPointcut);
}
 
Example #19
Source File: PersistenceExceptionTranslationAdvisor.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Create a new PersistenceExceptionTranslationAdvisor.
 * @param beanFactory the ListableBeanFactory to obtaining all
 * PersistenceExceptionTranslators from
 * @param repositoryAnnotationType the annotation type to check for
 */
PersistenceExceptionTranslationAdvisor(
		ListableBeanFactory beanFactory, Class<? extends Annotation> repositoryAnnotationType) {

	this.advice = new PersistenceExceptionTranslationInterceptor(beanFactory);
	this.pointcut = new AnnotationMatchingPointcut(repositoryAnnotationType, true);
}
 
Example #20
Source File: PersistenceExceptionTranslationAdvisor.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Create a new PersistenceExceptionTranslationAdvisor.
 * @param persistenceExceptionTranslator the PersistenceExceptionTranslator to use
 * @param repositoryAnnotationType the annotation type to check for
 */
public PersistenceExceptionTranslationAdvisor(
		PersistenceExceptionTranslator persistenceExceptionTranslator,
		Class<? extends Annotation> repositoryAnnotationType) {

	this.advice = new PersistenceExceptionTranslationInterceptor(persistenceExceptionTranslator);
	this.pointcut = new AnnotationMatchingPointcut(repositoryAnnotationType, true);
}
 
Example #21
Source File: JavaMelodyAutoConfiguration.java    From javamelody with Apache License 2.0 5 votes vote down vote up
/**
 * Monitoring of beans having the {@link RestController} annotation.
 * @return MonitoringSpringAdvisor
 */
@Bean
@ConditionalOnProperty(prefix = JavaMelodyConfigurationProperties.PREFIX, name = "spring-monitoring-enabled", matchIfMissing = true)
public MonitoringSpringAdvisor monitoringSpringRestControllerAdvisor() {
	return createMonitoringSpringAdvisorWithExclusions(
			new AnnotationMatchingPointcut(RestController.class),
			monitoredWithSpringAnnotationPointcut, asyncAnnotationPointcut,
			scheduledAnnotationPointcut);
}
 
Example #22
Source File: PersistenceExceptionTranslationAdvisor.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Create a new PersistenceExceptionTranslationAdvisor.
 * @param beanFactory the ListableBeanFactory to obtaining all
 * PersistenceExceptionTranslators from
 * @param repositoryAnnotationType the annotation type to check for
 */
PersistenceExceptionTranslationAdvisor(
		ListableBeanFactory beanFactory, Class<? extends Annotation> repositoryAnnotationType) {

	this.advice = new PersistenceExceptionTranslationInterceptor(beanFactory);
	this.pointcut = new AnnotationMatchingPointcut(repositoryAnnotationType, true);
}
 
Example #23
Source File: PersistenceExceptionTranslationAdvisor.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Create a new PersistenceExceptionTranslationAdvisor.
 * @param persistenceExceptionTranslator the PersistenceExceptionTranslator to use
 * @param repositoryAnnotationType the annotation type to check for
 */
public PersistenceExceptionTranslationAdvisor(
		PersistenceExceptionTranslator persistenceExceptionTranslator,
		Class<? extends Annotation> repositoryAnnotationType) {

	this.advice = new PersistenceExceptionTranslationInterceptor(persistenceExceptionTranslator);
	this.pointcut = new AnnotationMatchingPointcut(repositoryAnnotationType, true);
}
 
Example #24
Source File: Proxys.java    From onetwo with Apache License 2.0 4 votes vote down vote up
public static <T> T intercept(Object target, Class<? extends Annotation> annoClass, MethodInterceptor methodInterceptor){
	return intercept(target, ()->AnnotationMatchingPointcut.forMethodAnnotation(annoClass), methodInterceptor);
}
 
Example #25
Source File: JavaMelodyAutoConfiguration.java    From javamelody with Apache License 2.0 4 votes vote down vote up
/**
 * Monitoring of Feign clients.
 * @return MonitoringSpringAdvisor
 * @throws ClassNotFoundException should not happen
 */
@SuppressWarnings("unchecked")
@Bean
@ConditionalOnClass(name = "org.springframework.cloud.openfeign.FeignClient")
@ConditionalOnProperty(prefix = JavaMelodyConfigurationProperties.PREFIX, name = "spring-monitoring-enabled", matchIfMissing = true)
// we check that the DefaultAdvisorAutoProxyCreator above is not enabled, because if it's enabled then feign calls are counted twice
@ConditionalOnMissingBean(DefaultAdvisorAutoProxyCreator.class)
public MonitoringSpringAdvisor monitoringFeignClientAdvisor() throws ClassNotFoundException {
	final Class<? extends Annotation> feignClientClass = (Class<? extends Annotation>) Class
			.forName("org.springframework.cloud.openfeign.FeignClient");
	final MonitoringSpringAdvisor advisor = new MonitoringSpringAdvisor(
			new AnnotationMatchingPointcut(feignClientClass, true));
	advisor.setAdvice(new MonitoringSpringInterceptor() {
		private static final long serialVersionUID = 1L;

		@Override
		protected String getRequestName(MethodInvocation invocation) {
			final StringBuilder sb = new StringBuilder();
			final Method method = invocation.getMethod();
			final RequestMapping requestMapping = method.getAnnotation(RequestMapping.class);
			if (requestMapping != null) {
				String[] path = requestMapping.value();
				if (path.length == 0) {
					path = requestMapping.path();
				}
				if (path.length > 0) {
					sb.append(path[0]);
					sb.append(' ');
					if (requestMapping.method().length > 0) {
						sb.append(requestMapping.method()[0].name());
					} else {
						sb.append("GET");
					}
					sb.append('\n');
				}
			}
			final Class<?> declaringClass = method.getDeclaringClass();
			final String classPart = declaringClass.getSimpleName();
			final String methodPart = method.getName();
			sb.append(classPart).append('.').append(methodPart);
			return sb.toString();
		}
	});
	return advisor;
}
 
Example #26
Source File: MethodAnnotationPointcutAdvisor.java    From AutoLoadCache with Apache License 2.0 4 votes vote down vote up
public MethodAnnotationPointcutAdvisor(Class<? extends Annotation> methodAnnotationType, Advice advice) {
    this.pointcut = new AnnotationMatchingPointcut(null, methodAnnotationType);
    this.advice = advice;
}
 
Example #27
Source File: MethodValidationPostProcessor.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Override
public void afterPropertiesSet() {
	Pointcut pointcut = new AnnotationMatchingPointcut(this.validatedAnnotationType, true);
	this.advisor = new DefaultPointcutAdvisor(pointcut, createMethodValidationAdvice(this.validator));
}
 
Example #28
Source File: MethodValidationPostProcessor.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void afterPropertiesSet() {
	Pointcut pointcut = new AnnotationMatchingPointcut(this.validatedAnnotationType, true);
	this.advisor = new DefaultPointcutAdvisor(pointcut, createMethodValidationAdvice(this.validator));
}
 
Example #29
Source File: MethodLockConfiguration.java    From common-project with Apache License 2.0 4 votes vote down vote up
@Bean
public Pointcut pointcut(){
    return new AnnotationMatchingPointcut(null,MethodLock.class);
}
 
Example #30
Source File: ApiBootMessagePushClientSwitchAdvisor.java    From api-boot with Apache License 2.0 4 votes vote down vote up
/**
 * build pointcut instance
 */
private Pointcut buildPointcut() {
    // method
    Pointcut mpc = AnnotationMatchingPointcut.forMethodAnnotation(MessagePushSwitch.class);
    return mpc;
}