在App.java类无法扫描的包下编写HelloService;
package org.kfit.service; import org.springframework.stereotype.Service; @Service publicclass HelloService { /** * 启动的时候观察控制台是否打印此信息; */ public HelloService() { System.out.println("HelloService.HelloService()"); System.out.println("org.kfit.service.HelloService.HelloService()"); System.out.println("HelloService.HelloService()"); } } <?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"> <!-- 注入spring boot无法扫描到的bean. --> <bean id="helloService" class="org.kfit.service.HelloService"></bean> </beans>编写ConfigClass注入配置文件application-bean.xml
package com.kfit.config; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.ImportResource; /** * classpath路径:locations={"classpath:application-bean1.xml","classpath:application-bean2.xml"} * file路径: locations = {"file:d:/test/application-bean1.xml"}; */ @Configuration @ImportResource(locations={"classpath:application-bean.xml"}) //@ImportResource(locations={"file:d:/test/application-bean1.xml"}) publicclass ConfigClass { }app.java
package com.kfit; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * * * 大家也许会看到有些demo使用了3个注解: @Configuration; * * @EnableAutoConfiguration * @ComponentScan * * 其实:@SpringBootApplication申明让spring boot自动给程序进行必要的配置, * * 等价于以默认属性使用@Configuration, * @EnableAutoConfiguration和@ComponentScan * * 所以大家不要被一些文档误导了,让自己很迷茫了,希望本文章对您有所启发; * * @author Angel(QQ:412887952) * @version v.0.1 */ @SpringBootApplication public class App { public static void main(String[] args) { SpringApplication.run(App.class, args); } }ImportResouce有两种常用的引入方式:classpath和file,具体查看如下的例子 classpath路径:locations={“classpath:application-bean1.xml”, “classpath:application-bean2.xml” } file路径: locations = {“file:d:/test/application-bean1.xml”};
原文:http://412887952-qq-com.iteye.com/blog/2293846
