[英]Best way to pass data from custom annotation to spring @PostConstruct?
i am writting a framework and I need to pass some information from an annotation to a PostConstruct method.我正在编写一个框架,我需要将一些信息从注释传递到 PostConstruct 方法。
What iam currently failing at is getting the data in a nice way.我目前未能以一种很好的方式获取数据。
How can i get from here:我怎样才能从这里得到:
@EnableCoolFeature(data1="foo", data2="bar")
public class MainApp {
public static void main (){ //SpringApp init etc }
}
To here:到这里:
@Configuration
public class CoolFeatureConfiguration{
@PostConstruct
void processData(){
//how to get the data in here?
}
}
Also note that the user can enable "@EnableCoolFeature" multiple times on different Configurations另请注意,用户可以在不同的配置上多次启用“@EnableCoolFeature”
EDIT: i found a way which works for me编辑:我找到了一种适合我的方法
var beanNamesForAnnotation = applicationContext.getBeansWithAnnotation(EnableCoolFeature.class);
for (var tuple : beanNamesForAnnotation.entrySet()) {
var result = applicationContext.findAnnotationOnBean(tuple.getKey(), EnableCoolFeature.class);
//the magic!
}
An example can be found here .一个例子可以在这里找到。
You need to implement something like this:你需要实现这样的东西:
import java.lang.annotation.Annotation;
import org.springframework.core.annotation.AnnotationUtils;
@EnableCoolFeature(data1 = "foo", data2 = "bar")
public class MainApp {
public static void main ()
{
try {
Annotation enableCoolFeatureAnnotation = AnnotationUtils.findAnnotation(MainApp.class, EnableCoolFeature.class);
System.out.println("@EnableCoolFeature of MainApp.class is: "+enableCoolFeatureAnnotation);
System.out.println("@EnableCoolFeature value: "+AnnotationUtils.getValue(enableCoolFeatureAnnotation));
System.out.println("@EnableCoolFeature default value data1: "+AnnotationUtils.getDefaultValue(enableCoolFeatureAnnotation, "data1"));
System.out.println("@EnableCoolFeature default value data2: "+AnnotationUtils.getDefaultValue(enableCoolFeatureAnnotation, "data2"));
System.out.println("@EnableCoolFeature data1: "+AnnotationUtils.getValue(enableCoolFeatureAnnotation, "data1"));
System.out.println("@EnableCoolFeature data2: "+AnnotationUtils.getValue(enableCoolFeatureAnnotation, "data2"));
String data1 = (String)AnnotationUtils.getValue(enableCoolFeatureAnnotation, "data1");
String data2 = (String)AnnotationUtils.getValue(enableCoolFeatureAnnotation, "data2");
// ... pass the arguments to Spring Boot: https://www.baeldung.com/spring-boot-command-line-arguments
String[] args = { data1, data2 };
SpringApplication.run(MainApp.class, args);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Edit: As the author asked how could unknown annotations be processed dynamically, here is an example.编辑:正如作者询问如何动态处理未知注释,这里有一个例子。 Before the code above can be executed, we need to retrieve the annotation classes and the field names according to a defined criteria.
在执行上述代码之前,我们需要根据定义的条件检索注解类和字段名称。 Paste the following code right after
try {
in the code above:在上面的代码中
try {
之后粘贴以下代码:
try {
MergedAnnotations mas = MergedAnnotations.from(PaperlessSpringApp.class);
for (MergedAnnotation<Annotation> ma : mas)
{
Class<Annotation> annotationClass = ma.getType();
Field[] fields = annotationClass.getFields();
for (Field field:fields)
{
String fieldName = field.getName();
// Retrieve the values as shown below with data1 and data2 but this time using annotationClass and fieldName.
}
}
....
In the original code example MainApp.class
must be replaced by a parameter Class<?> appClass
that must be supplied by your users when they call the main()
method from their code.在原始代码示例中,
MainApp.class
必须替换为参数Class<?> appClass
,当用户从其代码中调用main()
方法时,该参数必须由用户提供。 Or a better solution would be to rename the method and make it an instance method (not static), then let your users inherit from MainApp and then you can use directly getClass()
to get your user's app class:或者更好的解决方案是重命名方法并使其成为实例方法(非静态),然后让您的用户从 MainApp 继承,然后您可以直接使用
getClass()
来获取用户的应用程序 class:
// This is part of your framework:
public class MainApp {
protected void runSpring() {
// ... same code
}
}
// This is the code your users would have to write:
@EnableCoolFeature(data1 = "foo", data2 = "bar")
public class YourUsersApp extends MainApp {
public static void main ()
{
new YourUsersApp().runSpring();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.