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

时间:2012-06-29 19:45:58

标签: java spring annotations spring-annotations

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

中的值

local.properties

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

我该如何解决这个问题?

更新 即使在为字段创建setter / getters之后,它似乎也没有读取值。我现在真的很无能为力。

更新01

添加init()方法并将其添加到bean后,stil不起作用。我甚至没有看到记录消息
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);
    }

我怎样才能调试此问题?

非常感谢!

1 个答案:

答案 0 :(得分:1)

我在这里看到一个问题,你在MongoService构造函数中实例化一个Mongo类,但是此时你的属性还没有基于local.properties文件注入,所以值得主机,端口和数据库在那里将为空。

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

MongoService中的

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

并在您的bean配置中:

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