單模塊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); } }
感謝閱讀,不當之處,望多多指教