Spring 之Bean的装配

Spring Bean的装配分为3中:

1、隐式发现和自动装配

  @Component:将一个Java类声明为Bean(组件类),等待Spring扫描发现。

  @ComponentScan:启用组件扫描将带有@Component的类实例化为Bean,一般用在配置类上,可指定扫描基础包,默认与配置类相同的包。

  @Configuration:声明一个Java类是配置类,它与@Component作用相当,只是更为直观,一般@Bean与其连用。

  <context:component-scan base-package="...">:通过XML方式启用组件扫描。

  @Autowired:为bean中的属性自动注入,前提是该类必须声明为一个bean,如添加@Component

  @Bean:将方法的返回对象作为bean,前提是该类必须是一个bean,如添加@Configuration

2、通过Java代码装配

  该部分就是@Configuration和@Bean的运用,通过手动创建对象,然后在配置成Bean。

3、通过XML配置

  xml配置规范:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:c="http://www.springframework.org/schema/c"
       xmlns:p="http://www.springframework.org/schema/p"
       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">

</beans>

  在<beans></beans>中声明需要的bean,<bean id="xxx" class="全类名" &others.../>,或<bean ...>some configuration</bean>的形式。

  借助构造器注入初始化bean:使用<constructor-arg> or c命名空间(需要引入c命名空间,如上述xml头部的xmlns:c=...)。

    使用<constructor-arg>:

    

<bean id="xxx" class="com.example.xxx">
    <constructor-arg ref="bean-id"/>
    <constructor-arg value="value"/>
    <constructor-arg><null/></constructor-arg>
</bean>

    使用c命名空间:

<bean id="xxx" class="com.example.xxx"
          c:xxx-ref = "bean-id"
          c:_xxx = "constant value"
/>
<!-- 还可以根据参数顺序命名-->
<bean id="xxx" class="com.example.xxx"
          c:_0-ref = "bean-id"
          c:_1 = "constant value"
/>
<!-- 当只有一个参数时,直接用c:_-ref或c:_=即可-->

    但c命名空间有限制,当构造参数传入一个集合时,只能使用<constructor-arg>:

  

<bean id="xxx" class="com.example.xxx">
    <constructor-arg ref="bean-id"/>
    <constructor-arg value="value"/>
    <constructor-arg><null/></constructor-arg>
    <constructor-arg>
        <list>
            <ref bean="b1"/>
            <ref bean="b2"/>
            <ref bean="b3"/>
        </list>
    </constructor-arg>
    <constructor-arg><!--set and constant-->
        <set>
            <value>one</value>
            <value>two</value>
            <value>three</value>
        </set>
    </constructor-arg>
</bean>

  设置属性

<bean id="xxx" class="com.example.xxx">
    <property name="property-name" ref="bean-id"/>
    <property name="property-name" value="constant-value"/>
    <property name="property-name">
        <list>
           ...
        </list>
    </property>
</bean>    

    或者用p命名空间(在xml头部用xmlns:p引入)

<bean id="xxx" class="com.example.xxx"
          p:p1-ref = "bean-id"
          p:p2 = "constant-value"
/> 

<!--p命名空间不可用顺序来确定属性,同样无法处理集合类-->

  Spring util命名空间简化集合类:将集合变成一个bean

<!--使用util:list创建一个list的bean-->
<util:list id = "bean-id">
    <ref bean = "bean-id"/>
    <!-- or <value> tag -->
</util:list>

4、导入和混合配置

  JavaConfig引用xml配置:在配置类上使用@ImportResource注解导入xml,如@ImportResource("classpath:beans.xml")

JavaConfig互相引用:在配置类上使用@Import注解,如@Import(class-name.class)

XML引用XML:<import resource="beans.xml"/>

5、JUnit单元测试

maven引入JUnit和spring-test:

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.0.4.RELEASE</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>RELEASE</version>
        </dependency>

  编写测试类:

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import soundsystem.CDPlayerConfig;
import soundsystem.CompactDisc;
import soundsystem.MediaPlayer;

/**
 * Author:
 * Date:2018/3/14 15:21
 * Email:
 * Comment:
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = CDPlayerConfig.class)
public class CDPlayerTest {

    @Autowired
    private MediaPlayer player;

    @Autowired
    private CompactDisc cd;

    @Test
    public void cdShouldNotBeNull() {
        Assert.assertNotNull(cd);
    }

    @Test
    public void playTest() {
        player.play();
    }
}

持续更新...

原文地址:https://www.cnblogs.com/jackcharles/p/8572850.html

时间: 2024-08-01 07:29:06

Spring 之Bean的装配的相关文章

Spring温故而知新 - bean的装配(续)

按条件装配bean 就是当满足特定的条件时Spring容器才创建Bean,Spring中通过@Conditional注解来实现条件化配置bean package com.sl.ioc; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Conditional; import org.springframework.context.annotation.

Spring笔记2——Spring中Bean的装配

1.引言 Spring中,对象无需自己负责查找或创建与其关联的其他对象,而是由容器负责把需要相互协作的对象引用赋予各个对象.创建应用对象之间的协作关系的行为通常称为装配(Wiring),这也是依赖注入的本质. 2.声明Bean 配置Bean的方式主要有两种:基于XML文件的配置方式和基于Java注解的配置方式.传统的基于XML文件的配置方式在声明Bean时,Spring配置文件的根元素来源于Spring beans命名空间所定义的<beans>元素.除了beans命名空间外,Java自带了多种

&lt;Spring实战&gt;2:装配Bean

1 声明Bean 1.1 创建 Spring 配置 Spring 容器提供两种配置 Bean 的方式:xml 配置和基于注解配置. Spring 配置文件: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/

Spring实战3:装配bean的进阶知识

主要内容: Environments and profiles Conditional bean declaration 处理自动装配的歧义 bean的作用域 The Spring Expression Language 在装配bean—依赖注入的本质一文中,我们探讨了Spring的三种管理bean的方式:自动装配.基于JavaConfig.基于XML文件.这篇文字将探讨一些Spring中关于bean的管理的高级知识,这些技能你可能不会每天都用,但是非常重要. 3.1 Environments

Spring之IOC&amp;DI/装配Bean(一)

简介 今天学习Spring的第一天,无非也就是入门基础知识.主要了解IOC和DI部分,要熟练掌握哦~ Spring简介 1. Spring介绍 Spring是一个非常活跃的开源框架:它是一个基于Core来构架多层JavaEE系统的框架,它的主要目地是简化企业开发. Spring以一种非侵入式的方式来管理你的代码,Spring提倡"最少侵入",这也就意味着你可以适当的时候安装或卸载Spring 2. Spring框架的优势 ?方便解耦,简化开发 ?Spring就是一个大工厂,可以将所有对

Spring bean依赖注入、bean的装配及相关注解

依赖注入 Spring主要提供以下两种方法用于依赖注入 基于属性Setter方法注入 基于构造方法注入 Setter方法注入 例子: public class Communication { private Messaging messaging; /* * DI via Setter */ public void setMessaging(Messaging messaging){ this.messaging = messaging; } public void communicate(){

Spring 实战-第二章-装配Bean

Bean是Spring对象的声明(装配wiring),要使用Spring,必须先装配需要使用的对象,有3种装配的方式 自动化装配Bean 通过Java代码装配 通过XML装配 自动化装配Bean 自动化装配Bean很简单 1.声明接口 package soundsystem; public interface CompactDisc { void play(); } 2.添加注解 package soundsystem; import org.springframework.stereotype

Spring学习总结之装配bean

1.  XML中显式配置 规范,文件头: <?xml version="1.0" encoding="UTF-8"?>            <beans xmlns=http://www.springframework.org/schema/beans                     xmlns=http://www.w3.org/2001/XMLSchema-instance                     xsi:schema

Spring -- Bean自动装配&amp;Bean之间关系&amp;Bean的作用域

Bean的自动装配 Spring IOC 容器可以自动装配 Bean. 需要做的仅仅是在 的 autowire 属性里指定自动装配的模式 有以下几种自动装配的类型: byType(根据类型自动装配): 若 IOC 容器中有多个与目标 Bean 类型一致的 Bean. 在这种情况下, Spring 将无法判定哪个 Bean 最合适该属性, 所以不能执行自动装配. byName(根据名称自动装配): 必须将目标 Bean 的名称和属性名设置的完全相同. constructor(通过构造器自动装配):