【Spring】Spring容器装配Bean的三种方式

参考文献:https://www.cnblogs.com/jimisun/p/9742293.html

一、隐式的Bean发现机制和自动装配

Spring从两个角度来实现自动化装配;组件扫描(Spring自动发现应用中上下文所需要的创建的Bean),自动装配(Spring自动满足Bean之间的依赖)。

  • 使用@Component将普通Java类配置成SpringBean
  • 使用@Autowired(自动装配)使Spring满足Bean的依赖

在Java类中配置组件扫描

主配置类

@Configuration
@ComponentScan("com.wei")
public class ApplicationConfig {
}
@Data
@Component
public class User {
    @Autowired
    private Cat cat;

}
@Component
public class Cat {
    public void shot(){
        System.out.println("cat shot!!!");
    }

}

测试

	@Test
    public void fun2(){
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(ApplicationConfig.class);
        User bean = applicationContext.getBean(User.class);
        Cat cat = bean.getCat();
        cat.shot();
    }

在XML配置文件配置组件扫描

  • 与java类中配置组件扫描的区别仅在applicationContext.xml文件、主配置类和测试类上。

applicationContext.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"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/aop
       https://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx.xsd">
       
<context:component-scan base-package="com.wei.pojo" />

</beans>

测试

	@Test
    public void fun1(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        User bean = applicationContext.getBean(User.class);
        Cat cat = bean.getCat();
        cat.shot();
    }

二、在Java中进行装配

同样我们可以再Spring的Java配置类中对SpringBean进行配置

  • 使用 @Bean 注解将方法返回的实例对象添加到上下文中
  • 在@Bean返回的实例对象中可以通过构造器注入传入相关依赖
///java主配置类/
@Configuration
@ComponentScan("com.wei")
public class ApplicationConfig {
    @Bean
    public Dog dog(){
        return new Dog();
    }
}
///Dog 类/
public class Dog {
    public Dog(){
            System.out.println("Dog shot!!!");
    }
}
///测试类/
	@Test
    public void fun3(){
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(ApplicationConfig.class);
        Dog dog = applicationContext.getBean(Dog.class);
    }
///输出结果
Dog shot!!!

三、在XML中进行装配

  • 对于在XML中进行配置可能是经常使用的,在以前的Spring版本中几乎都是使用XML进行配置Spring。
<?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:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-    context.xsd
       http://www.springframework.org/schema/aop
       https://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- bean注入就可用,component需要扫描 -->
<context:component-scan base-package="com.wei.pojo" />

<bean id="dog" class="com.wei.pojo.Dog"></bean>

</beans>
//测试类
	@Test
    public void fun4(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        Dog dog = applicationContext.getBean(Dog.class);
    }

四、总结

  • 注意:在实际应用中如何不想被海量的标签埋没前,你应当优先使用隐式的Bean发现机制和自动装配和在Java中进行装配,最后再选择使用在XML中配置。

  • 实际项目中我们通常都会选择组合使用

    1. 隐式的Bean发现机制和Java中进行装配进行组合

    2. 隐式的Bean发现机制和XML配置进行组合

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

nickkkkkkkkk

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值