1.包结构

2.主程序类

 1 /**
2 * 主程序类
3 * @SpringBootApplication:这是一个springboot应用
4 *
5 * @SpringBootApplication
6 *
7 * 等同于下面的三个包
8 * @SpringBootConfiguration
9 * @EnableAutoConfiguration
10 * @ComponentScan(com.atguigu.boot) ---->默认组件扫描基础包是主程序类MainApplication所在包及其子包
11 *
12 *
13 */
14 @SpringBootApplication(scanBasePackages={"com.atguigu"})
15 public class MainApplication {
16 public static void main(String[] args) {
17 //1.返回IOC容器
18 ConfigurableApplicationContext run = SpringApplication.run(MainApplication.class, args);
19 //2.获取容器内所有组件的名称
20 String[] names = run.getBeanDefinitionNames();
21 for (String name : names) {
22 System.out.println(name);
23 }
24
25 //3.从容器中获取指定组件
26 User user01 = run.getBean("user01", User.class);
27 User user02 = run.getBean("user01", User.class);
28 System.out.println("使用同一个组件id,多次获取组件,是否是同一个组件实例:" + (user01 == user02));
29
30 //配置类本身也是组件
31 Myconfig myConfig = run.getBean(Myconfig.class);
32 Myconfig myConfig2 = run.getBean(Myconfig.class);
33 System.out.println(myConfig == myConfig2);
34
35 //4. 配置类默认情况下是@Configuration(proxyBeanMethods = true),代表配置类此时不是一个普通的类
36 //可以把它理解为代理bean,此时打印结果是 com.atguigu.boot.config.Myconfig$$EnhancerBySpringCGLIB$$a3f7b687@5b5caf08
37 //如果配置类上@Configuration(proxyBeanMethods = false),此时配置类就是一个普通类
38 //此时打印结果:com.atguigu.boot.config.Myconfig@6e28bb87
39 System.out.println(myConfig);
40
41 //5.如果@Configuration(proxyBeanMethods = true) 开启代理bean调用方法
42 //则当配置类对象调用其注册组件方法去获取相应组件时,Springboot总会检查组件是否在容器中
43 //保持组件单实例
44 User user1 = myConfig.user01();
45 User user2 = myConfig.user01();
46 //默认情况下@Configuration(proxyBeanMethods = true),打印结果是 true
47 //如果@Configuration(proxyBeanMethods = false),则打印结果是 false
48 System.out.println("配置类是作为代理bean从容器中获取单例组件呢?还是作为普通类调用方法呢? " + (user1 == user2));
49
50 User user3 = run.getBean("user01", User.class);
51 Pet tomcatPet = run.getBean("tomcatPet", Pet.class);
52 //如果@Configuration(proxyBeanMethods = true),
53 //此时容器中的user01组件依赖容器中的tomcatPet组件,返回 true
54 System.out.println("用户的宠物是否是容器中的那个单实例宠物组件? " + (user3.getPet() == tomcatPet));
55
56 }
57 }

3.bean包下的两个实体类

User

 1 public class User {
2 private String name;
3 private Integer age;
4
5 private Pet pet;
6
7 public User() {
8 }
9
10 public User(String name, Integer age) {
11 this.name = name;
12 this.age = age;
13 }
14
15 public Pet getPet() {
16 return pet;
17 }
18
19 public void setPet(Pet pet) {
20 this.pet = pet;
21 }
22
23 public String getName() {
24 return name;
25 }
26
27 public void setName(String name) {
28 this.name = name;
29 }
30
31 public Integer getAge() {
32 return age;
33 }
34
35 public void setAge(Integer age) {
36 this.age = age;
37 }
38
39 @Override
40 public String toString() {
41 return "User{" +
42 "name='" + name + '\'' +
43 ", age=" + age +
44 ", pet=" + pet +
45 '}';
46 }
47 }

Pet

 1 public class Pet {
2 private String name;
3
4 public Pet() {
5 }
6
7 public Pet(String name) {
8 this.name = name;
9 }
10
11 public String getName() {
12 return name;
13 }
14
15 public void setName(String name) {
16 this.name = name;
17 }
18
19 @Override
20 public String toString() {
21 return "Pet{" +
22 "name='" + name + '\'' +
23 '}';
24 }
25 }

4.config包下的配置类 Myconfig

 1 /**
2 1.配置类里面使用@Bean标注在方法上给容器注册组件,默认是单实例的
3 2.配置类本身也是组件
4 3.proxyBeanMethods:配置类是否作为代理bean来调用方法
5 如果 proxyBeanMethods = true 则是Full模式(重量级模式)
6 ---> 此模式下,配置类作为代理bean来调用方法,springboot都会去容器里面检查有没有这个组件,如果有,就使用容器中的组件,确保单实例,形成组件依赖
7 如果 proxyBeanMethods = false 则是Lite模式(轻量级模式)
8 ---> 此模式下,配置类是作为普通类调用方法,springboot不会去容器里面检查组件,不会形成组件依赖,项目启动运行快
9
10 4.最佳实战
11 配置类组件之间无依赖关系,用Lite模式加速容器启动过程,减少判断
12 配置类组件之间有依赖关系,方法会被调用得到之前单实例组件,用Full模式
13 */
14 @Configuration(proxyBeanMethods = false) //告诉Springbooot这是一个配置类
15 public class Myconfig {
16
17 /**
18 * 外部无论对配置类中的组件注册方法调用多少次,获取的都是之前注册在容器中的单实例对象
19 * @return
20 */
21
22 @Bean //给容器中添加组件,默认以方法名作为组件id(组件名)。返回类型就是组件类型,返回值就是组件在容器中的实例
23 public User user01() {
24 User user = new User("zhangsan", 20);
25 //如果@Configuration(proxyBeanMethods = true),
26 // 此时容器中的user01组件依赖容器中的tomcatPet组件
27 user.setPet(cat());
28 return user;
29 }
30
31
32 @Bean("tomcatPet") //此时组件名就是tomcatPet,而不是方法名了
33 public Pet cat() {
34 return new Pet("HelloKitty");
35 }
36 }

springboot注解之@Configuration 和 @Bean的更多相关文章

  1. 1spring注解:@Configuration,@Bean,@ComponentScan(),@Scope

    传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事物,这么做有两个缺点:1.如果所有的内容都配置在.xml文件中,那么.xml文件将会十分庞大:如果按需求分开.xml文件 ...

  2. SpringBoot自动装配原理之Configuration以及@Bean注解的使用

    Configuration以及Bean注解的使用 该知识点在Spring中应该学过,没有学过或者遗忘的的朋友需要预习或温习前置知识点.SpringBoot其实就是Spring的进一步简化,所以前置知识 ...

  3. Java框架spring Boot学习笔记(七):@Configuration,@bean注解

    @Configuration作用在类上,相当于一个xml文件 @bean作用于方法上,相当于xml配置中的<bean>标签 一个例子: 新建一个Springboot工程 新建一个User类 ...

  4. Springboot@Configuration和@Bean详解

    Springboot@Configuration和@Bean详解 一.@Configuration @Target({ElementType.TYPE}) @Retention(RetentionPo ...

  5. 14 - springboot的@Configuration、@Bean、@Import()、@ImportResource()、@Conditional说明

    1.@Configuration.@Bean.@Import().@ImportResource().@Conditional 分析源码的时候总会见到标题中的这几个注解,因此:弄一篇博客来说明一下吧, ...

  6. Spring @Configuration 和 @Bean 注解

    @Configuration 和 @Bean 注解 带有 @Configuration 的注解类表示这个类可以使用 Spring IoC 容器作为 bean 定义的来源.@Bean 注解告诉 Spri ...

  7. 【Spring注解开发】组件注册-使用@Configuration和@Bean给容器中注册组件

    写在前面 在之前的Spring版本中,我们只能通过写XML配置文件来定义我们的Bean,XML配置不仅繁琐,而且很容易出错,稍有不慎就会导致编写的应用程序各种报错,排查半天,发现是XML文件配置不对! ...

  8. SpringBoot 注解事务声明式事务

    转载请注明: http://www.cnblogs.com/guozp/articles/7446477.html springboot 对新人来说可能上手比springmvc要快,但是对于各位从sp ...

  9. springboot注解使用说明

    springboot注解 @RestController和@RequestMapping注解 我们的Example类上使用的第一个注解是 @RestController .这被称为一个构造型(ster ...

随机推荐

  1. HTTP笔记1--Web及网络基础

    web页面如何呈现? 客户端:通过发送请求获取服务器资源的 Web 浏览器 web是建立在 HTTP 协议上通信的   WWW(万维网/web)的构建技术 把 SGML(StandardGeneral ...

  2. RSA 加密解密使用实例

    http://www.dtmao.cc/news_show_692109.shtml 本文不讨论RSA加密解密本身,只记录使用方法及遇到的坑,RSA原理及注意事项可在网上查找. 背景:公司的一个需求, ...

  3. Kubernets二进制安装(5)之私有仓库harbor搭建

    在IP地址为192.168.80.50,机器名为mfyxw50上搭建私有仓库harbor harbor下载地址: harbor下载连接地址:https://github.com/goharbor/ha ...

  4. anaconda jupyter notebook 启动方法

    介绍 anaconda jupyter notebook是一种基于浏览器的python编译环境.(大概) 使用时可能因为浏览器缓存造成问题. 但是很方便. 启动方法 anaconda navigato ...

  5. Linux 应用开发----socket编程笔记

    Linux socket编程 套接字定义描述 套接字的域 AF_INET ====>IPv4 AF_INET6 ====>IPv6 AF_UNIX ====>unix 域 AF_UP ...

  6. Linux 驱动框架---dm9000分析

    前面学习了下Linux下的网络设备驱动程序的框架inux 驱动框架---net驱动框架,感觉知道了一个机器的大致结构还是不太清楚具体的细节处是怎么处理的,所以今天就来以dm9000这个网上教程最多的驱 ...

  7. Steam 钓鱼模拟器

    Steam 钓鱼模拟器 Fishing Planet Fishing Planet 是一个独特和高度现实的在线第一人称多人钓鱼模拟器,由狂热的钓鱼爱好者钓鱼给你带来实际钓鱼充分刺激开发! 选择你的诱饵 ...

  8. js & void() & void(0)

    js & void() & void(0) https://www.runoob.com/js/js-void.html void() <a href="javascr ...

  9. input number step

    input number step <!DOCTYPE html> <html> <body> <h1>The input step attribute ...

  10. SVG viewBox & coordinate system

    SVG viewBox & coordinate system https://codepen.io/xgqfrms/pen/abOOrjp <html> <body> ...