时间:2022-12-30 11:45:21 | 栏目:JAVA代码 | 点击:次
旧框架SSM项目移行到SpringBoot中,xml配置文件很齐全,就可以省去配置的麻烦,直接读取旧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>
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); } }
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()); } }
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..."; } }
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框架,应该如何在项目中读取配置文件中的参数呢?
【spring-boot-starter-web】jar包。
因为springBoot启动的时候会自动去获取项目中在resources文件录目下的名为application.properties参数配置文件。
创建application.properties参数配置文件。
springBoot容器就会自动的将application.properties配置文件的配置信息自动的加入在spring容器中。
只要通过spring注解@Value("${xxx}")的方法注入到全局变量中即可读取配置文件中的参数。