添加组件

@Configuration、@Bean

//以swagger为例
@Configuration(proxyBeanMethods = false)
@EnableSwagger2 //使用swagger注解
public class SwaggerConfig { @Bean
public Docket webApiConfig(){
return new Docket(DocumentationType.SWAGGER_2)
.groupName("webApi")
.apiInfo(webApiInfo())
.select()
//.paths(Predicates.not(PathSelectors.regex("/admin/.*")))
.paths(Predicates.not(PathSelectors.regex("/error.*")))
.build(); }
}
  • @Configuration:告诉SpringBoot这是一个配置类 ,即配置文件,配置类也是组件

  • @Bean:添加组件。返回类型就是组件类型。返回值就是组件在容器中的实例,可以自定义组件名,默认组件名为方法名

proxyBeanMethods参数

  • true:full模式,表示通过@Bean注册的组件是单实例的,也是默认值。配置类中注册的组件每次被调用时,都会检查

    容器中是否已经存在该组件,若存在就不重新创建

  • false:lite模式,表示组件无论被调用多少次返回的组件都是新创建的,关闭了检查机制,组件每次被调用时都是重新创建

@Configuration(proxyBeanMethods = false)
public class MyConfig {
/*
给容器中添加组件。
以方法名作为组件的id。
返回类型是组件类型,返回值就是组件在容器中的实例
*/
@Bean
public User getUser(){
User lixiang = new User("莉香", 18);
//user组件依赖了Pet组件
lixiang.setPet(getPet());
return lixiang;
} @Bean
public Pet getPet(){
Pet pet = new Pet("哈士奇");
return pet;
}
}
@SpringBootApplication
public class Springboot01Application { public static void main(String[] args) {
//1、返回IOC容器
ConfigurableApplicationContext run = SpringApplication.run(Springboot01Application.class, args); //2、查看IOC容器里的组件
String[] names = run.getBeanDefinitionNames();
for (String name : names) {
System.out.println(name);
} //3、从容器中获取组件
MyConfig bean = run.getBean(MyConfig.class);
User user1 = bean.getUser();
User user2 = bean.getUser(); //User(userName=莉香, age=18, pet=Pet(name=哈士奇, weight=null))
System.out.println(user1); //org.springframework.aop.config.internalAutoProxyCreator,配置类是一个代理对象
System.out.println(bean); //4、测试proxyBeanMethods,默认开启
System.out.println(user1 == user2); //false Pet pet = run.getBean("getPet", Pet.class);
System.out.println("是否是同一个宠物:"+ (user1.getPet() == pet)); //是否是同一个宠物:false
} }

说明

  • 配置类的组件之间无依赖关系Lite模式加速容器启动过程,减少判断
  • 有依赖关系,用Full模式(默认),得到之前单实例组件,

@Bean、@Component、@Controller、@Service、@Repository

它们是Spring的基本标签,在Spring Boot中并未改变它们原来的功能

  • @Bean:初始化一个Spring IOC容器管理的新对象。
  • @Component: 被注解的类会自动被添加进Spring容器中
  • @Repository: 用于持久层
  • @Service: 表示被注解的类是位于业务层的业务组件。
  • @Controller :用于标注控制层
  • 以上组件的功能起始都是给IOC容器中注册组件,只是应用的场景不同
  • 只有组件在容器中才能访问容器中的其他组件,这也是为什么实现类要加@Service@component,因为实现类经常要使用@Autowired自动装配其他组件

@ComponentScan

public @interface ComponentScan {
@AliasFor("basePackages")
String[] value() default {}; @AliasFor("value")
String[] basePackages() default {}; ...
}
  • 启动类注解@SpringBootApplication的组合属性之一
  • @ComponentScan的功能其实就是自动扫描加载符合条件的组件(@Component,@Service,@Repository)或者用@Bean修饰加入到IOC容器中的组件
  • 我们可以通过basePackages属性定制@ComponentScan自动扫描的范围,如果不指定,则默认会从启动类所在包开始向下扫描

@Import

顾名思义,将已存在的类导入到IOC容器中

@Import({User.class, DBHelper.class})//给容器中自动创建出这两个类型的组件
@Configuration(proxyBeanMethods = false)
public class MyConfig {
}

@Conditional条件装配

满足Conditional指定的条件,则进行注入

F4打开继承结构,可以看到有很多判断条件,springboot底层使用该注解的地方很多,可自行了解

@ImportResource

原生配置文件引入:若项目中有用原先xml方式的配置文件,spring容器是获取不到配置文件的数据的,在配置类中加入该注解就可以获取xml文件配置的组件

<!--======================beans.xml=========================-->
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <bean id="haha" class="com.atguigu.boot.bean.User">
<property name="name" value="zhangsan"></property>
<property name="age" value="18"></property>
</bean> <bean id="hehe" class="com.atguigu.boot.bean.Pet">
<property name="name" value="tomcat"></property>
</bean>
</beans>
@Configuration
@ImportResource("classpath:beans.xml")
public class MyConfig { }

配置绑定

@ConfigurationProperties + @Component

读取到properties文件中的内容,并且把它封装到实体类中

@Data
@ToString
@Component //注册进容器中
@ConfigurationProperties(prefix = "mycar") //获取前缀带有mycar的配置
public class Car { private String brand; private Integer price;
}

类的属性必须有getset方法

#application.properties
mycar.brand=BYD
mycar.price=10000

@ConfigurationProperties只能给注册进容器的组件绑定配置,但有时调用的是第三方接口,若接口方实体类进容器,@ConfigurationProperties就无法使用,这时就要用到另一个注解

@EnableConfigurationProperties + @ConfigurationProperties

@EnableConfigurationProperties:获取配置文件的属性值同时会将实体类加入容器中

@Configuration
@EnableConfigurationProperties({Car.class})
public class MyConfig { }

@EnableConfigurationProperties的参数是数组形式,参数只有一个值时可以不加{},有多个参数时要加{},并用,隔开

//此时可以不加@Component
@Data
//@Component
@ConfigurationProperties(prefix = "mycar") //获取前缀带有mycar的配置
public class Car { private String brand; private Integer price;
}

最新文章

  1. Lesson 6 Percy Buttons
  2. 防止SQL注入的方法
  3. 把excel导入的自定义时间改成yyyyMMdd
  4. 【HDU2196 Computer】经典树形dp
  5. Canvas实现文字粒子化,并且绕轴旋转(完善)
  6. Form 表单常用正则验证 (收藏)
  7. BZOJ 1005 明明的烦恼 (组合数学)
  8. C语言中的system函数參数具体解释
  9. NodeJs之进程守护
  10. VS中调试时监控线程的最后错误代码
  11. php 不写闭合标签
  12. 学习ASP.NET Core Razor 编程系列十七——分组
  13. [物理学与PDEs]第4章第3节 一维反应流体力学方程组 3.3 一维反应流体力学方程组的数学结构
  14. HTML5+CSS3整体回顾
  15. POJ_2019_Cornfields
  16. net core web服务器实现
  17. c#socket TCP同步网络通信
  18. 原!上线遇到的问题, java序列化关键字transient 修饰的属性变成null了
  19. 基于Oracle的EntityFramework的WEBAPI2的实现(二)——使用DbFirst
  20. Codeigniter框架前后台部署(多目录部署)

热门文章

  1. 字体小于12px 无法缩小解决方案
  2. turtle color设置的几种方式
  3. Pycharm新建模板默认添加作者时间等信息(逼格更高,好像很历害的样子)
  4. Dapr + .NET Core实战(九)本地调试
  5. linux 测试2
  6. FastAPI(64)- Settings and Environment Variables 配置项和环境变量
  7. DL4J实战之二:鸢尾花分类
  8. 缓冲区溢出利用与ShellCode编写
  9. EPUB文件格式简单解析
  10. 【MySQL】MySQL(三)存储过程和函数、触发器、事务