zoukankan      html  css  js  c++  java
  • SpringBoot底层原理

    SpringBoot直接可以用@Autowried原理是什么?

    以前我们使用@Autowired要自己创建Bean:https://www.cnblogs.com/maomaodesu/p/12180477.html#创建bean

    但SpringBoot在引入坐标时会自动创建Bean,这种绑定坐标创建Bean的方式我们也可以手动实现:

    创建一个Bean比如User,
    通过注解方式把Bean配置到SpringIOC容器中:写一个userConfig配置类,使用到@Configuration注解和@Bean注解(底层又用到@Enable*和@Import)

                                最常用的定义Bean的方式:
                                创建配置类,在配置类中定义Bean
    		        @Configuration
    		        public class UserConfig{
    			        @Bean
    			        public User user(){
    			        	return new User();
    			        }
    		        }
    

    使用getBean("user")方法是可以从IOC容器中创建出user对象的。
    在userConfig类中加入@Condition条件注解,新建一个Condition类来充当条件(该类的返回值是true或者flase)
    如果让Condition类一直返回false,则使用getBean(User.class)方法是无法从IOC容器中找到user的。
    总结一下就是,SpringBoot通过这种方式让Condition类的判断依据为是否导入某坐标,这样就可以产生一旦导入该坐标就允许直接从IOC里创建该对象而无需手写Bean的现象。

  • 相关阅读:
    输入输出重定向
    MarkdownPad 2中编辑
    (转)Maven最佳实践:划分模块
    (转)maven设置内存
    我收集的sonar参考资料
    (转)linux service理解
    制作service服务,shell脚本小例子(来自网络)
    6
    4
    5
  • 原文地址:https://www.cnblogs.com/maomaodesu/p/12553541.html
Copyright © 2011-2022 走看看