此时不需要关注这是个spring boot项目,就按照java项目创建。
右键Model,选择Add FrameWork Support...
勾选上Maven即可
common ---> dao ---> core ---> web
web依赖core层,core层依赖dao层,dao依赖common层。
<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>
@SpringBootApplicationpublic class BootWebApplication { public static void main(String[] args) { SpringApplication.run(BootWebApplication.class, args); }}
@RestControllerpublic class HelloController { @RequestMapping("hello") public String sayHello() { return "Hello Spring boot!"; }}
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 =====================================================@RestControllerpublic 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")@SpringBootApplicationpublic 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")@EnableAutoConfigurationpublic 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")})@SpringBootApplicationpublic class BootWebApplication { public static void main(String[] args) { SpringApplication.run(BootWebApplication.class, args); }}====================================================================3.这种方式看起来简单,但是web层的bean被加载了两次,这就不好了@Configuration@ComponentScan("cn.amos")@SpringBootApplicationpublic class BootWebApplication { public static void main(String[] args) { SpringApplication.run(BootWebApplication.class, args); }}
感谢阅读,不当之处,望多多指教
联系客服