本文共 6956 字,大约阅读时间需要 23 分钟。
org.springframework.boot spring-boot-starter-parent 2.3.4.RELEASE org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-maven-plugin
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 + '}'; }}
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 + '\'' + '}'; }}
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"); }}
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)); }}
当满足条件,则可以自动装配下面内容
可以用于导入原生态的xml的bean装配
在MyConfig类中 @ImportResource(“classpath.xml”) 即可导入Spring配置文件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 + '}'; }}
/** 只有在容器中的组件(@Component),才会拥有SpringBoot提供的强大功能 * */@Component@ConfigurationProperties(prefix = "mycar") //配置属性
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 + '}'; }}
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 { }
转载地址:http://cuazi.baihongyu.com/