博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【Spring Boot】4.组件注解
阅读量:3958 次
发布时间:2019-05-24

本文共 6956 字,大约阅读时间需要 23 分钟。

目录

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

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

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 测试

在这里插入图片描述

在这里插入图片描述

转载地址:http://cuazi.baihongyu.com/

你可能感兴趣的文章
codeforces——1097B,Petr and a Combination Lock(搜索)
查看>>
杭电ACM——2064,汉诺塔III(递推)
查看>>
杭电ACM——2065,"红色病毒"问题(思维)
查看>>
北大ACM——2385,Apple Catching(DP)
查看>>
杭电AM——2072,单词数(暴力)
查看>>
杭电ACM——2073,无限的路(思维)
查看>>
杭电ACM——2069,Coin Change(DP)
查看>>
杭电ACM——2074,叠筐
查看>>
北大ACM——3616,Milking Time(DP)
查看>>
杭电ACM——2076,夹角有多大
查看>>
牛客练习赛43——B Tachibana Kanade Loves Probability(暴力,思维)
查看>>
牛客第十七届上海大学程序设计春季联赛——E CSL 的魔法(贪心)
查看>>
杭电ACM——1028,Ignatius and the Princess III(母函数)
查看>>
杭电ACM——1171,Big Event in HDU(母函数)
查看>>
杭电ACM——6491,时间间隔(思维)
查看>>
杭电AC——1085,Holding Bin-Laden Captive!(母函数)
查看>>
杭电ACM——2110,Crisis of HDU(母函数)
查看>>
杭电AM——2152,Fruit(母函数)
查看>>
杭电ACM——2566,统计硬币(DP)
查看>>
堆栈(数据结构)
查看>>