SpringBoot如何读取xml配置bean(@ImportResource)
作者:上官天夜 时间:2021-08-03 22:33:44
读取xml配置bean(@ImportResource)
1、应用场景
旧框架SSM项目移行到SpringBoot中,xml配置文件很齐全,就可以省去配置的麻烦,直接读取旧xml文件
2、spring-common.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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="msgService" class="com.zemel.micorboot.service.impl.MessageServiceImpl"></bean>
</beans>
3、SpringBoot读取xml
package com.zemel.micorboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;
/**
* Hello world!
*
*/
@SpringBootApplication
@ImportResource(locations={"classpath:xml/spring-common.xml"})
public class App
{
public static void main( String[] args )
{
SpringApplication.run(App.class, args);
}
}
4、应用xml中的bean对象
package com.zemel.micorboot.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zemel.micorboot.base.AbstractBaseController;
import com.zemel.micorboot.service.MessageService;
@RestController
public class MessageController extends AbstractBaseController {
@Autowired
private MessageService messageService;
@GetMapping("/echo")
public String echo(String mid){
System.out.println("[***访问地址***]"+super.getMessage("member.add.action"));
return super.getMessage("welcome.msg", mid);
}
public String msg(){
return (messageService.getMessage());
}
}
5、Service类
package com.zemel.micorboot.service;
public interface MessageService {
String getMessage();
}
package com.zemel.micorboot.service.impl;
import com.zemel.micorboot.service.MessageService;
public class MessageServiceImpl implements MessageService{
@Override
public String getMessage() {
return "my message...";
}
}
6、测试
package com.zemel.micorboot.controller;
import javax.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import com.zemel.micorboot.App;
@SpringBootTest(classes=App.class)
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
public class MessageControllerTest {
@Resource
private MessageController mc;
@Test
public void testEcho() {
System.out.println(this.mc.echo("mldnjava"));
}
@Test
public void testMsg(){
System.out.println(this.mc.msg());
}
}
读取配置文件中的参数
springBoot是java开发中会经常用到的框架,那么在实际项目中项目配置了springBoot框架,应该如何在项目中读取配置文件中的参数呢?
1、打开eclipse开发工具软件
2、在项目中确保pom.xml文件已引用
【spring-boot-starter-web】jar包。
因为springBoot启动的时候会自动去获取项目中在resources文件录目下的名为application.properties参数配置文件。
3、在项目中的src/main/resource文件录目下
创建application.properties参数配置文件。
4、在application.properties配置文件中添加对应的参数
5、此时在项目启动的时候
springBoot容器就会自动的将application.properties配置文件的配置信息自动的加入在spring容器中。
6、在需要使用的配置参数信息的类中
只要通过spring注解@Value("${xxx}")的方法注入到全局变量中即可读取配置文件中的参数。
来源:https://blog.csdn.net/zhangminemail/article/details/83419007
标签:SpringBoot,xml,bean,@ImportResource
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java SpringCache+Redis缓存数据详解
2023-11-29 01:01:05
![](https://img.aspxhome.com/file/2023/1/60131_0s.jpg)
Java spring AOP基础
2022-08-10 12:39:25
java的引用类型的详细介绍
2022-05-15 09:34:40
Struts2实现对action请求对象的拦截操作方法
2023-06-08 01:54:13
![](https://img.aspxhome.com/file/2023/2/70342_0s.jpg)
基于Java GUI 事件处理方式
2023-11-25 13:08:55
![](https://img.aspxhome.com/file/2023/9/60079_0s.png)
Java判断两个日期相差天数的方法
2021-11-29 05:55:07
Mybatis-Plus注入SQL原理分析
2022-11-09 21:17:22
![](https://img.aspxhome.com/file/2023/6/64496_0s.jpg)
C#字符串加密解密方法实例
2022-01-11 23:30:14
java 使用idea将工程打成jar并创建成exe文件类型执行的方法详解
2022-12-07 02:49:30
![](https://img.aspxhome.com/file/2023/2/60702_0s.png)
struts2实现文件上传显示进度条效果
2021-11-19 16:16:21
在Struts2中如何将父类属性序列化为JSON格式的解决方法
2022-08-01 09:30:34
Spring Security入门demo案例
2023-07-01 18:57:30
Java中线程安全问题
2021-12-02 05:51:59
![](https://img.aspxhome.com/file/2023/2/85442_0s.jpg)
OpenTelemetry初识及调用链Trace详解
2021-08-03 12:37:14
![](https://img.aspxhome.com/file/2023/9/85549_0s.png)
JDK8 中Arrays.sort() 排序方法详解
2023-01-17 10:38:30
![](https://img.aspxhome.com/file/2023/2/79892_0s.png)
C#操作INI配置文件示例详解
2021-11-06 11:03:01
![](https://img.aspxhome.com/file/2023/6/77246_0s.png)
java9学习系列之安装与jshell使用
2022-08-04 21:51:16
C#通过链表实现队列的方法
2023-06-19 15:14:17
小米Java程序员第二轮面试10个问题 你是否会被刷掉?
2023-11-13 09:00:29
![](https://img.aspxhome.com/file/2023/6/59026_0s.jpg)
c# 实现文件上传下载功能的实例代码
2021-12-10 15:00:30