通过@ImportResource实现xml配置的装载
来源:互联网 发布:西科软件 知乎 编辑:程序博客网 时间:2024/06/04 18:57
通过初识springboot的实践,所有的bean装载全部通过java配置实现,那么一直以来习惯的xml配置是否就没有了用武之地呢,答案是否定的,下面就通过实践验证说明。
1、通过之前的实践,在启动类APP中定义了@ComponentScan(basePackages={"com.shf.SpringBoot1","com.shf.springboot.*"})扫描相关的注解配置类,故本次案例中的bean放置在其他包下,从而通过xml文件注册;
2、首先定义一个service层bean:
package com.shf.springboot2.service;
import org.springframework.stereotype.Service;
@Service
public class HelloService1 {
public void method1(){
System.out.println("class:HelloService1__method:method1");
}
}
3、添加xml配置文件applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd" default-autowire="byName" default-lazy-init="false" >
<description>Spring Configuration</description>
<!-- 开启注解模式 -->
<context:annotation-config />
<!-- 使用Annotation自动注册Bean -->
<context:component-scan base-package="com.shf.springboot2.service">
</context:component-scan>
</beans>
4、下面就是新增一个关键的配置类XmlConfiguration:
package com.shf.springboot.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
@Configuration
@ImportResource(locations={"classpath:applicationContext.xml"})
public class XmlConfiguration {
}
该配置类此时必须在启动类中扫描到。
5、在控制其中注入2中定义的service对应的bean,并调用其方法:
@RestController
public class HelloWorldController {
@Autowired
ServerConfig serverConfig;
@Autowired
ServerConfig2 serverConfig2;
@Autowired
HelloService1 helloService1;
@RequestMapping("/")
public String helloWorld() {
System.out.println("server.properties服务器端口:"+serverConfig.getPort());
System.out.println("application.properties服务器端口:"+serverConfig2.getPort());
helloService1.method1();
return "Hello World!";
}
}
6、启动程序,验证结果,是否能够正常的打印service中的内容。
以上正常装载bean,正常注入bean,正常使用bean实例。
本案例主要使用到了一个新的注解:
@ImportResource:通过locations属性加载对应的xml配置文件,同时需要配合@Configuration注解一起使用,定义为配置类;
0 0
- 通过@ImportResource实现xml配置的装载
- 通过XML批量装载图片
- 通过xml配置来实现数据库的批量修改一
- Spring-通过xml配置实现AOP
- android中通过自定义xml实现你需要的shape效果 xml属性配置
- android中通过自定义xml实现你需要的shape效果 xml属性配置
- android中通过自定义xml实现你需要的shape效果 xml属性配置
- android中通过自定义xml实现你需要的shape效果 xml属性配置
- android中通过自定义xml实现你需要的shape效果 xml属性配置
- android中通过自定义xml实现你需要的shape效果 xml属性配置
- 码农小汪-spring框架学习之9-基于 Java 的配置元数据 @ImportResource
- springboot 用了@ImportResource 配置的拦截器就不起作用了
- Spring AOP详解及通过XML配置实现的实现方法
- jboss下通过配置log4j.xml实现web应用的日志
- jboss下通过配置log4j.xml实现web应用的日志 (zt)
- struts2通过基于XML的配置实现校验没有提示信息
- android中通过自定义xml实现shape效果 的属性配置
- JavaConfig @Import 和 @ImportResource注解的使用
- 如何使用github管理代码
- 【Abertay阿伯泰大学毕业】
- 解决COM端口号与工具比匹配的问题
- 序列操作(线段树,模板)
- openTSDB+HBase+ZK遇到的坑汇总
- 通过@ImportResource实现xml配置的装载
- Java进阶-----TreeMap
- c++内存的分区
- Retrofit2.0源码分析
- yyyy-MM-dd HH:mm:ss转MM-dd HH:mm,String转String
- 2016-12-23
- rnn
- Swagger生成API文档
- 针对《面试心得与总结—BAT、网易、蘑菇街》一文中出现的技术问题的收集与整理(3)