【Spring入门-02】Bean 的装配

本文介绍了 Spring Bean 的装配方式,包括通过 XML 配置文件、Java 代码装配,还阐述了自动化扫描装配(基于 Java 代码注解和 XML 配置文件),以及混合配置(在 JavaConfig 中引入 XML 配置、在 XML 配置中引入 JavaConfig),并给出参考代码和书籍。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Spring Bean 的装配

Spring 提供了多种类型的应用上下文:

  1. AnnotationConfigApplicationContext:基于 Java 的配置类中加载
  2. ClassPathXmlApplicationContext:从类路径下基于 XML 的配置文件中加载
  3. FileSystemXmlApplicationContext:从文件系统下基于 XML 的配置文件中加载

通过 XML 配置文件装配 bean

定义一个 XmlBean 类,其中依赖 OtherBean

public class XmlBean {

    private String name;
    private OtherBean otherBean;

    public XmlBean(String name, OtherBean otherBean) {
        this.name = name;
        this.otherBean = otherBean;
        System.out.println("通过XML配置文件装配Bean的构造方法");
    }

    @Override
    public String toString() {
        return "XmlBean{" +
                "name='" + name + '\'' +
                ", otherBean=" + otherBean +
                '}';
    }
}

public class OtherBean {
}

在 bean.xml 配置文件中通过 <bean> 元素声明一个 bean 。<constructor-arg> 构造器注入初始化 bean ,其中 value 属性表明给定的值以字面量的形式注入到构造器中, ref 属性表明正在装配的是一个引用。

<?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="otherBean" class="com.chen.bean.assembly.xml.OtherBean" />

    <bean id="xmlBean" class="com.chen.bean.assembly.xml.XmlBean">
        <constructor-arg index="0" type="java.lang.String" value="xml"/>
        <constructor-arg index="1" type="com.chen.bean.assembly.xml.OtherBean" ref="otherBean" />
    </bean>

</beans>

使用 Junit 测试 Bean 是否装配成功

import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import static org.junit.Assert.*;


public class XmlBeanTest {

    private ApplicationContext context;

    @Before
    public void before() {
        context = new ClassPathXmlApplicationContext("bean.xml");
    }

    @Test
    public void test() {
        XmlBean bean = context.getBean(XmlBean.class);
        assertNotNull(bean);
        System.out.println(bean);
    }
}

通过 Java 代码装配 bean

定义一个简单的 JavaBean 对象

public class JavaBean {

    public JavaBean() {
        System.out.println("通过Java代码装配Bean的构造方法");
    }
}

通过 @Configuration 表明该类为 Bean 的配置类
通过 @Bean 配置和初始化 JavaBean 交给 Spring IoC 容器管理

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class JavaBeanConfig {

    @Bean
    public JavaBean javaBean(){
        return new JavaBean();
    }
}

使用 Junit 测试 Bean 是否装配成功

import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import static org.junit.Assert.assertNotNull;

public class JavaBeanConfigTest {

    private ApplicationContext context;

    @Before
    public void before() {
        context = new AnnotationConfigApplicationContext(JavaBeanConfig.class);
    }

    @Test
    public void test() {
        assertNotNull(context.getBean(JavaBean.class));
    }
}

自动化扫描装配 bean

Spring 从两个角度来实现自动化装配:

  1. 组件扫描(component scanning):Spring 会自动发现应用上下文中所创建的 bean
  2. 自动装配(autowiring):Spring 自动满足 bean 之间的依赖

1. 基于 Java 代码注解

使用 @Component 定义 AutowireBean 是 Spring 的一个组件类,其中可以通过 value 属性来设置 bean 的别名

import org.springframework.stereotype.Component;

@Component(value = "bean")
public class AutowireBean {

    public AutowireBean() {
        System.out.println("自动装配Bean的构造方法");
    }
}

通过 @CompoentScan 启用 Spring 的组件扫描,默认扫描与配置类相同的包以及子包,其中可以通过 basePackages 属性指定扫描的包名,还可以通过 basePackageClasses 属性指定扫描的类名

import org.springframework.context.annotation.ComponentScan;

@ComponentScan(basePackageClasses = AutowireBeanConfig.class)
public class AutowireBeanConfig {
}

使用 Junit 测试 Bean 是否自动装配成功

import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import static org.junit.Assert.assertNotNull;

public class AutowireAutowireBeanConfigTest {

    private ApplicationContext context;

    @Before
    public void before() {
        context = new AnnotationConfigApplicationContext(AutowireBeanConfig.class);
    }

    @Test
    public void test() {
        assertNotNull(context.getBean(AutowireBean.class));
        assertNotNull(context.getBean("bean"));
    }
}

2. 基于 XML 配置文件

使用 Spring context 命名空间的 <context:component-scan> 元素开启自动扫描装配

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

    <context:component-scan base-package="com.chen.bean.assembly.autowire"/>

</beans>

混合配置

1. 在 JavaConfig 中引入 XML 配置

@Import(BeanConfig.class) :引入基于 Java 代码配置的 BeanConfig
@ImportResource("classpath:bean.xml") :引入基于 XML 配置的 bean.xml

@Configuration
@Import(BeanConfig.class)
@ImportResource("classpath:bean.xml")
public class JavaBeanConfig {
}

2. 在 XML 配置中引入 JavaConfig

<bean>:引入基于 Java 代码配置的 JavaBeanConfig
<import>:导入基于 XML 配置 的 bean.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: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/beans/spring-context.xsd">

    <bean class="JavaBeanConfig" />
    <import resource="bean.xml" />

</beans>

参考

  1. GitHub 代码
  2. Spring 实战(第4版)
内容概要:本文档详细介绍了基于MATLAB实现多目标差分进化(MODE)算法进行无人机三维路径规划的项目实例。项目旨在提升无人机在复杂三维环境中路径规划的精度、实时性、多目标协调处理能力、障碍物避让能力和路径平滑性。通过引入多目标差分进化算法,项目解决了传统路径规划算法在动态环境和多目标优化中的不足,实现了路径长度、飞行安全距离、能耗等多个目标的协调优化。文档涵盖了环境建模、路径编码、多目标优化策略、障碍物检测与避让、路径平滑处理等关键技术模块,并提供了部分MATLAB代码示例。 适合人群:具备一定编程基础,对无人机路径规划和多目标优化算法感兴趣的科研人员、工程师和研究生。 使用场景及目标:①适用于无人机在军事侦察、环境监测、灾害救援、物流运输、城市管理等领域的三维路径规划;②通过多目标差分进化算法,优化路径长度、飞行安全距离、能耗等多目标,提升无人机任务执行效率和安全性;③解决动态环境变化、实时路径调整和复杂障碍物避让等问题。 其他说明:项目采用模块化设计,便于集成不同的优化目标和动态环境因素,支持后续算法升级与功能扩展。通过系统实现和仿真实验验证,项目不仅提升了理论研究的实用价值,还为无人机智能自主飞行提供了技术基础。文档提供了详细的代码示例,有助于读者深入理解和实践该项目。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值