• Spring 中的一些小知识点


    项目结构

    在这里插入图片描述

    public class Application {
        private static final Logger logger = LoggerFactory.getLogger(Application.class);
    
        public static void main(String[] args) {
            logger.info("start");
            ApplicationContext ac = new ClassPathXmlApplicationContext("app.xml");
            HelloWorld helloWorld =  ac.getBean("helloWorld",HelloWorld.class);
            helloWorld.printMessage();
            logger.info("end");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    // app.xml
    
    <beans
            xmlns="http://www.springframework.org/schema/beans"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
        
        <import resource="classpath*:app2.xml"/>
    
        <bean id="bean2" class="com.demo.BeanB"  />
    
    
        <bean id="beanA" class="com.demo.BeanA" />
    
        <bean id="helloWorld" class="com.demo.HelloWorld">
            <property name="message" value="weishui2"/>
            <property name="beanB" ref="beanB"/>
            <property name="beanA" ref="beanA"/>
    
        bean>
        
    beans>
    
    
    
    // app2.xml
    
    <beans
            xmlns="http://www.springframework.org/schema/beans"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
    
        <bean id="beanB" class="com.demo.BeanB" />
    
        <bean id="helloWorld" class="com.demo.HelloWorld">
            <property name="message" value="weishui1"/>
            <property name="beanB" ref="beanB"/>
            <property name="beanA" ref="beanA"/>
        bean>
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40

    规则0 bean在spring里面,生命周期大概分为两步

       第一步 先告诉Spring,自己的存在,自己都有哪些成员变量
       第二步 实例化 把需要的成员变量注入进去
    
    • 1
    • 2

    规则1 Bean的加载顺序和Xml里面配置的先后顺序一致。

    规则2 在xml里使用import resource=“classpath*:app2.xml”/> 相当于把目标xml里面的bean搬迁到本xml里,加载顺序和规则1一样。

    规则3 在xml里面bean class=“com.demo.BeanB” /> 这样初始化的Bean的id是com.demo.BeanB#0 就是全类名加一个#再加一个实例的编号。

    规则4 在同一个xml里 写两个bean,class和id都一样的情况下,会报错。

    规则5 在两个xml里面写两个相同的bean,不会报错,但是后面加载的那个bean会覆盖前面的那个。 相关资料:spring.main.allow-bean-definition- overriding

    规则6 @autowire 是默认按照Type来注入的。如果容器里有两个bean的class一样,那么autowire那边就会报错。因为spring不知道应该用哪个来注入。但是如果给某个bean上加一个primary,就说明这个类是首选,就不会报错了。

    <?xml version="1.0" encoding="UTF-8"?>
    <beans
            xmlns="http://www.springframework.org/schema/beans"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
    
        <bean id="helloWorld" class="com.demo.HelloWorld"/>
        <bean id="beanA" class="com.demo.BeanA" />
        <bean id="beanB" class="com.demo.BeanB" />
    
    </beans>
    
    
    package  com.demo;
    import lombok.Data;
    import org.springframework.beans.factory.annotation.Autowired;
    
    
    @Data
    public class HelloWorld {
    
        @Autowired
        private BeanA beanA;
    
        @Autowired
        private BeanB beanB;
    
        public HelloWorld(){
            System.out.println("init HelloWorld " + beanB);
        }
    
        private String message;
    
        public void say(){
            beanA.say();
            beanB.say();
        }
    
        public void printMessage(){
            System.out.println("Your Message : " + message);
        }
    
        public String getMessage() {
            return message;
        }
    
        public void setMessage(String message) {
            this.message = message;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50

    规则7 就上面那个case,HelloWorld 能正常实例化,BeanA和BeanB都能正常注入。

    public class HelloWorld {
    
        private  BeanA beanA;
    
        private  BeanB beanB;
    
        public HelloWorld(BeanA beanA1,BeanB beanB){
            this.beanA = beanA1;
            this.beanB = beanB;
            System.out.println("init HelloWorld " + beanA + " "+ beanB);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    规则8 就像上面这个case,两个成员变量虽然没有标注autowire,但是因为有构造函数的存在,BeanA和BeanB 依然能注入进来。

    规则9 在@Configuration 里面的@Bean, 加载顺序也是代码的编写顺序,越靠上的Bean,越先被初始化。

    规则10 当有@Configuration,@ImportResource,XML里面还有import的时候

    1 先加载ClassPathXmlApplicationContext("app.xml") 里面指定的文件里面的bean
    2 在加载上面那个xml里面import的xml的数据
    3 加载Configuration里面的bean
    4 ImportResource的xml里面的数据
    
    • 1
    • 2
    • 3
    • 4
  • 相关阅读:
    方案:AI赋能,森林防火可视化智能监管与风险预警系统解决方案
    个人在运行python代码过程中的坑230928
    不用rustup,Windows下gnu版Rust安装与开发环境配置
    【DPDK】使用 Open vSwitch * 采用 DPDK 帧间 VM NFV 应用程序
    Spring 源码阅读:用于创建 AOP 代理的后处理器分析
    Pytorch的variable和tensor区别
    [unity3d][通过代码]让模型移动,动态改变模型位置,点对点移动
    jquery ajax 提交json格式数据
    BCC源码内容概览(3)
    HTML---表单验证
  • 原文地址:https://blog.csdn.net/dlf123321/article/details/126433558