【SpringBoot】SpringBoot自定义@EnableXX

SpringBoot自定义@EnableXX

在Springboot框架中,有很多类似于@EnableXX的注解,比如@EnableAsync,@EnableScheduling等等,
一个简单的注解即可实现功能相应功能的引入,这么神奇吗?那我们该怎么自定义@Enable注解,来使用这些骚操作呢?

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import({AutoConfigurationImportSelector.class})
public @interface EnableAutoConfiguration {
    String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration";

    Class<?>[] exclude() default {};

    String[] excludeName() default {};
}

我们可以看到关键的注解 @Import() 引入了一个配置类,那我们不妨按照他的思路去实现。

代码实现

实现思路

通过UserConfig配置将user 注入bean,然后启动的时候去获取bean。
在不加注解的情况下 ,不会获取到Bean,加了注解可以获取到Bean。

以下是我的code

User.java

public class User {

}

UserConfig.java

注意:这里不要在配置类上加 @configration注解哦

public class UserConfig {

    @Bean
    public User userBean(){
        return new User();
    }

}

EnableUser.java

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import({UserConfig.class})
public @interface EnableUser {

}

测试一下下子

咱就在启动类上进行修改吧

@EnableUser
@SpringBootApplication
public class BootEnableApplication {
    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(BootEnableApplication.class, args);
        Object user = context.getBean("userBean");
        System.out.println(user);
    }

}

未加注解的情况

输出结果:

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.1.RELEASE)

2023-06-30 17:38:00.629  INFO 14444 --- [           main] xy.boot.enable.BootEnableApplication     : Starting BootEnableApplication on ZJZL-20210402GJ with PID 14444 (F:\idea-project\xy-integration-middleware\boot\boot-enable\target\classes started by Administrator in F:\idea-project\xy-integration-middleware)
2023-06-30 17:38:00.635  INFO 14444 --- [           main] xy.boot.enable.BootEnableApplication     : No active profile set, falling back to default profiles: default
2023-06-30 17:38:01.922  INFO 14444 --- [           main] xy.boot.enable.BootEnableApplication     : Started BootEnableApplication in 1.961 seconds (JVM running for 4.699)
Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'userBean' available
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:772)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1221)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:294)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
	at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1083)
	at xy.boot.enable.BootEnableApplication.main(BootEnableApplication.java:16)

Process finished with exit code 1

加上注解 @EnableUser

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.1.RELEASE)

2023-06-30 17:26:23.773  INFO 7268 --- [           main] xy.boot.enable.BootEnableApplication     : Starting BootEnableApplication on ZJZL-20210402GJ with PID 7268 (F:\idea-project\xy-integration-middleware\boot\boot-enable\target\classes started by Administrator in F:\idea-project\xy-integration-middleware)
2023-06-30 17:26:23.786  INFO 7268 --- [           main] xy.boot.enable.BootEnableApplication     : No active profile set, falling back to default profiles: default
2023-06-30 17:26:25.560  INFO 7268 --- [           main] xy.boot.enable.BootEnableApplication     : Started BootEnableApplication in 2.983 seconds (JVM running for 8.185)
xy.boot.enable.entity.User@672f11c2
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值