java – 从像pojo或singleton这样的非组件类中读取application.properties
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – 从像pojo或singleton这样的非组件类中读取application.properties,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3440字,纯文字阅读大概需要5分钟。
内容图文
![java – 从像pojo或singleton这样的非组件类中读取application.properties](/upload/InfoBanner/zyjiaocheng/781/c4f2128f3d4f40d6a3e197e5b22e25d7.jpg)
我正在使用弹簧靴和对于属性我已将application.properties放在src / main / resources中
来自@Controllers& @Services,我能够读取属性,但是从我的模型pojo&单身类,我无法读取值.
我的@SpringBootApplication在com.vehicle&我没有覆盖@ComponentScan,所以我相信它应该读取所??有底层包.
以下是代码:
application.properties
vehicle.lift.maxWeight=10
vehicle.lift.error.overWeight=Overweight
vehicle.battery.default=5.0
vehicle.battery.critical.limit=15
vehicle.walk.distance.error=Not able to move
WalkServiceImpl.java(能从这里读取)
package com.vehicle.prototype.service.impl;
@Service
public class WalkServiceImpl implements CapabilityService {
@Value("${vehicle.walk.distance.error}")
private String mDistanceError;
@Override
public void performTask(VehicleData vehicleData) {
double distance = vehicleData.getWalkingDistance();
double remainingBattery = vehicleData.getRemainingBattery();
if (remainingBattery < distance) {
vehicleData.setErrorMessage(mDistanceError);
System.out.println(mDistanceError);
} else {
vehicleData.setRemainingBattery(remainingBattery - distance);
}
VehicleUtil.checkBatteryStatus(vehicleData);
}
}
VehicleData.java(Pojo – 无法从这里读取)
package com.vehicle.prototype.model;
public class VehicleData {
private double walkingDistance;
private double liftWeight;
@Value("${vehicle.battery.default}")
private double remainingBattery;
// setters & getters ....
}
VehicleUtil.java(Singleton – 无法从这里读取)
package com.vehicle.prototype.utils;
public class VehicleUtil {
private static VehicleUtil mInstance = null;
private static Object mLock = new Object();
@Value("${vehicle.battery.critical.limit}")
private static double mCriticalLimit;
@Value("${vehicle.battery.default}")
private static double mTotalPower;
@Value("${vehicle.battery.critical.warning}")
private static String powerWarning;
private VehicleUtil() {
// empty private constructor.
}
public static VehicleUtil getInstance() {
if (mInstance == null) {
synchronized (mLock) {
if (mInstance == null)
mInstance = new VehicleUtil();
}
}
return mInstance;
}
public static void checkBatteryStatus(VehicleData vehicleData) {
double criticalMark = (mCriticalLimit * 100.0f) / mTotalPower;
if (vehicleData.getRemainingBattery() < criticalMark) {
vehicleData.setBatteryCritical(Boolean.TRUE);
System.out.println(powerWarning);
} else {
vehicleData.setBatteryCritical(Boolean.FALSE);
}
}
}
请让我知道如何解决这个问题.
谢谢
解决方法:
您需要将属性注入spring-managed bean.
你可以做的是将属性捆绑在这样的bean中(see documentation)
@ConfigurationProperties(prefix="vehicle")
public class VehicleProperties {
@Value("${battery.critical.limit}")
private double mCriticalLimit;
@Value("${battery.default}")
private double mTotalPower;
@Value("${battery.critical.warning}")
private String powerWarning;
}
然后,将Vehicle Properties注入您的服务并将它们传递给VehicleUtil:
public class WalkServiceImpl {
@Autowired
private VehicleProperties vehicleProperties;
@Override
public void performTask(VehicleData vehicleData) {
...
VehicleUtil.checkBatteryStatus(vehicleProperties, vehicleData);
}
}
更好的是,我会将VehicleUtil转换为托管bean
@Component
public class VehicleUtil {
@Autowired
private VehicleProperties vehicleProperties;
}
VehicleData
我假设VehicleData是一些业务对象,从客户端传递或者你自己创建的,它们是很多实例(不只是一个单例).在这种情况下,将VehicleData转换为托管bean是没有意义的,它的外部依赖关系可以通过它的setter提供给它:
public class VehicleData {
private double walkingDistance;
private double liftWeight;
private double remainingBattery;
// setters & getters ....
}
内容总结
以上是互联网集市为您收集整理的java – 从像pojo或singleton这样的非组件类中读取application.properties全部内容,希望文章能够帮你解决java – 从像pojo或singleton这样的非组件类中读取application.properties所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。