gpt4 book ai didi

java - Spring:使用 @Value 注释时,Bean 无法从外部属性文件读取值

转载 作者:行者123 更新时间:2023-12-01 15:18:44 24 4
gpt4 key购买 nike

XML 配置

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

<util:properties id="mongoProperties" location="file:///storage/local.properties" />

<bean id="mongoService" class="com.business.persist.MongoService"></bean>
</beans>

MongoService 看起来像

@Service
public class MongoService {

@Value("#{mongoProperties[host]}")
private String host;

@Value("#{mongoProperties[port]}")
private int port;

@Value("#{mongoProperties[database]}")
private String database;

private Mongo mongo;

private static final Logger LOGGER = LoggerFactory.getLogger(MongoService.class);

public MongoService() throws UnknownHostException {
LOGGER.info("host=" + host + ", port=" + port + ", database=" + database);
mongo = new Mongo(host, port);
}

public void putDocument(@Nonnull final DBObject document) {
LOGGER.info("inserting document - " + document.toString());
mongo.getDB(database).getCollection(getCollectionName(document)).insert(document, WriteConcern.SAFE);
}

我将 MongoServiceTest 写为

public class MongoServiceTest {

@Autowired
private MongoService mongoService;

public MongoServiceTest() throws UnknownHostException {
mongoRule = new MongoRule();
}

@Test
public void testMongoService() {
final DBObject document = DBContract.getUniqueQuery("001");
document.put(DBContract.RVARIABLES, "values");
document.put(DBContract.PVARIABLES, "values");

mongoService.putDocument(document);
}

我认为测试失败为

12:37:25.224 [main] INFO  c.s.business.persist.MongoService - host=null, port=0, database=null
java.lang.NullPointerException
at com.business.persist.MongoServiceTest.testMongoService(MongoServiceTest.java:40)

这意味着 bean 无法从 local.properties 读取值

本地属性

### === MongoDB interaction === ###
host="127.0.0.1"
port=27017
database=contract

我该如何解决这个问题?

更新即使在为字段创建 setter/getter 后,它似乎也不会读取值。我现在真是一无所知。

更新01

添加init()方法并将其添加到bean后,它仍然不起作用。我什至没有看到日志消息
XML

<bean id="mongoService" class="com.business.persist.MongoService" init-method="init"></bean>

MongoService

@Service
public class MongoService {

@Value("#{mongoProperties['host']}")
private String host;

@Value("#{mongoProperties['port']}")
private int port;

@Value("#{mongoProperties['database']}")
private String database;

private Mongo mongo;

private static final Logger LOGGER = LoggerFactory.getLogger(MongoService.class);

public MongoService() {}

public void init() throws UnknownHostException {
LOGGER.info("host=" + host + ", port=" + port + ", database=" + database);
mongo = new Mongo(host, port);
}

public void putDocument(@Nonnull final DBObject document) {
LOGGER.info("inserting document - " + document.toString());
mongo.getDB(database).getCollection(getCollectionName(document)).insert(document, WriteConcern.SAFE);
}

我该如何调试这个问题?

非常感谢!

最佳答案

我在这里看到一个问题,您正在 MongoService 构造函数中实例化 Mongo 类,但是此时您的属性尚未基于 local.properties< 注入(inject) 文件中的主机、端口和数据库的值将为空。

相反,您可以做的是不在 Service 构造函数中实例化 Mongo 类,让属性按照您的方式注入(inject)到 MongoService 中,然后将其创建为 init 方法的一部分,该方法在您的属性被调用后调用设置:

在 MongoService 中:

public void init(){
mongo = new Mongo(host, port);
}

在你的bean配置中:

<bean id="mongoService" class="com.business.persist.MongoService" init-method="init"></bean>

关于java - Spring:使用 @Value 注释时,Bean 无法从外部属性文件读取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11268049/

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