一、创建项目
此时不需要关注这是个spring boot项目,就按照java项目创建。
1、外层创建一个空项目,不用勾选;

2、删除src目录;

3、创建四个模块,都选择quickstart,包括web模块也是;
- boot-common
- boot-core
- boot-dao
- boot-web

4、打开右侧Maven Project,如果有某层是灰度显示的话

右键Model,选择Add FrameWork Support...

勾选上Maven即可

5、配置依赖关系
common ---> dao ---> core ---> web
web依赖core层,core层依赖dao层,dao依赖common层。
二、基础配置
1、首先是外层pom.xml
<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>
2、其次是boot-web层配置,这里边要加上构建的配置
<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>
3、创建WebApplication类,设置main启动
@SpringBootApplication
public class BootWebApplication {
public static void main(String[] args) {
SpringApplication.run(BootWebApplication.class, args);
}
}
4、添加Controller,测试demo
@RestController
public class HelloController {
@RequestMapping("hello")
public String sayHello() {
return "Hello Spring boot!";
}
}
5、web层resources里添加application.yml
server:
port: 8085
context-path: /boot
最后测下吧 http://127.0.0.1:8085/boot/hello
三、简单逻辑
需求:在web层的controller里边打印一句core层返回的话
========== 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

五、注解备注:
1、@Configuration
声明当前类是一个配置类
2、@ComponentScan
使用方式如下:
@ComponentScan("cn.amos")
3、@ComponentScans
使用方式如下:
@ComponentScans({@ComponentScan("cn.amos.core"), @ComponentScan("cn.amos.dao")})
4、@SpringBootApplication
@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);
}
}
感谢阅读,不当之处,望多多指教
|