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");
    }
}
// app.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"
        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
<?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="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>

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

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

规则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;
    }
}

规则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);
    }

规则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里面的数据
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值