IOC读取配置文件

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.properties配置文件

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

 

3、创建db.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);


    }
}

 

转载于:https://www.cnblogs.com/zhou-test/p/10091952.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个使用 Autofac 读取配置文件并动态注入 IOC 的示例代码。 首先,我们需要安装 Autofac 的 NuGet 包。在 Visual Studio 中,可以通过 NuGet 包管理器搜索并安装 Autofac。 然后,我们需要创建一个配置文件来定义我们的依赖项。假设我们的配置文件是一个 XML 文件,如下所示: ```xml <?xml version="1.0" encoding="utf-8" ?> <dependencies> <dependency interface="MyNamespace.IMyService" type="MyNamespace.MyService" /> </dependencies> ``` 在这个配置文件中,我们定义了一个名为 `dependencies` 的根元素,它包含一个或多个 `dependency` 元素。每个 `dependency` 元素都具有两个属性:`interface` 和 `type`。`interface` 属性指定依赖项的接口类型,`type` 属性指定依赖项的实现类型。 接下来,我们需要编写代码来读取配置文件并注册依赖项。下面是一个简单的示例: ```csharp using System.IO; using System.Xml.Linq; using Autofac; public static class ContainerBuilderExtensions { public static void RegisterDependenciesFromConfigFile(this ContainerBuilder builder, string configFile) { var document = XDocument.Load(configFile); var dependencies = document.Element("dependencies").Elements("dependency"); foreach (var dependency in dependencies) { var interfaceType = Type.GetType(dependency.Attribute("interface").Value); var implementationType = Type.GetType(dependency.Attribute("type").Value); builder.RegisterType(implementationType).As(interfaceType); } } } ``` 这个扩展方法将会读取指定的配置文件,并将其中定义的依赖项注册到 Autofac 的容器中。我们可以在应用程序的启动代码中调用这个方法,以便动态注入依赖项。 ```csharp var builder = new ContainerBuilder(); builder.RegisterDependenciesFromConfigFile("dependencies.xml"); var container = builder.Build(); // 从容器中解析需要的依赖项 var service = container.Resolve<IMyService>(); ``` 在这个示例中,我们首先创建了一个 `ContainerBuilder` 对象,并注册了一个扩展方法 `RegisterDependenciesFromConfigFile`。然后,我们在应用程序的启动代码中调用这个方法,并使用 `Build` 方法构建一个 Autofac 容器。最后,我们可以通过调用 `Resolve` 方法来从容器中解析需要的依赖项。 注意,我们在这个示例中使用了 `Type.GetType` 方法来动态获取类型。这意味着我们需要在配置文件中指定完全限定的类型名称,包括命名空间。如果你不想在配置文件中指定完全限定的类型名称,你可以考虑使用其他方法来动态获取类型,例如使用反射或使用一个字符串到类型的映射表。 希望这个示例能够帮助你了解如何使用 Autofac 读取配置文件并动态注入依赖项。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值