springboot中xml配置之@ImportResource

springboot中进行相关的配置往往有java配置和xml配置两种方式。

使用java的方式配置只需要使用@configuration注解即可,而使用xml的方式配置的话需要使用@ImportResource来加载配置文件

不过多描述,直接以一个很简单的通过xml配置注入bean的例子来展示@ImportResource注解的使用

xml配置放在resources目录下

<?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="hello" class="com.example.yang.bean.Hello"/>

</beans>

新增一个配置类,导入xml配置

@ImportResource("classpath:beans.xml")
@Configuration
public class BeansConfig {

}

然后在使用的时候直接注入即可

@RestController
public class TestController {

    @Autowired
    private Hello hello;

    @RequestMapping("/hello")
    public String say() {
        return hello.say("小明");
    }
}

原文地址:https://www.cnblogs.com/xtuxiongda/p/11181679.html

时间: 2024-08-30 12:11:03

springboot中xml配置之@ImportResource的相关文章

spring管理SessionFactory中XML配置

1. 综合练习目标 2. 综合练习需求 3.模块划分 1. 综合练习目标 <1>复习 Java 基本语法 <2>熟悉掌握Java开发常用API <3>尝试建立面向对象思想 2. 综合练习需求 <1>接收用户的命令行输入 <2>以文件为基础完成数据的增删改查操作          3.模块划分 UI模块:(Java存在文本中都是以字符型式) 数据验证模块:验证用户输入是否合法 spring管理SessionFactory中XML配置,布布扣,bub

springboot中数据库配置加密

在springboot中,配置数据库等信息时,用户名和密码明文显示会大大降低安全性,在此介绍一种加密方式,简单易用. 添加依赖: <dependency>    <groupId>com.github.ulisesbocchio</groupId>    <artifactId>jasypt-spring-boot-starter</artifactId>    <version>1.8</version> </de

Spring 中 Xml配置文件属性的说明

Xml配置文件属性的说明: <bean id="TheAction" ⑴ class="net.xiaxin.spring.qs.UpperAction" ⑵ singleton="true" ⑶ init-method="init" ⑷ destroy-method="cleanup" ⑸ depends-on="ActionManager" ⑹ > <propert

spring中xml配置和autowired混用

1.类的混用: 配置文件中的配置: <bean id="a" class="com.ab.cc.A" /> 类中的配置 @Autowired A a; 这样的好处,可以少掉get/set方法 [email protected]和@Resource的区别 Autowired默认是根据byType自动装配,所以有多个类型的xml或者注解某个类的时候,会报错,这时候可以再添加@Qualifier注解,指定名称.这个是spring自带的,换框架的时候,要spri

kafka+windows+java+springboot中的配置

1.百度kafka+zookeeper+windows配置 1.1  zookeeper配置 dataDir=/tmp/zookeeper # the port at which the clients will connect clientPort=2181 # disable the per-ip limit on the number of connections since this is a non-production config maxClientCnxns=0 1.2 kafk

springboot中切换配置(多个配置文件--生产、开发、测试)

问题描述: 在springboot项目中可能有测试环境.开发环境.生产环境,在这些环境中我们可能要使用不同的配置,如果每次切换环境的时候都要重新写一份配置文件就很麻烦了,所以下面提供一种方法可以快速且简便的切换不同环境下的配置. 解决方案: 1.首先在resources目录下创建完整的配置文件(包括测试.开发.生产环境下的相关配置文件),然后创建一个application.yml文件,因为springboot项目在启动的时候会默认加载该配置文件,解析其中的内容 2.在application.ym

spring-boot pom.xml 配置给docker打时间戳

在你想加入时间戳的项目里加入以下内容,pom.xml里面 在<plugins>下面加入插件 <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>buildnumber-maven-plugin</artifactId> <version>1.4</version> <configuration> <timestampFormat&

在SpringBoot中配置定时任务

前言 之前在spring中使用过定时任务,使用注解的方式配置很方便,在SpringBoot中的配置基本相同,只是原来在spring中的xml文件的一些配置需要改变,在SpringBoot中也非常简单. 已经加入我的github模版中:https://github.com/LinkinStars/springBootTemplate 定时任务的分类 所谓定时任务,就是在项目启动之后,定时的去执行一个任务,从而满足业务的需要. 定时任务分为下面几种,串行,并行,同步,异步 串行,并行:当配置了多个定

【小家Spring】老项目迁移问题:@ImportResource导入的xml配置里的Bean能够使用@PropertySource导入的属性值吗?

#### 每篇一句 > 大师都是偏执的,偏执才能产生力量,妥协是没有力量的.你对全世界妥协了你就是空气.所以若没有偏见,哪来的大师呢 #### 相关阅读 [[小家Spring]详解PropertyPlaceholderConfigurer.PropertyOverrideConfigurer等对属性配置文件Properties的加载和使用](https://blog.csdn.net/f641385712/article/details/91444601) [[小家Spring]Spring中@