Spring Boot 教程

@ImportResource 注解

@ImportResource 注解用于导入 Spring 的配置文件,如:spring-mvc.xml、application-Context.xml。遗憾的是 Spring Boot 里面没有Spring 配置文件,都是通过 Java 代码进行配置。如果我们自己编写了配置文件,Spring Boot 是不能自动识别,此时需要使用 @ImportResource 注解将自己的配置文件加载进来。

与 @Import 注解一样,此注解提供的功能类似于 Spring XML 中的 <import /> 元素。当设计的 @Configuration 类由AnnotationConfigApplicationContext 引导时,通常会使用它,但是一些XML功能(如名称空间)仍然是必要的。

默认情况下,如果以 “ .groovy” 结尾,则将使用 GroovyBeanDefinitionReader 处理 value() 属性的参数。否则,将使用 XmlBeanDefinitionReader 解析 Spring <beans /> XML文件。 可以选择声明 reader() 属性,允许用户选择自定义 BeanDefinitionReader 实现。

@ImportResource 注解的可选属性,如下:

  • String[] locations:要导入的资源路径,如:classpath:spring-mvc.xml 从类路径加载 spring-mvc.xml 配置文件。

  • Class<? extends BeanDefinitionReader> reader:在处理通过 value() 属性指定的资源时使用的 BeanDefinitionReader 实现。默认情况下,读取器将适应指定的资源路径:“.groovy” 文件将使用 GroovyBeanDefinitionReader 处理;然而,所有其他资源都将使用 XmlBeanDefinitionReader 进行处理。

  • String[] value:locations() 的别名

示例代码

我们在 resources 目录下面创建一个 application-context.xml 文件,该文件中手动声明一个 Bean,然后在 Controller 中引用。

(1)application-context.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">
    <bean id="dateBean" class="com.huangx.springboot.demo1.bean.DateBean" />
</beans>

(2)定义 DateBean,代码如下:

package com.huangx.springboot.demo1.bean;

import java.text.SimpleDateFormat;
import java.util.Date;

public class DateBean {
    private static final SimpleDateFormat DF =
            new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private String currentDate;

    public DateBean init() {
        this.currentDate = DF.format(new Date());
        return this;
    }

    public String getCurrentDate() {
        return currentDate;
    }

    public void setCurrentDate(String currentDate) {
        this.currentDate = currentDate;
    }
}

(3)客户端代码,在客户端中使用 @Autowired 注入 DateBean 对象。然后调用它的 init() 和 getCurrentDate() 方法,代码如下:

import com.huangx.springboot.springboot_importresource_demo1.bean.DateBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@ImportResource(locations = {"classpath:application-context.xml"})
@RestController
@SpringBootApplication
public class Demo1Application {

    @Autowired
    private DateBean dateBean;

    public static void main(String[] args) {
        SpringApplication.run(Demo1Application.class, args);
    }

    @RequestMapping("/")
    public String index() {
        if(null == dateBean) {
            return "-";
        } else {
            return dateBean.init().getCurrentDate();
        }
    }

}

上面的 @ImportResource(locations = {"classpath:application-context.xml"}) 代码可以简写为 @ImportResource({"classpath:application-context.xml"})。

说说我的看法
全部评论(
没有评论
关于
本网站属于个人的非赢利性网站,转载的文章遵循原作者的版权声明,如果原文没有版权声明,请来信告知:hxstrive@outlook.com
公众号