如何使用注释的值初始化bean

问题描述 投票:0回答:1

我有以下注释。

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
@Import(MyBeanInitializer.class)
public @interface MyAnnotation {

String clientType() default "" ;

}

而且我有一个如下所示的Bean初始化程序组件

@Configuration
public class MyBeanInitializer {

@Bean() // trigger this when annoattion's value == "A"
public CommonBean firstBean() {
    return new BeanA;

}

@Bean() // trigger this when annoattion's value == "B"
public CommonBean firstBean() {
    return new BeanB;

}
}

我的BeanA和BeanB的Commoin接口

public interface CommonBean {
void doSomething();
}

我的两个实现是>

@Component()
public class BeanA implements CommonBean {

 @Overrid
 public void doSomething (){
 // implementation here
 }
}



@Component()
public class BeanB implements CommonBean {

 @Overrid
 public void doSomething (){
 // implementation here
 }
}

我需要将上面用作另一个Spring Boot项目的库。在该项目中,我用Application.java注释了@MyAnnotation(clientType="web"),然后使用构造函数Injection将BeanA

BeanB注入该项目内的类。

通过查看通过注释传递的值来初始化bean的机制是什么?

我有以下注释。 @Retention(RetentionPolicy.RUNTIME)@Target({ElementType.TYPE})@Import(MyBeanInitializer.class)public @interface MyAnnotation {字符串clientType()默认“”; }而且我...

java spring spring-annotations
1个回答
1
投票

请勿为此使用注释值。

© www.soinside.com 2019 - 2024. All rights reserved.