spring读取配置文件info.xml 01

先添加jar包

<dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
        <!--spring-core-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
        </dependency>
        <!--spring-bean-->
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
        </dependency>
        <!--spring-context-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
        </dependency>
        <!--spring-expression-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
        </dependency>
    </dependencies>

第二步创建配置文件spring.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  
</beans>

第三步书写一个类User

package com.ioc09.entity;

import java.io.IOException;
import java.util.Properties;

/**
 * package_name:com.ioc09.entity
 * Author:徐亚远
 * Date:2020/1/21 18:56
 * 项目名:springDemo01
 * Desription:
 **/
public class User {
    private String username;
    private String password;

    @Override
    public String toString() {
        return "User{" +
                "username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void init() {
        this.username = reveale(username);
        this.password = reveale(password);
    }

    private String reveale(String value) {
        //判断是否为空
        if (value == null || "".equals(value)) {
            return value;
        }
        //截取字符串
        if (value.startsWith("${") && value.endsWith("}")) {
            String key = value.substring(2, value.length() - 1);
            return getProperty(key);
        }
        return value;
    }

    private String getProperty(String key) {
        Properties properties = new Properties();
        try {
            properties.load(User.class.getClassLoader().getResourceAsStream("ioc09/info.properties"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (properties.containsKey(key)){
            return properties.getProperty(key);
        }else {
            throw  new RuntimeException("not found key:"+key+" in the properties");
        }
    }
}

第四步 配置spring.xmll文件

<?xml version="1.0" encoding="UTF-8"?>
<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.xsd">
   <bean id="user" class="com.ioc09.entity.User" init-method="init">
       <property name="username" value="${username}"/>
       <property name="password" value="${password}"/>
   </bean>

</beans>

第五步 配置info.properties文件

username=xiaoxu
password=admin

第五步书写测试方法UserController

package com.ioc09.controller;

import com.ioc09.entity.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * package_name:com.ioc09.controller
 * Author:徐亚远
 * Date:2020/1/21 19:18
 * 项目名:springDemo01
 * Desription:
 **/
public class UserController {
    public static void main(String args[]) {
        ApplicationContext ac = new ClassPathXmlApplicationContext("ioc09/spring.xml");
        User user = (User) ac.getBean("user");
        System.out.println("username:"+user.getUsername());
        System.out.println("password:"+user.getPassword());
    }


}

执行结果如图:
在这里插入图片描述

发布了64 篇原创文章 · 获赞 1 · 访问量 918

猜你喜欢

转载自blog.csdn.net/weixin_43311650/article/details/104065524
今日推荐