Spring Bean自动装配

简介: 1.通过ID自动装配Spring Bean自动装配可以自动搜索beans.xml中的bean,进行自动装配,这样就可以使开发人员不必显示的声明它例如,Person.java中包含了猫和狗的实体类对象


1.通过ID自动装配


Spring Bean自动装配可以自动搜索beans.xml中的bean,进行自动装配,这样就可以使开发人员不必显示的声明它

例如,Person.java中包含了猫和狗的实体类对象


public class Person {
    private String name;
    private Dog dog;
    private Cat cat;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Dog getDog() {
        return dog;
    }
    public void setDog(Dog dog) {
        this.dog = dog;
    }
    public Cat getCat() {
        return cat;
    }
    public void setCat(Cat cat) {
        this.cat = cat;
    }
    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", dog=" + dog +
                ", cat=" + cat +
                '}';
    }
}


现在我们可以通过自动装配机制引入猫和狗的类对象:

autowire="byName"是通过bean的ID引入的


<bean id="cat" class="top.imustctf.pojo.Cat"/>
<bean id="dog" class="top.imustctf.pojo.Dog"/>
<bean id="person" class="top.imustctf.pojo.Person" autowire="byName">
    <property name="name" value="dahe"/>
</bean>


测试类:


@Test
public void test() {
    // 获取Spring的上下文对象
    ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
    Person person = context.getBean("person", Person.class);
    person.getDog().shout();  // 汪汪汪!
    person.getCat().shout();  // 喵喵喵!
}


2.通过类型自动装配


autowire="byType"是通过bean的class引入的


<bean id="cat" class="top.imustctf.pojo.Cat"/>
<bean id="dog" class="top.imustctf.pojo.Dog"/>
<bean id="person" class="top.imustctf.pojo.Person" autowire="byType">
    <property name="name" value="dahe"/>
</bean>


3.注解自动装配


注解自动装配的前期环境配置:


导入context约束


xmlns:context="http://www.springframework.org/schema/context"


以及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">


配置注解的支持

最终的配置文件效果:


<?xml version="1.0" encoding="UTF-8"?>
<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">
    <context:annotation-config/>
</beans>


Autowired


Autowired可以实现自动装配机制:

很简单!我们只需要在pojo类中添加如下的注解即可实现自动装配:


@Autowired
private Dog dog;
@Autowired
private Cat cat;


现在我们可以去掉可恶的autowire参数,来尽情测试吧!


<bean id="person" class="top.imustctf.pojo.Person">
    <property name="name" value="dahe"/>
</bean>


Autowired默认按照Type进行匹配,如果配置文件中存在相同的类型,我们可以使用Qualifier注解来指定使用id进行匹配


<bean id="myDog" class="top.imustctf.pojo.Dog"/>


@Autowired
@Qualifier(value = "myDog")
private Dog dog;
目录
打赏
0
0
0
0
38
分享
相关文章
|
21天前
|
Spring IOC—基于注解配置和管理Bean 万字详解(通俗易懂)
Spring 第三节 IOC——基于注解配置和管理Bean 万字详解!
113 26
【Spring】获取Bean对象需要哪些注解
@Conntroller,@Service,@Repository,@Component,@Configuration,关于Bean对象的五个常用注解
【Spring】IoC和DI,控制反转,Bean对象的获取方式
IoC,DI,控制反转容器,Bean的基本常识,类注解@Controller,获取Bean对象的常用三种方式
Spring容器Bean之XML配置方式
通过对以上内容的掌握,开发人员可以灵活地使用Spring的XML配置方式来管理应用程序的Bean,提高代码的模块化和可维护性。
75 6
🌱 深入Spring的心脏:Bean配置的艺术与实践 🌟
本文深入探讨了Spring框架中Bean配置的奥秘,从基本概念到XML配置文件的使用,再到静态工厂方式实例化Bean的详细步骤,通过实际代码示例帮助读者更好地理解和应用Spring的Bean配置。希望对你的Spring开发之旅有所助益。
177 4
实战指南:四种调整 Spring Bean 初始化顺序的方案
本文探讨了如何调整 Spring Boot 中 Bean 的初始化顺序,以满足业务需求。文章通过四种方案进行了详细分析: 1. **方案一 (@Order)**:通过 `@Order` 注解设置 Bean 的初始化顺序,但发现 `@PostConstruct` 会影响顺序。 2. **方案二 (SmartInitializingSingleton)**:在所有单例 Bean 初始化后执行额外的初始化工作,但无法精确控制特定 Bean 的顺序。 3. **方案三 (@DependsOn)**:通过 `@DependsOn` 注解指定 Bean 之间的依赖关系,成功实现顺序控制,但耦合性较高。
156 4
实战指南:四种调整 Spring Bean 初始化顺序的方案
Spring容器中的bean是线程安全的吗?
Spring容器中的bean默认为单例模式,多线程环境下若操作共享成员变量,易引发线程安全问题。Spring未对单例bean做线程安全处理,需开发者自行解决。通常,Spring bean(如Controller、Service、Dao)无状态变化,故多为线程安全。若涉及线程安全问题,可通过编码或设置bean作用域为prototype解决。
53 1
Spring从入门到入土(bean的一些子标签及注解的使用)
本文详细介绍了Spring框架中Bean的创建和使用,包括使用XML配置文件中的标签和注解来创建和管理Bean,以及如何通过构造器、Setter方法和属性注入来配置Bean。
108 9
Spring从入门到入土(bean的一些子标签及注解的使用)
Spring框架中Bean是如何加载的?从底层源码入手,详细解读Bean的创建流程
从底层源码入手,通过代码示例,追踪AnnotationConfigApplicationContext加载配置类、启动Spring容器的整个流程,并对IOC、BeanDefinition、PostProcesser等相关概念进行解释
466 24