• 使用@ImportResource加载XML配置文件设置的Bean


    <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">
        <modelVersion>4.0.0</modelVersion>
        <groupId>www.tszr</groupId>
        <artifactId>chapter021</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.1.3.RELEASE</version>
        </parent>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-configuration-processor</artifactId>
                <optional>true</optional>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <source>11</source>
                        <target>11</target>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </project>
    package com.itheima.dao;
    
    public class MyService {
    
    }
    <?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="myService" class="com.itheima.dao.MyService" />
    
    </beans>
    package com.itheima;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.context.annotation.ImportResource;
    
    @SpringBootApplication
    @ImportResource("classpath:beans.xml")
    public class Application {
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    }
    package com.itheima;
    
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.context.ApplicationContext;
    import org.springframework.test.context.junit4.SpringRunner;
    
    import com.itheima.domain.Person;
    import com.itheima.config.MyPropertiesConfig;
    
    @RunWith(SpringRunner.class)
    @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
    public class ApplicationTest {
        
        @Autowired
        private ApplicationContext applicationContext;
        
        @Test
        public void ioTest() {
            System.out.println("spring容器是否包含了XML设置的bean呢?");
            System.out.println(applicationContext.containsBean("myService"));
        }
    }

  • 相关阅读:
    快速了解layui中layer的使用
    导航栏切换按钮事件
    jQuery中 end(); 的用法
    JS中关于 一个关于计时器功能效果的实现
    js 中 setInterval 的返回值问题
    javascript 构造函数中的属性与原型上属性优先级的比较
    斐波那契数列 -- 递归算法(-)
    javascript 变量声明有var与无var 的区别
    作用域的理解--第一篇
    Javascript---数组常用方法
  • 原文地址:https://www.cnblogs.com/tszr/p/15907274.html
Copyright © 2020-2023  润新知