Spring-bean

本文探讨了Spring Boot中手动配置XML中的对象装配与自动装配的两种方式,包括属性注入和注解驱动的装配,以及bean的作用域设置。重点介绍了byName和byType装配策略,以及如何通过注解实现依赖注入。
摘要由CSDN通过智能技术生成

一、手动装配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
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="cat" class="com.xiaolu.pojo.Cat"></bean>
    <bean id="dog" class="com.xiaolu.pojo.Dog"></bean>
    <bean id="people" class="com.xiaolu.pojo.People">
        <property name="name" value="李四"></property>
        <property name="cat" ref="cat"></property>
        <property name="dog" ref="dog"></property>
    </bean>
</beans>

需要自己在xml里面添加属性的配置

<property name="name" value="李四"></property>
<property name="cat" ref="cat"></property>
<property name="dog" ref="dog"></property>
public class MyTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
        People people = (People) context.getBean("people");
        people.getCat().shout();
        people.getDog().shout();

    }
}

二、bean的自动装配

spring自动在上下文中寻找配置的对象装配。

通过byname

使用autowire=byName

<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="cat" class="com.xiaolu.pojo.Cat"></bean>
    <bean id="dog" class="com.xiaolu.pojo.Dog"></bean>
    <bean id="people" class="com.xiaolu.pojo.People" autowire="byName">
        <property name="name" value="李四"></property>
    </bean>
</beans>

通过bytype

同理使用autowire=byType

通过注解

1、开启注解方式装配

<context:annotation-config></context:annotation-config>
<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
        https://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="cat" class="com.xiaolu.pojo.Cat"></bean>
    <bean id="dog" class="com.xiaolu.pojo.Dog"></bean>
    <bean id="people" class="com.xiaolu.pojo.People">
        <property name="name" value="李四"></property>
    </bean>
    <context:annotation-config></context:annotation-config>
</beans>

2、在需要装配的位置使用注解@Autowired

public class People {
    @Autowired
    private Cat cat;
    @Autowired
    private Dog dog;
    private String name;

    public Cat getCat() {
        return cat;
    }
    public void setCat(Cat cat) {
        this.cat = cat;
    }
    public Dog getDog() {
        return dog;
    }
    public void setDog(Dog dog) {
        this.dog = dog;
    }
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    @Override
    public String toString() {
        return "People{" +
                "cat=" + cat +
                ", dog=" + dog +
                ", name='" + name + '\'' +
                '}';
    }
}

三、bean的作用域

  • singleton
  • prototype
  • request
  • session
  • application
  • websocket
    默认为单例模式。
    可以通过scope修改
 <bean id="cat" class="com.xiaolu.pojo.Cat" scope="prototype"></bean>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值