gpt4 book ai didi

通过注解注入(inject) Java 字段

转载 作者:行者123 更新时间:2023-12-04 04:55:23 25 4
gpt4 key购买 nike

我正在尝试编写代码来注释类中的字段,以便从配置文件中设置该字段的值。这实际上与 spring 框架具有的 @Value 属性相同,但由于我不会深入讨论的原因,我没有使用 spring 框架。

我正在做的项目是一个使用 Jersey 框架的网络应用程序。对于所有前期信息,我深表歉意,但为了完整起见,这里是我所拥有的基本设置:

这是主要的应用程序类:

package com.ttrr.myservice;

import com.ttrr.myservice.controllers.MyController;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
import java.util.HashSet;
import java.util.Set;

@ApplicationPath("/")
public class MyApplication extends Application {
@Override
public Set<Class<?>> getClasses() {
final Set<Class<?>> classes = new HashSet<Class<?>>();
// register root resources
classes.add(MyController.class);

return classes;
}
}

这是 MyController 类,我希望注释可以在其中工作:
package com.ttrr.myservice.controllers;

import com.ttrr.myservice.annotations.Property;

import javax.servlet.ServletContext;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;

@Path("test")
public class MyController {

@Property("my.value.from.config")
private String myValue;

@GET
@Produces("text/html")
@Path("/testPage")
public String testPage(@Context ServletContext context) {

return "<p>" + myValue + "</p>";
}
}

我的注释界面非常简单:
package com.ttrr.myservice.annotations;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Property {
String value() default "";
}

我有一个注释解析器类:
package com.ttrr.myservice.annotations;

import java.io.IOException;
import java.lang.reflect.Field;
import java.util.Properties;

public class PropertyParser {

public static Properties properties;

static {
properties = new Properties();
try {
Property.class.getClassLoader();

Properties defaults = new Properties();
defaults.load(Property.class.getClassLoader().getResourceAsStream("application.properties"));

for(Object key : defaults.keySet()) {
properties.put(key, defaults.get(key));
}

} catch (IOException e) {
//in this case do nothing, properties will simply be empty
}
}

public void parse(Class<?> clazz) throws Exception {

Field[] fields = clazz.getDeclaredFields();

for (Field field : fields) {
if (field.isAnnotationPresent(Property.class)) {
Property property = field.getAnnotation(Property.class);
String value = property.value();

if (!"".equals(value)) {
field.set(clazz, properties.getProperty(value));
}
}
}
}
}

我真正苦苦挣扎的地方是将它们放在一起,并将我的 application.properties 文件中的值放入 M​​yController 类的实例中。

感谢您花时间阅读所有内容!任何帮助将不胜感激。

最佳答案

您尝试设置值的方式不正确,因为您需要一个实例来设置值。

field.set(clazz, properties.getProperty(value));

应该:
field.set(instance, properties.getProperty(value));

你应该为你的 parse 方法添加一个参数:
public void parse(Class<?> clazz, Object instance) throws Exception {

关于通过注解注入(inject) Java 字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16820349/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com