ImportAware

/**
 * Interface to be implemented by any @{@link Configuration} class that wishes
 * to be injected with the {@link AnnotationMetadata} of the @{@code Configuration}
 * class that imported it. Useful in conjunction with annotations that
 * use @{@link Import} as a meta-annotation.
 *
 * @author Chris Beams
 * @since 3.1
 */
public interface ImportAware extends Aware {

    /**
     * Set the annotation metadata of the importing @{@code Configuration} class.
     */
    void setImportMetadata(AnnotationMetadata importMetadata);

}

 

这个类注释不容易翻译,比较拗口,就是说白了实现这个接口,重写setImportMetadata方法,就是用来处理自定义注解的,比如将注解的某些属性值赋值给其他bean的属性。

 

转载于:https://www.cnblogs.com/yangfei-beijing/p/9047703.html

在 Spring Boot 中,你可以使用 `AspectJAutoProxyRegistrar` 类来实现在运行时动态注册切面。下面是一种实现方式: 1. 首先,在 Spring Boot 的主配置类上添加 `@EnableAspectJAutoProxy` 注解,启用 AspectJ 自动代理功能。 ```java import org.springframework.context.annotation.EnableAspectJAutoProxy; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @EnableAspectJAutoProxy public class YourApplication { public static void main(String[] args) { SpringApplication.run(YourApplication.class, args); } } ``` 2. 创建一个切面类,实现你的业务逻辑。例如,创建一个名为 `CustomAspect` 的类。 ```java import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.springframework.stereotype.Component; @Aspect @Component public class CustomAspect { @Before("execution(* com.example.demo.service.YourService.*(..))") public void beforeMethod() { // 在这里编写你的切面逻辑 // 例如,在目标方法执行前执行某些操作 } } ``` 3. 在运行时动态注册切面,可以通过编程的方式将切面类添加到 Spring 的 Bean 容器中。 ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.ImportAware; import org.springframework.core.annotation.AnnotationAttributes; import org.springframework.core.type.AnnotationMetadata; @Configuration public class AspectJAutoProxyRegistrar implements ImportAware { private AnnotationAttributes enableAspectJAutoProxy; @Override public void setImportMetadata(AnnotationMetadata importMetadata) { this.enableAspectJAutoProxy = AnnotationAttributes.fromMap( importMetadata.getAnnotationAttributes(EnableAspectJAutoProxy.class.getName(), false)); } @Autowired private void registerBeanPostProcessors(ConfigurableListableBeanFactory beanFactory) { // 创建切面类的实例 CustomAspect customAspect = new CustomAspect(); // 注册切面类的 BeanDefinition BeanDefinitionRegistry beanDefinitionRegistry = (BeanDefinitionRegistry) beanFactory; beanDefinitionRegistry.registerBeanDefinition(CustomAspect.class.getName(), BeanDefinitionBuilder.genericBeanDefinition(CustomAspect.class, () -> customAspect).getBeanDefinition()); // 设置切面类的相关属性 AutowireCapableBeanFactory autowireCapableBeanFactory = beanFactory.getAutowireCapableBeanFactory(); if (autowireCapableBeanFactory instanceof DefaultListableBeanFactory) { ((DefaultListableBeanFactory) autowireCapableBeanFactory) .registerResolvableDependency(CustomAspect.class, customAspect); } } } ``` 请根据你的实际情况修改上述代码,并将 `CustomAspect` 替换为你自己的切面类。这样,在运行时启动 Spring Boot 应用程序时,切面类将被动态注册到 Spring 的 Bean 容器中,并且会自动应用到符合切点表达式的目标方法上。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值