Java Spring:如何通过读取外部属性文件来注入带有@Value注释的值?

Java Spring:如何通过读取外部属性文件来注入带有@Value注释的值?,java,spring,annotations,Java,Spring,Annotations,我有以下情况 -有一个MongoService类,它从文件中读取主机、端口和数据库 xml配置 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://w

我有以下情况
-有一个MongoService类,它从文件中读取主机、端口和数据库

xml配置

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

    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>file:///storage/local.properties</value>
            </list>
        </property>
    </bean>
</beans>  
public class MongoServiceTest {

    @Autowired
    private MongoService mongoService;

}
MongoService类as

@Service
public class MongoService {

    private final Mongo mongo;
    private final String database;
    private static final Logger LOGGER = LoggerFactory.getLogger(MongoService.class);

    public MongoService(@Nonnull final @Value("#{ systemProperties['host']}") String host, @Nonnull final @Value("#{ systemProperties['port']}") int port, @Nonnull final @Value("#{ systemProperties['database']}") String db) throws UnknownHostException {
        LOGGER.info("host=" + host + ", port=" + port + ", database=" + db);
        mongo = new Mongo(host, port);
        database = db;
    }
}
当我想测试bean是否正常时,我在
MongoServiceTest.java

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

    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>file:///storage/local.properties</value>
            </list>
        </property>
    </bean>
</beans>  
public class MongoServiceTest {

    @Autowired
    private MongoService mongoService;

}
它抱怨说
无法识别MongoService的bean

然后,我将以下内容添加到上述xml中

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

然后它抱怨说
“没有找到匹配的构造函数”

我想做什么

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

    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>file:///storage/local.properties</value>
            </list>
        </property>
    </bean>
</beans>  
public class MongoServiceTest {

    @Autowired
    private MongoService mongoService;

}
a、 )MongoService应该是
@Autowired
并从
file:///storage/local.properties

问题

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

    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>file:///storage/local.properties</value>
            </list>
        </property>
    </bean>
</beans>  
public class MongoServiceTest {

    @Autowired
    private MongoService mongoService;

}
a、 )访问构造函数中的值是否正确<代码>(文件名是local.properties,我使用@Value(#{systemProperties['host']})语法)

b、 )我需要做什么才能使其正常工作,以便
@Autowired private MongoService MongoService
正确加载并读取local.properties文件中的值

另外,我对Spring很陌生,不知道如何让它工作


非常感谢您的帮助。默认情况下,Spring使用默认构造函数(不带参数)实例化对象,然后使用setter方法设置所有属性。如果您不想(或不能)使用setter或定义默认construtor,那么必须告诉spring作为构造函数参数传入什么

下面是一篇博客文章,解释如何做到这一点:

基本语法如下所示:

<bean name="MyBean" class="com.example.BeanClass">
  <constructor-arg index="0"><ref bean="OtherBeanName"/></constructor-arg>
</bean>


index属性是可选的,但它要求您按照与构造函数参数相同的顺序对XML
构造函数arg
元素进行排序。

我认为,您必须按如下方式将构造函数arg添加到config XML中

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

        <constructor-arg type="java.lang.String">
            <value>host</value>
        </constructor-arg>

        <constructor-arg type="int">
            <value>port</value>
        </constructor-arg>

        <constructor-arg type="java.lang.String">
            <value>database</value>
        </constructor-arg>

    </bean>