加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

IOC读取配置文件

发布时间:2020-12-16 23:40:25 所属栏目:百科 来源:网络整理
导读:1. 创建一个bean文件 package com.longteng.utils;import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Component;import javax.annotation.PostConstruct; @Component public class DbUtils { @Value ( " ${

1. 创建一个bean文件

package com.longteng.utils;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;


@Component public class DbUtils {
    @Value ("${db.test1.ip}") private String ip;
    @Value ("${db.test1.port}") private int port;
    @Value ("${db.test1.userName}")
    private String userName;
    @Value ("${db.test1.pwd}")
    private String password;
    @PostConstruct
    public  void initDb(){
        System.out.println ("初始化数据库。。。。。。");
        System.out.println (""+toString ());
    }

    @Override
    public String toString() {
        return "DbUtils{" +
                "ip=‘" + ip +  +
                ",port=" + port +
                ",userName=‘" + userName +  +
                ",password=‘" + password +  +
                };
    }

    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    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;
    }
}

?

2、创建db配置文件

db.test1.ip=127.0.0.1
db.test1.port=3306
db.test1.userName=zhou
db.test1.pwd=123456

?

3、创建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"
       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:component-scan base-package="com.longteng.utils"></context:component-scan>
    <context:property-placeholder location="config/db.properties"></context:property-placeholder>
</beans>

?

?

4、创建测试类

package com.longteng.lesson2;


import com.longteng.utils.DbUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class DbTest {
    ApplicationContext context;

    @BeforeClass
    public void initApplication() {
        context = new ClassPathXmlApplicationContext ("db.xml");
    }

    @Test
    public void test() {
        DbUtils dbUtils = context.getBean ("dbUtils",DbUtils.class);


    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读