单模块demo:https://github.com/AmosWang0626/boot-singlejava
多模块demo:https://github.com/AmosWang0626/bootgit
此时不须要关注这是个spring boot项目,就按照java项目建立。github
右键Model,选择Add FrameWork Support...web
勾选上Maven便可spring
common ---> dao ---> core ---> webapp
web依赖core层,core层依赖dao层,dao依赖common层。maven
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> <relativePath/> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
<build> <finalName>boot</finalName> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <mainClass>cn.amos.web.BootWebApplication</mainClass> <layout>ZIP</layout> </configuration> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
@SpringBootApplication public class BootWebApplication { public static void main(String[] args) { SpringApplication.run(BootWebApplication.class, args); } }
@RestController public class HelloController { @RequestMapping("hello") public String sayHello() { return "Hello Spring boot!"; } }
server: port: 8085 context-path: /boot
最后测下吧 http://127.0.0.1:8085/boot/helloide
需求:在web层的controller里边打印一句core层返回的话spring-boot
========== HelloBusiness.java ====================================================== public interface HelloBusiness { /** * say hello * * @return String */ String sayHello(); } =========== HelloBusinessImpl.java ================================================== @Component("helloBusiness") public class HelloBusinessImpl implements HelloBusiness { @Override public String sayHello() { return this.getClass().getSimpleName() + " Say Hello!"; } } ========== HelloController.java ===================================================== @RestController public class HelloController { @Resource private HelloBusiness helloBusiness; @RequestMapping("hello") public String sayHello() { return helloBusiness.sayHello(); } } ===================================================================================
按照之前的思惟,这样写是对的测试
可是:运行的时候报错
A component required a bean of type 'cn.amos.core.business.HelloBusiness' that could not be found.
怎么办呢?
通过上时间的调试,准备在程序的入口WebApplication上入手
加上两个注解搞定,实现没问题,具体细节呢,详见文末。
@Configuration
@ComponentScan("cn.amos")
@Configuration @ComponentScan("cn.amos") @SpringBootApplication public class BootWebApplication { public static void main(String[] args) { SpringApplication.run(BootWebApplication.class, args); } }
http://127.0.0.1:8085/boot/hello
声明当前类是一个配置类
使用方式以下:
@ComponentScan("cn.amos")
使用方式以下:
@ComponentScans({@ComponentScan("cn.amos.core"), @ComponentScan("cn.amos.dao")})
@SpringBootApplication = @Configuration + @EnableAutoConfiguration + @ComponentScan
局限性:@SpringBootApplication里的@ComponentScan只能自动扫描当前包下全部使用@Service、@Component、@Repository和@Controller的类,并注册为bean。
因此为了扫描到其余模块下声明的bean,咱们须要从WebApplication类入手,有如下三种实现方式:
咱们须要扫描 boot-dao | boot-core | boot-web三层, 拿到使用@Component(@Service/@Repository/@Controller)注解的类. ==================================================================== 1.自我推荐方式,类不会被重复加载,没有用到默认的 @SpringBootApplication 这是一种捷径吧,包名通常都是cn.amos.*.*的,因此cn.amos能扫描全部类. @Configuration @ComponentScan("cn.amos") @EnableAutoConfiguration public class BootWebApplication { public static void main(String[] args) { SpringApplication.run(BootWebApplication.class, args); } } ==================================================================== 2.这种方式配置@ComponentScans,类也不会被重复加载,相对配置多了点 @Configuration @ComponentScans({@ComponentScan("cn.amos.core"), @ComponentScan("cn.amos.dao")}) @SpringBootApplication public class BootWebApplication { public static void main(String[] args) { SpringApplication.run(BootWebApplication.class, args); } } ==================================================================== 3.这种方式看起来简单,可是web层的bean被加载了两次,这就很差了 @Configuration @ComponentScan("cn.amos") @SpringBootApplication public class BootWebApplication { public static void main(String[] args) { SpringApplication.run(BootWebApplication.class, args); } }
感谢阅读,不当之处,望多多指教