Spring Boot简介
Sping Boot 所具备的特征有:
开发环境配置
<profile> <id>jdk‐1.8</id> <activation> <activeByDefault>true</activeByDefault> <jdk>1.8</jdk> </activation> <properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion> </properties> </profile>
创建一个简单的案例,新建maven工程项目,如下图
1 <parent> 2 <groupId>org.springframework.boot</groupId> 3 <artifactId>spring-boot-starter-parent</artifactId> 4 <version>2.2.2.RELEASE</version> 5 </parent> 6 7 <dependencies> 8 <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web --> 9 <dependency> 10 <groupId>org.springframework.boot</groupId> 11 <artifactId>spring-boot-starter-web</artifactId> 12 <version>2.2.1.RELEASE</version> 13 </dependency> 14 </dependencies>
1 /** 2 * @SpringBootApplication 来标注一个主程序类,说明这是一个Spring Boot应用 3 */ 4 @SpringBootApplication 5 public class HelloWorldMainApplication { 6 public static void main(String[] args) { 7 // Spring应用启动起来 8 SpringApplication.run(HelloWorldMainApplication.class,args); 9 } 10 }
1 @Controller 2 public class HelloController { 3 @ResponseBody 4 @RequestMapping("/hello") 5 public String hello(){ 6 return "Hello World!"; 7 } 8 }
1 @AutoConfigurationPackage 2 @Import(EnableAutoConfigurationImportSelector.class) 3 public @interface EnableAutoConfiguration {
继续进入
@AutoConfigurationPackage的注解底层代码
1 @Target({ElementType.TYPE}) 2 @Retention(RetentionPolicy.RUNTIME) 3 @Documented 4 @Inherited 5 @Import({Registrar.class}) 6 public @interface AutoConfigurationPackage { 7 }
在进入到Registar的底层
1 static class Registrar implements ImportBeanDefinitionRegistrar, DeterminableImports { 2 Registrar() { 3 } 4 5 public void registerBeanDefinitions(AnnotationMetadata metadata, BeanDefinitionRegistry registry) { 6 AutoConfigurationPackages.register(registry, (new AutoConfigurationPackages.PackageImport(metadata)).getPackageName()); 7 } 8 9 public Set<Object> determineImports(AnnotationMetadata metadata) { 10 return Collections.singleton(new AutoConfigurationPackages.PackageImport(metadata)); 11 } 12 }
@Import(AutoConfigurationPackage.Registar.class):Spring的底层注解@Import,给容器中导入一个组件;导入的组件由
AutoConfigurationPackages.Registrar.class;
将主配置类(@SpringBootApplication标注的类)的所在包及下面所有子包里面的所有组件扫描到Spring容器;
将所有需要导入的组件以全类名的方式返回;这些组件就会被添加到容器中;
会给容器中导入非常多的自动配置类(xxxAutoConfiguration);就是给容器中导入这个场景需要的所有组件,
并配置好这些组件;
有了自动配置类,免去了我们手动编写配置注入功能组件等的工作;
有了自动配置类,免去了我们手动编写配置注入功能组件等的工作;
SpringFactoriesLoader.loadFactoryNames(EnableAutoConfiguration.class,classLoader);
Spring Boot在启动的时候从类路径下的META-INF/spring.factories中获取EnableAutoConfiguration指定的值,将
这些值作为自动配置类导入到容器中,自动配置类就生效,帮我们进行自动配置工作;以前我们需要自己配置的东西,自动配置类都帮我们;
J2EE的整体整合解决方案和自动配置都在spring-boot-autoconfigure-1.5.9.RELEASE.jar;
Spring Boot的热部署
1 <dependency> 2 <groupId>org.springframework.boot</groupId> 3 <artifactId>spring-boot-devtools</artifactId> 4 <optional>true</optional> 5 </dependency>
Spring Boot的配置文件
application.yml
例如:yaml中的配置文件的方式:
server: port: 8081
yaml语法
属性和值也是大小写敏感;
1 package zh.stu.springboot.Model; 2 3 import org.springframework.boot.context.properties.ConfigurationProperties; 4 import org.springframework.stereotype.Component; 5 6 import java.util.Date; 7 import java.util.List; 8 import java.util.Map; 9 10 /** 11 * @ConfigurationProperties("person") 使用来读取application.yaml文件中的配置,其中里面的参数表示,读取的是以person开头的配置 12 * 13 * 只有这个组件是容器的组件,才能容器提供的@ConfigurationProperties功能 14 */ 15 @Component 16 @ConfigurationProperties(prefix="person") 17 public class Person { 18 String lastName; 19 20 int age; 21 22 boolean boss; 23 24 Date birth; 25 26 Map<String,String> maps; 27 28 List<String> list; 29 30 dog dog; 31 32 @Override 33 public String toString() { 34 return "Person{" + 35 "lastName=‘" + lastName + ‘\‘‘ + 36 ", age=" + age + 37 ", boss=" + boss + 38 ", birth=" + birth + 39 ", maps=" + maps + 40 ", list=" + list + 41 ", dog=" + dog + 42 ‘}‘; 43 } 44 45 public zh.stu.springboot.Model.dog getDog() { 46 return dog; 47 } 48 49 public void setDog(zh.stu.springboot.Model.dog dog) { 50 this.dog = dog; 51 } 52 53 public String getLastName() { 54 return lastName; 55 } 56 57 public void setLastName(String lastName) { 58 this.lastName = lastName; 59 } 60 61 public int getAge() { 62 return age; 63 } 64 65 public void setAge(int age) { 66 this.age = age; 67 } 68 69 public boolean isBoss() { 70 return boss; 71 } 72 73 public void setBoss(boolean boss) { 74 this.boss = boss; 75 } 76 77 public Date getBirth() { 78 return birth; 79 } 80 81 public void setBirth(Date birth) { 82 this.birth = birth; 83 } 84 85 public Map<String, String> getMaps() { 86 return maps; 87 } 88 89 public void setMaps(Map<String, String> maps) { 90 this.maps = maps; 91 } 92 93 public List<String> getList() { 94 return list; 95 } 96 97 public void setList(List<String> list) { 98 this.list = list; 99 } 100 101 }
1 package zh.stu.springboot.Model; 2 3 public class dog { 4 5 private String name; 6 7 @Override 8 public String toString() { 9 return "dog{" + 10 "name=‘" + name + ‘\‘‘ + 11 ‘}‘; 12 } 13 14 public String getName() { 15 return name; 16 } 17 18 public void setName(String name) { 19 this.name = name; 20 } 21 }
1 <!‐‐导入配置文件处理器,配置文件进行绑定就会有提示‐‐> 2 <dependency> 3 <groupId>org.springframework.boot</groupId> 4 <artifactId>spring‐boot‐configuration‐processor</artifactId> 5 <optional>true</optional> 6 </dependency>
1 person: 2 lastName: zhangH 3 age: 23 4 boss: false 5 birth: 2020/2/2 6 maps: {1:1,2:2} 7 list: [cat,dog] 8 dog: 9 name: dudu
1 package zh.stu.springboot; 2 3 import org.junit.jupiter.api.Test; 4 import org.springframework.beans.factory.annotation.Autowired; 5 import org.springframework.boot.test.context.SpringBootTest; 6 import org.springframework.context.ApplicationContext; 7 import zh.stu.springboot.Model.Person; 8 9 import java.beans.PersistenceDelegate; 10 11 @SpringBootTest 12 public class yamltest { 13 14 @Autowired 15 Person person; 16 17 @Autowired 18 ApplicationContext context; 19 @Test 20 public void contextload(){ 21 System.out.println(person); 22 } 23 }
Person{lastName=‘zhangH‘, age=23, boss=false, birth=Sun Feb 02 00:00:00 CST 2020, maps={61=, 122=}, list=[cat, dog], dog=dog{name=‘dudu‘}}
prefix = "person":配置文件中哪个下面的所有属性进行一一映射。只有这个组件是容器中的组件,才能容器提供的@ConfigurationProperties功能;
@ConfigurationProperties(prefix = "person")默认从全局配置文件中获取值;
@PropertySource(value = {"classpath:person.properties"})
@ImportResource这个注解例如:
@ImportResource(locations = {"classpath:beans.xml"})
使用@Bean给容器中添加组件
1 /** 2 * @Configuration:指明当前类是一个配置类;就是来替代之前的Spring配置文件 3 * 4 * 在配置文件中用<bean><bean/>标签添加组件 5 * * 6 / 7 @Configuration 8 public class MyAppConfig { 9 //将方法的返回值添加到容器中;容器中这个组件默认的id就是方法名 10 @Bean 11 public HelloService helloService02(){ 12 System.out.println("配置类@Bean给容器中添加组件了..."); 13 return new HelloService(); 14 } 15 }
原文:https://www.cnblogs.com/LBJLAKERS/p/12001253.html