今日立春,SpringBoot! 简单springboot项目搭建开始。
准备新建springboot项目
jar 文件类型,jdk版本为8,启用lombok和开发测试工具,
生成的pom文件里,增加maven的版本信息否则pom第一行报错
<properties> <java.version>1.8</java.version> <maven-jar-plugin.version>3.1.1</maven-jar-plugin.version> </properties>
开始:
1,新建 domain 类 User,
// 安装lombok后,启用lombok注解 @Data @AllArgsConstructor @NoArgsConstructor @ToString public class User { private int id; private String name; private String address; }
2,新建config类,并且用@Configuration,标识为一个配置类。且实例化一个User
@Configuration public class UserConfig { @Bean public User user() { return new User(1,"xiaoming","beijing"); } }
3. 运行测试类
@SpringBootTest class DemoApplicationTests { @Autowired private User user; @Test void contextLoads() { System.out.println(user); } }
4、结果
2021-02-03 09:59:20.443 INFO 5644 --- [ main] cn.taotao.DemoApplicationTests : Started DemoApplicationTests in 2.97 seconds (JVM running for 5.125) User(id=1, name=xiaoming, address=beijing) 2021-02-03 09:59:20.829 INFO 5644 --- [extShutdownHook] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor'
5、报错信息,
2021-02-03 11:57:09.301 ERROR 9872 --- [ main] o.s.test.context.TestContextManager : Caught exception while allowing TestExecutionListener [org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener@4de4b452] to prepare test instance [com.example.demo.DemoApplicationTests@2d746ce4]
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.example.demo.DemoApplicationTests': Unsatisfied dependency expressed through field 'user'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.example.domain.User' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:643) ~[spring-beans-5.3.2.jar:5.3.2]
提示:expected at least 1 bean which qualifies as autowire candidate. 至少有一个bean被实例化
无法实例化bean,原因往往目录结构不对,导致不自动扫描
自动扫描的默认配置是,主程序application入口,必须在各个包之上,各个子包不是继承自入口包,需要在application的注解文件中加入scanBasePackages的注解
即使是Test的测试程序,也必须在application的入口处,加入scanbasepackages的注解
package com.example.demo; //这个入口在demo包下,但是配置文件和实体bean在example下
//指明scan哪个父包,否则不会实例化差异包下的bean,甚至连Test也运行不正常 @SpringBootApplication(scanBasePackages = "com.example") public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
或者,用@ComponentScan 注解,扫描父包,默认扫描执行入口包 及其子包。
@SpringBootApplication @ComponentScan(basePackages = "com.example") public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
另外还有常用的注解;
@Import (value ={configA.class,configB.class}) ,引入其他的配置文件
@Qulifier ("user1") 合格者,当注入的bean不是一个的时候,用来标识唯一。