Spring实战之(1)装配Bean

1. 自动化装配Bean

1.@ComponentScan

@ComponentScan 配置在类上,如果没有设置
属性 basePackages(即values)的值来指定包,会扫描该类所在包及其子包下的 @Component注解, 将其注册为 bean 对象

实例:

 

4262551-af9cef24c01ea19b.png

类的结构图

@ComponentScan
public class StudentHelper {
}

其他类上都包含 @Component注解 ,StudentHelper 类上有 @ComponentScan 注解

测试:


@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = StudentHelper.class)  // 加载StudentHelper类
public class Test01 {

    @Autowired
    private StudentDao studentDao;

    @Autowired
    private AnotherTeacher anotherTeacher;
    @Autowired
    private Teacher teacher;


    @Test
    public void test1() {
        studentDao.eat();
    }

    @Test
    public void test2() {
        teacher.teach();
        anotherTeacher.sayHello();
    }
}

执行test1 、test2 均输出正确结果,说明 StudentDaoImpl 、 Teacher、 AnotherTeacher 被成功扫描并注册为 Bean

2.使用JavaConfig装配Bean

该方式不再在原来的Bean上添加注解@Component或 @ComponentScan或 @Autowired, 而是创建单独的JavaConfig类,用于创建各种Bean对象

用@Configuration注解该类,等价 在XML中配置beans;用@Bean标注方法等价于XML中配置bean。

 

4262551-33aefdd33b2902a6.png

类的结构

public class Phone {
    public void call(){
        System.out.println("打电话");
    }
}
public class Computer {
    public void code(){
        System.out.println("敲代码");
    }
}

public class Student {

    private Computer computer;

    private Phone phone;
    public Student(Computer computer,Phone phone){
        this.computer = computer;
        this.phone=  phone;
    }

    public void study(){
        System.out.println("学习");
    }

    public void code(){
        computer.code();
    }

    public void call(){
        phone.call();
    }

}

配置Bean的类:

@Configuration   // 要加@Configuration 注解
public class StudentConfig {

    @Bean   // Bean注解
    public Phone phone(){
        return new Phone();
    }
    @Bean
    public Computer computer(){
        return new Computer();
    }

    @Bean(name="student")  // 默认Bean的name 即为方法名
    public Student student(Computer computer,Phone phone){  
        return new Student(computer, phone);
    }
}

测试:

public class TestBean {
    @Test                // 通过手动加载的方式测试
    public void b() {
        ApplicationContext context = new AnnotationConfigApplicationContext("com.example.bean");  // 注意,这里是配置类 StudentConfig 所在包的包名,不是
        Student student = context.getBean("student", Student.class);
        student.call();
        student.code();
        student.study();
    }
}

也可以通过上面例子中依赖注入的方式获取Student对象,进行测试

3.导入和混合配制(JavaConfig和XML)

1.JavaConfig中引用Xml

当 StudentConfig 中Bean太多,需要进行拆分

1)以JavaConfig方式分离出去

例如:将 Computer 这个Bean 单独拆分出去, 新建ComputerConfig 配置Bean

@Configuration
public class ComputerConfig {
    @Bean
    public Computer computer(){
        return new Computer();
    }
}

现在要考虑的就是将 独立出去的ComputerConfig 和 原先的 StudentConfig 联系到一起

方法一: 在StudentConfig类上 添加 @Import(ComputerConfig.class)

@Configuration
@Import(ComputerConfig.class)
public class StudentConfig {
    @Bean
    public Phone phone(){
        return new Phone();
    }
    @Bean(name="student")
    public Student student(Computer computer,Phone phone){
        return new Student(computer, phone);
    }
}

方法二: 创建一个联合类,将ComputerConfig和 StudentConfig 组合在一起

@Configuration
@Import({StudentConfig.class,ComputerConfig.class})
public class CombinedConfig {
}

2)以xml方式分离出去

classpath: 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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="computer" class="com.example.bean.Computer"/>
</beans>

@ImportResource("") , 在JavaConfig类中引用Xml配置的Bean对象

创建组合类,通过@ImportResource和@Import 将Computer 类与 StudentConfig类进行组合

@Configuration
@Import(StudentConfig.class)
@ImportResource("classpath:applicationContext.xml")
public class CombinedConfig {
}

2.在Xml中引用JavaConfig类

假设 StudentComputer原本都配置在applicationContext.xml中,现需要拆分 Bean 配置,将Computer拆分出去到computer.xml,在 原来的xml中需要用 import 导入Computer`的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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">
    <import resource="classpath: computer.xml" />
    <bean id="computer" class="com.example.bean.Student"/>
</beans>

如果 ComputerJavaConfig的方式拆分出去,那么在上面的xml 配置文件如何引用 Computer对应的JavaConfig呢? import 标签只能导入 其他xml文件

ComputerConfig.java

@Configuration
public class ComputerConfig {
    @Bean
    public Computer computer(){
        return new Computer();
    }
}

方法一: 在applicationContext.xml 配置 ComputerConfig.java的bean

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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean class="com.example.bean.ComputerConfig" />

    <bean id="student" class="com.example.bean.Student">
        <constructor-arg ref="computer"/>  <!--和ComputerConfig中方法computer名保持一致-->
        <constructor-arg ref="phone"/>
    </bean>
    <bean id="phone" class="com.example.bean.Phone" />
</beans>

方法二: 使用第三方xml 文件,组合 ComputerConfigapplicationContext.xml

combinedContext.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.xsd">

    <bean class="com.example.bean.ComputerConfig" />
    <import resource="classpath:applicationContext.xml" />
</beans>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Spring Boot自动装配bean是指Spring Boot根据一定的规则自动创建和配置bean,而不需要手动在配置文件中进行配置。这种自动装配的方式可以大大简化开发人员的工作,提高开发效率。Spring Boot会根据类路径下的依赖和配置文件,自动扫描并创建bean,同时也可以通过注解和配置文件进行自定义的配置。这种自动装配的方式是Spring Boot的一个重要特性,也是其受欢迎的原因之一。 ### 回答2: Spring Boot自动装配bean是Spring Boot框架的一个重要功能,在实践中也是经常使用的功能。通过Spring Boot自动装配bean,可以将代码的复杂性降低到最低限度,使得开发人员能够更专注于业务逻辑的实现。 Spring Boot自动装配bean的核心机制是基于条件化。当Spring Boot检测到某些条件满足时,才会自动装配相应的bean。这些条件可以是环境变量、配置文件、类路径、系统属性等。这样做的好处在于,可以避免因为无关的bean而导致的性能下降和资源的浪费。 Spring Boot自动装配bean也可以通过注解来实现。开发人员可以使用@Configuration注解声明一个类,然后在该类中使用@Bean注解将某个对象交给Spring容器管理。Spring Boot会在应用启动时自动扫描所有含有@Configuration注解的类,并初始化它们中的所有@Bean对象。使用这种方式,可以更加方便地管理bean之间的依赖关系和调用顺序。 另外,Spring Boot还提供了一些自动装配的注解,如@AutoConfigureBefore和@AutoConfigureAfter。这些注解可以用来指定bean的加载顺序,以免出现因为顺序问题而导致的错误。此外,Spring Boot还提供了@Conditional注解,它可以将某个bean的加载与特定条件相关联,只有当条件满足时,才会进行自动装配。 总之,Spring Boot自动装配bean对于简化代码结构、提高代码可读性和可维护性有着重要的意义。掌握这一机制,能够帮助开发人员更加高效地开发和维护Spring Boot应用。 ### 回答3: Spring Boot提供了自动装配bean的功能,其核心原则是约定优于配置。自动装配bean的机制是基于Spring的自动装配功能之上的,它能够从类路径上扫描所有的类,自动为这些类创建相应的bean,无需手动配置或编写XML文件。 在Spring Boot应用程序中,自动装配bean的过程分为两个步骤:组件扫描和依赖注入。 在组件扫描过程中,Spring Boot会扫描classpath中所有的包和类,找到带有注解的组件(比如@Component、@Service、@Repository和@Controller等),然后自动将这些组件注册为bean,以供其他组件使用。 在依赖注入过程中,Spring Boot会根据组件之间的依赖关系和配置信息,自动注入交叉依赖的bean,以完成整个应用程序的配置。 Spring Boot还提供了一些特殊的注解,用于细粒度地控制bean的自动装配行为。比如,@Conditional注解可以根据条件选择是否注册该bean,@Primary注解可以指定一个默认的bean实现,@Qualifier注解可以根据名称或类型区分相同类型的bean。 总之,Spring Boot的自动装配bean机制在很大程度上简化了应用程序的配置工作,增加了开发效率和代码可读性。但是,对于复杂的应用程序和完全自定义的bean,仍然需要手动配置和编写XML文件。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值