Spring boot
导入
spring XML
配置文件
在 App.java 类编写 HelloService2;
首先我们这里有几个包:com.hpit,org.hpit,我们这里打算把 App.java 启动类放到 com.hpit 中,根据 Spring
Boot 扫描(根包到子包的原则),我们把 HelloService2 写在 Spring Boot 可以扫描的位置,HelloService 写在
Spring Boot 无法扫描到的位置,那么我们使用配置文件 bean 的方式进行引入,具体代码如下:
1.创建一个 App 默认无法扫描到的 bean
org.hpit.demo.service.HelloService
package
org.hpit.demo.service;
import
org.apache.log4j.Logger;
import
org.springframework.stereotype.Service;
/**
*
TODO
当前类无法被App扫描到将被配置在applicationContext.xml中
*
*/
@Service
(
"helloService"
)
public class
HelloService {
private
Logger
logger
= Logger.
getLogger
(getClass());
public void
hello() {
logger
.info(
"这个bean是springboot默认情况下无法扫描到的"
);
}
}
2.在 resource 下创建 spring 传统配置文件 applicationContext.xml(名字任意)
src/main/resource/applicationContext.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
=
"helloService"
class
=
"org.hpit.demo.service.HelloService"
>
bean
>
beans
>
3.创建一个系统启动任务类,用于测试 App 无法扫描到的 Bean 是否能自动装配
com.hpit.springboot03.runner.TestXMLBeanRunner
package
com.hpit.springboot03.runner;
import
javax.annotation.Resource;
import
org.hpit.demo.service.HelloService;
import
org.springframework.boot.CommandLineRunner;
import
org.springframework.core.annotation.Order;
import
org.springframework.stereotype.Component;
/**
*
TODO
测试App无法扫到的Bean是否能引入
*
*/
@Component
@Order
(value = 1)
public class
TestXMLBeanRunner
implements
CommandLineRunner {
@Resource
private
HelloService
helloService
;
@Override
public void
run(String...
arg0
)
throws
Exception {
helloService
.hello();
}
}
4.在 App.java 中配置引入配置文件的注解 @ImportResource
package
com.hpit.springboot03;
import
javax.servlet.MultipartConfigElement;
import
org.springframework.boot.SpringApplication;
import
org.springframework.boot.autoconfigure.SpringBootApplication;
import
org.springframework.boot.web.servlet.MultipartConfigFactory;
import
org.springframework.boot.web.servlet.ServletComponentScan;
import
org.springframework.context.annotation.Bean;
import
org.springframework.context.annotation.ComponentScan;
import
org.springframework.context.annotation.ImportResource;
@SpringBootApplication
@ServletComponentScan
// 开始servlet扫描
@ComponentScan
(basePackages = {
"com.hpit"
})
@ImportResource(locations = { "applicationContext.xml" }) // 导入spring配置文件
public class
App {
public static void
main(String[]
args
)
throws
Exception {
SpringApplication.
run
(App.
class
,
args
);
}
// 配置文件上传
@Bean
public
MultipartConfigElement multipartConfigFactory() {
MultipartConfigFactory
configFactory
=
new
MultipartConfigFactory();
configFactory
.setMaxFileSize(
"128MB"
);
// KB MB 设置单个上传文件大小
configFactory
.setMaxRequestSize(
"1024MB"
);
configFactory
.setLocation(
"/"
);
// 设置文件上传路径
return
configFactory
.createMultipartConfig();
}
}
5.启动应用,观察日志输出,发现系统可以引入 App 无法扫描到的 bean