【发布时间】:2020-07-09 16:52:02
【问题描述】:
好吧,这对于所有退伍军人来说可能看起来很愚蠢,但请耐心等待,因为我只是在 Spring 和 Spring Boot 中找到自己的方法。
我这里有一个控制器类,
@RestController
public class Controller {
private static final Logger logger = LogManager.getLogger(Controller.class);
private static Controller controller = null;
@Autowired
private ApplicationParameters applicationParameters;
public static Controller getInstance() {
if (controller == null) {
synchronized (Controller.class) {
if (controller == null) {
controller = new Controller();
}
}
}
return controller;
}
public Controller() {}
public ApplicationParameters getApplicationParameters() {
return applicationParameters;
}
@RequestMapping("/")
public void init() {
try {
for (Entry<String, String> prop : applicationParameters.getProperties().entrySet())
logger.info("Loaded System Property: " + prop.getKey() + " -> " + prop.getValue());
Utils.concatenate("key1", "key2");
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
}
}
它使用属性文件中的属性自动装配ApplicationParameters bean。
实用类
public class Utils {
protected static final Logger logger = LogManager.getLogger(Utils.class);
//Need to get the value of the property keys propKey1 & propKey2 and concat them.
public static String concatenate(String propKey1, String propKey2) throws Exception {
if(StringUtils.isNoneEmpty(propKey2) && StringUtils.isNoneEmpty(propKey1)) {
return Controller.getInstance().getApplicationParameters().getProperties().get(propKey1) + Controller.getInstance().getApplicationParameters().getProperties().get(propKey2)
} else {
logger.error("System Property is undefined." );
return null;
}
}
所以,我想在我的项目的整个生命周期中使用这个自动连接的ApplicationParameters bean 作为singleton 实例。
例如,我想在Utils 类中使用它。显然Utils 类不是弹簧管理的,它只是一个普通的旧 java 类。
所以我想知道如何在我的 Utils 类中使用完全初始化的 applicationParameters。
这是我迄今为止尝试过的:
-
在
Utils类中再次自动装配ApplicationParameters,就像这样,public class Utils { @Autowired private ApplicationParameters applicationParameters; protected static final Logger logger = LogManager.getLogger(Utils.class);
但applicationParameters 在这里将是null,我猜这是因为Utils 不是弹簧管理的bean。
- 使
Controller类单例。 (不确定如何执行此操作,因为需要在 Web 服务器启动时调用 init(),然后在哪里调用 getInstance()?)
所以,有没有好心人来帮助这里的新手。
附: Utils 类仅作为示例显示,以说明 Spring 管理的自动装配 bean 必须在常规 java 类中使用。
【问题讨论】:
-
您的 Util 类是否使用 Service/Controller/Component 注释?否则,您在类中使用的 Autowired 注释不起作用。您也可以使用 PostConstruct 而不是 RequestMapping 来注释您的 init 方法。
-
您的单例实现可能是错误的。当您将公共构造函数放入类中时,仍然有人可以创建该类的新实例。您应该将构造函数设为私有,以便它们只能通过 getInstance 方法访问类实例。最好再读一遍spring bean scope,singleton,prototype文档。
标签: java spring spring-boot