xml和@Configuration中的properties

xml中注入properties

dengyi.properties

dengyi.name=dengyi
dengyi.age=22
dengyi.love=jianglei
dengyi.tall=175

spring-bean.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    <!--  注入 -->
    <context:property-placeholder location="dengyi.properties"/>

    <!-- 使用 -->
    <bean id="student" class="com.jianglei.property.bean.Student">
        <property name="name" value="${dengyi.name}"/>
        <property name="love" value="${dengyi.love}"/>
        <property name="age" value="${dengyi.age}"/>
    </bean>
</beans>

@Configuration 类中注入properties

dengyi.properties

dengyi.name=dengyi
dengyi.age=22
dengyi.love=jianglei
dengyi.tall=175

AppConfig.java

@Configuration
@PropertySource(value = "classpath:dengyi.properties")
public class AppConfig {

    @Value("${dengyi.name}")
    private String name;

    @Value("${dengyi.love}")
    private String love;

    @Value("${dengyi.age}")
    private Integer age;

    @Bean
    public Dengyi dengyi() {
        Dengyi dengyi = new Dengyi();
        dengyi.setAge(age);
        dengyi.setName(name);
        dengyi.setLove(love);

        return dengyi;
    }
}

results matching ""

    No results matching ""