spring-tx源码分析(2)_Import注解+ServiceLog进阶

在 spring-tx源码分析(1)_Import注解 中,了解到Import注解、PointcutAdvisor的基础使用,并且编写了一个ServiceLog的示例程序,本文将继续优化这个程序:

  • 支持指定包扫描,指定包下面被ServiceLog注解标注的方法才会进行拦截
  • 兼容spring的原生aop

代码优化

概述

为了实现上面的功能,需要对之前的示例代码进行修改:

  • EnableServiceLog注解增加一个字段,用于指定扫描的包名
  • Pointcut实现类需要在matches方法中对比目标方法所在包是否与EnableServiceLog注解指定的包名匹配
  • 使用ImportBeanDefinitionRegistrar将Pointcut实现类注入到容器,以便后续的装配

EnableServiceLog修改

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import({ServiceLogConfigurationSelector.class, ServiceLogScannerRegistrar.class})
public @interface EnableServiceLog {

  String basePackage() default "";
}

可以看到:

  • 增加了basePackage参数
  • Import了ServiceLogConfigurationSelector和ServiceLogScannerRegistrar两个组件。ServiceLogConfigurationSelector用于导入ServiceLogConfiguration来注入ServiceLogAdviser对象,这个之前就存在。ServiceLogScannerRegistrar用于在BeanDefinition阶段注入Pointcut Bean定义,这个后续会介绍

ServiceLogPointcut修改

public class ServiceLogPointcut extends StaticMethodMatcherPointcut {

  private String basePackage;

  public ServiceLogPointcut(String basePackage) {
    this.basePackage = basePackage;
  }

  @Override
  public boolean matches(Method method, Class<?> targetClass) {
    Method specificMethod = AopUtils.getMostSpecificMethod(method, targetClass);
    Class<?> clazz = specificMethod.getDeclaringClass();
    if (!clazz.getName().startsWith(this.basePackage)) {
      return false;
    }
    return specificMethod.getAnnotation(ServiceLog.class) != null;
  }
}

matches方法做了修改,首先匹配目标Class与指定包名,然后再判断方法是否被标注了ServiceLog注解。

ServiceLogScannerRegistrar

这个类实现了ImportBeanDefinitionRegistrar接口,是Import注解支持导入的三种组件的一种。

在实现方法里面,将向BeanDefinitionRegistry中注入ServiceLogPointcut Bean定义:

public class ServiceLogScannerRegistrar implements ImportBeanDefinitionRegistrar {

  @Override
  public void registerBeanDefinitions(
      AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {

    Map<String, Object> attributes = importingClassMetadata
        .getAnnotationAttributes(EnableServiceLog.class.getName());

    GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
    beanDefinition.setBeanClassName("serviceLogPointcut");
    beanDefinition.setBeanClass(ServiceLogPointcut.class);

    ConstructorArgumentValues constructorArgumentValues = new ConstructorArgumentValues();
    constructorArgumentValues.addGenericArgumentValue(attributes.get("basePackage"));
    beanDefinition.setConstructorArgumentValues(constructorArgumentValues);

    registry.registerBeanDefinition("serviceLogPointcut", beanDefinition);
  }
}

示例程序

  • 编写Configuration类,标注EnableServiceLog注解,指定basePackage
  • 使用EnableAspectJAutoProxy注解开启aop代理支持
  • 编写业务代码,使用ServiceLog注解标注业务方法

示例程序下载

https://download.csdn.net/download/xuguofeng2016/86727144

1、Spring配置文件 Spring配置文件是Spring框架中非常重要的一部分,它通常以XML格式编写,用于配置Spring应用程序中的各种组件,例如Bean、AOP、数据源、事务等。 在Spring配置文件中,最常用的标签是<bean>标签,用于定义和配置Spring IoC容器中的Bean对象。除此之外,还有<import>标签,用于引入其他配置文件;<aop:config>标签,用于配置AOP相关的切面和通知等;<tx:advice>标签,用于配置事务管理相关的通知等。 Spring配置文件的编写需要遵守一定的规范和约束条件,例如必须指定命名空间、必须定义命名空间的schema等。同时,Spring还提供了多种加载配置文件的方式,例如ClassPathXmlApplicationContext、FileSystemXmlApplicationContext等。 2、Spring IoC基于注解的操作和案例 除了使用XML配置文件之外,Spring IoC容器还支持基于注解的Bean定义和注入操作。在Spring中,使用注解可以大大简化配置文件的编写,提高开发效率和可读性。 常用的Spring注解包括: - @Component:用于标识一个组件,通常与@Autowired等注解一起使用。 - @Autowired:用于自动注入一个Bean对象。 - @Qualifier:用于指定一个Bean对象的名称。 - @Value:用于注入一个基本类型或String类型的属性值。 - @Configuration:用于标识一个配置类,通常与@Bean等注解一起使用。 - @Bean:用于定义一个Bean对象,通常用于@Configuration类中。 - @Profile:用于指定一个Bean对象的环境依赖。 下面是一个基于注解Spring IoC配置案例: ``` @Configuration public class AppConfig { @Bean public UserService userService() { return new UserServiceImpl(); } } ``` 在这个案例中,使用@Configuration注解表示这是一个配置类,使用@Bean注解表示定义了一个名为“userService”的Bean对象。该Bean对象的类型是UserServiceImpl。 另外,还可以使用@Autowired和@Qualifier注解来实现Bean的自动注入。例如: ``` @Service public class UserServiceImpl implements UserService { @Autowired @Qualifier("userRepository") private UserRepository userRepository; // ... } ``` 在这个案例中,使用@Service注解表示这是一个服务类,使用@Autowired注解表示自动注入一个名为“userRepository”的Bean对象。其中,@Qualifier注解用于指定Bean对象的名称。 总之,Spring配置文件和基于注解的操作是Spring框架中非常重要和常用的组件,它们为Java开发人员提供了一种高效、灵活和可维护的方式来管理对象和依赖关系。通过基于注解的方式,可以大大简化配置文件的编写工作,提高开发效率和可读性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值