目录
1.注解组件
- @Configuration、@Bean、
- @Component(bean的属性注入)
- 衍生注解(@Controller、@Service、@Repository)
- @Conditional条件装配:当满足条件,则可以自动装配下面内容
- @Import:给容器中自动创建这个类型的组件 ,默认组件等等名字就是全类名
- @ImportResource:可以用于导入原生态的xml的bean装配
- @ComponentScan:将组件自动加载到容器,通过属性指定扫描(只要在主程序的包下或扫描下,都会一一自动扫描)
- @Component+@ConfigurationProperties:导入application.properties配置文件里面的属性值
- @EnableConfigurationProperties+@ConfigurationProperties:导入application.properties配置文件里面的属性值
2. Configuration
2.1 配置Pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
2.2 编写实体类
2.3 User
package com.node.boot.pojo;
public class User {
private String name;
private Integer age;
private Pet pet;
public User() {
}
public User(String name, Integer age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Pet getPet() {
return pet;
}
public void setPet(Pet pet) {
this.pet = pet;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
", pet=" + pet +
'}';
}
}
2.4 Pet
package com.node.boot.pojo;
public class Pet {
private String name;
public Pet(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Pet{" +
"name='" + name + '\'' +
'}';
}
}
2.3 编写Config
package com.node.boot.config;
import com.node.boot.pojo.Pet;
import com.node.boot.pojo.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
//@Import({User.class})给容器中自动创建这个类型的组件 ,默认组件等等名字就是全类名
@Configuration(proxyBeanMethods = true)//Configuration注解:告诉SpringBoot这是一个配置类 == 配置文件
/* proxyBeanMethods的理解:
1.他是代理bean的方法
2.当proxyBeanMethods = true的时候(Full模式),user组件依赖了Pet组件,则user调用Pet成立(看主程序)
3.当proxyBeanMethods = false的时候(Lite轻量级模式),user组件依赖了Pet组件,则user调用Pet不成立(看主程序)
4.Full(相当于单例模式):外部无论对配置类中的这个组件注册方法调用多少次获取的都是之前注册容器中的单实例对象,ture
5.最佳实战:当配置 类组件之间无依赖关系用Lite模式加速容器启动过程,减少判断;
当配置类组件之间有依赖关系,方法会被调用得到之前单实例组件,用Full模式
* */
public class MyConfig {
@Bean //给容器中添加组件。以方法名作为组件的id。返回类型就是组件类型。返回的值,就是组件在容器中的实例
public User user1(){
User zhangsan= new User("zhangsan", 18);
//user组件依赖了Pet组件
zhangsan.setPet(catPet());
return zhangsan;
}
@Bean
public Pet catPet(){
return new Pet("cat");
}
}
2.4 编写主程序
package com.node.boot.controller;
import com.node.boot.config.MyConfig;
import com.node.boot.pojo.Pet;
import com.node.boot.pojo.User;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan("com.node.boot")//将组件自动加载到容器,通过属性指定扫描(只要在主程序的包下或扫描下,都会一一自动扫描)
public class MainApplication {
public static void main(String[] args) {
//1.返回为我们的IOC容器
ConfigurableApplicationContext run= SpringApplication.run(MainApplication.class,args);
//2.查看容器里面的组件
String[] names=run.getBeanDefinitionNames();
for(String name:names){
System.out.println(name);
}
//3.从容器中获取组件(对象) 单例模式,不管调用多少次都是之前注册容器的单实例对象
Pet cat1 = run.getBean("catPet", Pet.class);
Pet cat2 = run.getBean("catPet", Pet.class);
System.out.println("组件:"+(cat1 == cat2));
//4.MyConfig也是一个组件
MyConfig bean=run.getBean(MyConfig.class);
System.out.println(bean);
//5.user组件依赖了Pet组件
User user=run.getBean("user1",User.class);
Pet cat=run.getBean("catPet",Pet.class);
System.out.println("用户的宠物:"+(user.getPet()==cat));
}
}
3.@Conditional条件装配
当满足条件,则可以自动装配下面内容
4.@ImportResource
可以用于导入原生态的xml的bean装配
在MyConfig类中
@ImportResource(“classpath.xml”)
即可导入Spring配置文件
5.导入application.properties配置文件里面的属性值
5.1 方法一:@Component+@ConfigurationProperties
5.1.1 实体类Car
package com.node.boot.pojo;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
public class Car {
private String brand;
private Integer price;
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public Integer getPrice() {
return price;
}
public void setPrice(Integer price) {
this.price = price;
}
@Override
public String toString() {
return "Car{" +
"brand='" + brand + '\'' +
", price=" + price +
'}';
}
}
5.1.2 application.properties
5.1.3 在实体类中绑定配置文件的属性
/*
* 只有在容器中的组件(@Component),才会拥有SpringBoot提供的强大功能
* */
@Component
@ConfigurationProperties(prefix = "mycar") //配置属性
5.1.4 测试
5.2 方法二:@EnableConfigurationProperties+@ConfigurationProperties
5.2.1 Car实体类
package com.node.boot.pojo;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
public class Car {
private String brand;
private Integer price;
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public Integer getPrice() {
return price;
}
public void setPrice(Integer price) {
this.price = price;
}
@Override
public String toString() {
return "Car{" +
"brand='" + brand + '\'' +
", price=" + price +
'}';
}
}
5.2.2 application.properties
5.2.3 在Myconfig中添加@EnableConfigurationProperties(Car.class)
package com.node.boot.config;
import com.node.boot.pojo.Car;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
@EnableConfigurationProperties(Car.class) //开启配置属性(开启Car配置绑定功能)+@ConfigurationProperties(prefix = "mycar") //配置属性
public class MyConfig {
}
5.2.4 在实体类中添加@ConfigurationProperties(prefix = “mycar”)
5.2.5 测试