Skip to content

Spring 通过 XML 装配 Bean

🏷️ Spring

配置类 DbConfig(假设是数据配置,拥有 4 个属性 driver, url, username, password

点击查看代码
java
package xmlbean;

import org.springframework.stereotype.Component;

/**
 * Created by liujiajia on 2017/3/7.
 */
//@Component
public class DbConfig {
    private String driver;
    private String url;
    private String username;
    private String password;

    public String getDriver() {
        return driver;
    }

    public void setDriver(String driver) {
        this.driver = driver;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

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

配置类 XmlBeanConfig (使用 @ImportResource 标注导入配置文件 db-config.xml

java
package xmlbean;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;

/**
 * Created by liujiajia on 2017/3/7.
 */
@Configuration
@ComponentScan
//@EnableAutoConfiguration
@ImportResource("classpath:db-config.xml")
public class XmlBeanConfig {
//    public static void main(String[] args) {
////        SpringApplication.run(XMLConfig.class, args);
//    }
}

配置文件 db-config.xml (文件中定义了 DbConfig 类型的 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"
       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">

    <bean id="dbConfig" class="xmlbean.DbConfig">
        <property name="driver" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
        <property name="url" value="jdbc:sqlserver://127.0.0.1:1433;databaseName=testdb" />
        <property name="username" value="user001" />
        <property name="password" value="pwd001" />
    </bean>
</beans>

测试代码 XmlBeanConfigTest (测试 dbConfig 已经自动导入,并且值同 xml 文件中的配置一致)

点击查看代码
java
package xmlbean;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

/**
 * Created by liujiajia on 2017/3/7.
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = XmlBeanConfig.class)
public class XmlBeanConfigTest {
    @Autowired
    private DbConfig dbConfig;

    @Test
    public void cdShouldNotBeNull() {
        assertNotNull(dbConfig);
    }

    @Test
    public void configShouldEqualTheSettingInConfigXML() {
        assertEquals("com.microsoft.sqlserver.jdbc.SQLServerDriver", dbConfig.getDriver());
        assertEquals("jdbc:sqlserver://127.0.0.1:1433;databaseName=testdb", dbConfig.getUrl());
        assertEquals("user001", dbConfig.getUsername());
        assertEquals("pwd001", dbConfig.getPassword());
    }
}

执行测试,结果应该是两个绿色的。

pom.xml 文件 (仅供参考)

点击查看代码
xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.2.RELEASE</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>WireBean</artifactId>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>4.3.7.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>RELEASE</version>
            <scope>test</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.github.stefanbirkner/system-rules -->
        <dependency>
            <groupId>com.github.stefanbirkner</groupId>
            <artifactId>system-rules</artifactId>
            <version>1.16.1</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>