Springboot 加载xml
1.springboot 加载xml 下面两种方式都可以Spring Boot @ImportResource注释示例 Spring提供了一个@ImportResource注释,用于将spring-test.xml文件中的bean加载到Application Context中。@ImportResource({ “ classpath *:spring-test.xml ” })在Spring
·
1.springboot 加载xml 下面两种方式都可以
Spring Boot @ImportResource注释示例
Spring提供了一个 @ImportResource 注释,用于将spring-test.xml文件中的bean加载到Application Context中。
@ImportResource({ “ classpath *:spring-test.xml ” })
在Spring Boot的入口类中我们使用:
@SpringBootApplication
@ImportResource(locations = {"classpath:spring-test.xml"})
public class ProductApplication {
public static void main(String[] args) {
SpringApplication.run(ProductApplication.class, args);
}
}
推荐的方法是创建一个单独的配置类来加载此XML bean定义文件。
@Configuration
@ImportResource(locations = {"classpath:spring-test.xml"})
public class XmlConfiguration {
}
2.新建spring-test.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="xxlTestImpl" class="com.test.mybatisplus.service.impl.XxlTestImpl"/>
</beans>
3.新建XxlTestImpl 类
public class XxlTestImpl {
public String getTest() {
return "getTest";
}
}
4. 调用xxlTestImpl 方法
@Autowired
com.test.mybatisplus.service.impl.XxlTestImpl xxlTestImpl;
@ApiOperation(value = "查询list",notes = "查询list")
@RequestMapping(value ="/selectList" , method = RequestMethod.GET,produces = {"application/json;charset=UTF-8"})
public List<PressInfoEntity> selectList(){
QueryWrapper<PressInfoEntity> queryWrapper=new QueryWrapper();
queryWrapper.like("short_press","版");
List<PressInfoEntity> list= pressInfoService.selectList(queryWrapper);
System.out.println("XML配置:"+xxlTestImpl.getTest());
return list;
}
5.MybatisplusApplication 启动类上添加
@ImportResource(locations = {"classpath:spring-test.xml"})
@Slf4j
@SpringBootApplication
@MapperScan(value="com.test.mybatisplus.mapper")
@ImportResource(locations = {"classpath:spring-test.xml"})
//@EnableApolloConfig
public class MybatisplusApplication {
public static void main(String[] args) {
SpringApplication.run(MybatisplusApplication.class, args);
//NettyServer.run(8080);
//log.info("======netty服务已经启动========");
}
}
6.启动项目 运行方法
更多推荐
已为社区贡献1条内容
所有评论(0)