Spring - 02 - 自动化装配Bean

【Spring】— Spring中Bean的装配方式 在Spring实例化Bean的过程中,Spring首先会调用Bean的默认构造方法来实例化Bean对象,然后通过反射的方式调用setter()方法来注入属性值。在Spring中,尽管使用XML配置文件可以实现Bean的装配工作,但如果应用中有很多Bean,就会导致XML配置文件过于臃肿,给以后的维护和升级工作带来一定的困难。虽然使用注解的方式装配Bean在一定程度上减少了配置文件中的代码量,但是也有企业项目中是没有使用注解方式开发的,那么有没有什么办法既可以减少代码量,又能够实现Bean的装配呢? 阅读详情


上面一篇文章中,学习了"Spring - 01 - 通过XML装配Bean",下面学习自动化装配Bean


自动化装配Bean


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

组件扫描:Spring会自动发现应用上下文中所创建的bean

自动装配:Spring自动满足bean之间的依赖


一、创建可被发现的bean

直接上代码

package com.test.spring.server.test4;

/**
 * Created by CYX on 2018/4/17.
 */
public interface CompactDisc {

    void play();

}
package com.test.spring.server.test4;

import org.springframework.stereotype.Component;

/**
 * @author CYX
 * @create 2018-04-17-20:47
 */
//该注解表明该类会作为组件类,并告知Spring要为这个类创建bean。
@Component
public class SgtPeppers implements CompactDisc {

    private String title = "Sgt. Pepper's Lonely Hearts Club Band";
    private String artist = "The Beatles";

    @Override
    public void play() {
        System.out.println("Playing " + title + " by " + artist);
    }
}
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:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">

    <context:component-scan base-package="com.test.spring.server.test4"/>

</beans>

通过XML启用组件扫描

测试主类

package com.test.spring.server.test4;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * 测试组件扫描
 *
 * @author CYX
 * @create 2018-04-17-21:08
 */
public class Test4App {

    public static void main(String[] args) {

        ApplicationContext context = new ClassPathXmlApplicationContext("/spring/test4/applicationContext.xml");

        SgtPeppers sgtPeppers = context.getBean("sgtPeppers",SgtPeppers.class);

        sgtPeppers.play();
    }

}
输出结果:


和之前 在XML中显示配置Bean(Spring - 01 - 通过XML装配Bean) 对比一下,我们并没有在applicationContext.xml配置文件中配置<bean>元素用来显示的声明bean。

只是在需要创建bean对象的类上增加了一个注解@Component。

@Component注解的作用是 把普通pojo对象 实例化到Spring容器中,相当于applicationContext.xml配置文件中的:

<bean id="sgtPeppers" class="com.test.spring.server.test4.SgtPeppers"></bean>
Spring Framework 5.0.5.RELEASE API  的解释:
Indicates that an annotated class is a "component". 
Such classes are considered as candidates for auto-detection when using annotation-based configuration and classpath scanning.
表示注释类是“组件”。
当使用基于注释的配置和类路径扫描时,这些类被认为是自动检测的候选对象。

当我们的类不属于各种归类时,我们就可以使用@Component来标注这个类。被标注的类,可以认为是"组件"

不过这里要注意,仅仅使用@Component注解时,我们并没有指定bean 的id,默认使用类名的首字母小写。

<context:component-scan>配置的作用是 开启组件自动扫描

<context:component-scan base-package="com.test.spring.server.test4"/>的含义是,扫描com.test.spring.server.test4包下的所有注解。



二、为组件扫描的bean命名

Spring应用 上下文 中所有的bean都会给定一个id。

在前面的例子中,我们并没有明确的为 SgtPeppers 指定 bean-id;

但Spring会为其指定一个id,具体的讲,这个bean所给定的id,为 sgtPeppers,也就是将类名的第一个字母变为小写。

如果要为这个bean设置不同的bean-id,需要将 期望id 作为值,传递给@Component注解。

例如:

@Component("sssgtPeppers")
public class SgtPeppers implements CompactDisc {

    private String title = "Sgt. Pepper's Lonely Hearts Club Band";
    private String artist = "The Beatles";

    @Override
    public void play() {
        System.out.println("Playing " + title + " by " + artist);
    }
}
修改下主方法的bean id,其他不用修改,看下运行结果:



还有一种为bean命名的方式,这种方式不使用@Component注解,而是使用Java依赖注入规范所提供的的@Named注解来设置。

注意:使用@Named注解,需要添加依赖

<dependency>
        <groupId>javax.inject</groupId>
        <artifactId>javax.inject</artifactId>
        <version>1</version>
</dependency>
@Named("test4-sgtPeppers")
public class SgtPeppers implements CompactDisc {

    private String title = "Sgt. Pepper's Lonely Hearts Club Band";
    private String artist = "The Beatles";

    @Override
    public void play() {
        System.out.println("Playing " + title + " by " + artist);
    }
}

修改下主方法代码,看下运行结果:


Spring支持将@Named作为@Component注解的替代方案。

不过还是推荐@Component注解



三、使用Java Config自动装配Bean

在上面代码的基础上修改一下

package com.test.spring.server.test4;

/**
 * Created by CYX on 2018/4/17.
 */
public interface CompactDisc {

    void play();

}
package com.test.spring.server.test4;


import javax.inject.Named;

/**
 * @author CYX
 * @create 2018-04-17-20:47
 */
//该注解表明该类会作为组件类,并告知Spring要为这个类创建bean。
//@Component("sgtPeppers")
//@Component("sssgtPeppers")
@Named("test4-sgtPeppers")
public class SgtPeppers implements CompactDisc {

    private String title = "Sgt. Pepper's Lonely Hearts Club Band";
    private String artist = "The Beatles";

    @Override
    public void play() {
        System.out.println("Playing " + title + " by " + artist);
    }
}
package com.test.spring.server.test4;

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

/**
 * 类CDPlayerConfig通过Java代码定义了Spring的装配规则。
 * <p>
 * 在后面我们再详细介绍
 * <p>
 * 这里,我们只需要观察一下CDPlayerConfig类并没有显示的声明任何bean。
 * <p>
 * 只不过它使用了@ComponentScan注解,这个注解能够在Spring中启用组件扫描。
 * <p>
 * 如果没有其他配置的话,@ComponentScan默认会扫描与配置类相同的包。
 * <p>
 * Spring将会扫描这个包以及这个包下所有的子包,查找带有@Component注解的类。
 *
 * 这种方式,我们暂时注释掉,使用XML来开启组件扫描。
 *
 * @author CYX
 * @create 2018-04-17-20:57
 */
@Configuration
@ComponentScan(basePackages = "com.test.spring.server.test4")
public class CDPlayerConfig {
}
package com.test.spring.server.test4;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * 测试组件扫描
 *
 * @author CYX
 * @create 2018-04-17-21:08
 */
public class Test4App {


    public static void main(String[] args) {

//        ApplicationContext context = new ClassPathXmlApplicationContext("/spring/test4/applicationContext.xml");

        ApplicationContext context =new AnnotationConfigApplicationContext(CDPlayerConfig.class);
        SgtPeppers sgtPeppers = context.getBean("test4-sgtPeppers",SgtPeppers.class);

        sgtPeppers.play();

    }

}

注意:这里我并没有使用applicationContext.xml,将之前XML配置文件中的

<context:component-scan base-package="com.test.spring.server.test4"/>注释掉,所以并没有贴出来。

输出结果:


创建Java Config类的关键在于为其添加@Configuration注解。
@Configuration注解表明这个类是一个配置类,该类应该包含在Spring应用上下文中如何创建bean的细节。


@ComponentScan注解默认会扫描与配置类相同的包,以及这个包下面所有的子包。

然后查找带有@Component注解,找到的话,就会为他们创建bean实例。



四、通过为bean添加注解实现自动装配

自动装配就是让Spring自动满足bean依赖的一种方法;

在满足依赖的过程中,会在Spring应用上下文中寻找某个bean需求的其他bean。

为了声明要进行自动装配,可以借助Spring 的@Autowired注解

代码优先

package com.test.spring.server.test05;

/**
 * Created by CYX on 2018/4/17.
 */
public interface CompactDisc {

    void play();

}
package com.test.spring.server.test05;

import org.springframework.stereotype.Component;

/**
 * @author CYX
 * @create 2018-04-30-17:09
 */
@Component
public class SayHello {

    public SayHello() {
        System.out.println("SayHello 默认构造器");
    }

    public void sayHelloWithName() {
        System.out.println("SayHello :hello cyx");
    }

    public void sayHelloWithAddress() {
        System.out.println("SayHello :hello 南京");
    }

}
package com.test.spring.server.test05;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author CYX
 * @create 2018-04-17-20:47
 */
@Component("sssgtPeppers")
public class SgtPeppers implements CompactDisc {

    private String title = "Sgt. Pepper's Lonely Hearts Club Band";
    private String artist = "The Beatles";

//    @Autowired
    private SayHello sayHello;

    /*@Autowired
    public void setSayHello(SayHello sayHello) {
        this.sayHello = sayHello;
    }*/

    @Autowired
    public SgtPeppers(SayHello sayHello) {
        this.sayHello = sayHello;
    }

    @Override
    public void play() {
        System.out.println("Playing " + title + " by " + artist);
        sayHello.sayHelloWithAddress();
        sayHello.sayHelloWithName();
    }
}
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:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">


    <context:component-scan base-package="com.test.spring.server.test05"/>

</beans>

主方法

package com.test.spring.server.test05;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * 测试组件扫描
 *
 * @author CYX
 * @create 2018-04-17-21:08
 */
public class Test5App {


    public static void main(String[] args) {

        ApplicationContext context = new ClassPathXmlApplicationContext("/spring/test05/applicationContext05.xml");

        SgtPeppers sgtPeppers = context.getBean("sssgtPeppers", SgtPeppers.class);

        sgtPeppers.play();

    }

}

运行结果:


@Autowired 注解不仅可以放在成员变量、setter方法上,还可以放在构造器上。



五、通过Java代码装配Bean

在上面,我们已经初步使用过通过Java Config来装配Bean,下面详细的了解一下。

代码优先:

package com.test.spring.server.test06;

/**
 * Created by CYX on 2018/4/17.
 */
public interface CompactDisc {

    void play();

}
package com.test.spring.server.test06;


/**
 * @author CYX
 * @create 2018-04-17-20:47
 */
public class SgtPeppers implements CompactDisc {

    private String title = "Sgt. Pepper's Lonely Hearts Club Band";
    private String artist = "The Beatles";


    public SgtPeppers() {
    }

    public SgtPeppers(String title, String artist) {
        this.title = title;
        this.artist = artist;
    }

    @Override
    public void play() {
        System.out.println("Playing " + title + " by " + artist);
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getArtist() {
        return artist;
    }

    public void setArtist(String artist) {
        this.artist = artist;
    }
}
package com.test.spring.server.test06;

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

/**
 * @author CYX
 * @create 2018-04-17-20:57
 */
@Configuration
@ComponentScan(basePackages = "com.test.spring.server.test06")
public class CDPlayerConfig {

    @Bean(name = "sssgtPeppers")
    public SgtPeppers initializationSgtPeppers() {
        return new SgtPeppers("888888","99999");
    }

}

主方法

package com.test.spring.server.test06;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * 测试组件扫描
 *
 * @author CYX
 * @create 2018-04-17-21:08
 */
public class Test6App {


    public static void main(String[] args) {

        ApplicationContext context = new AnnotationConfigApplicationContext(CDPlayerConfig.class);
        SgtPeppers sgtPeppers = context.getBean("sssgtPeppers", SgtPeppers.class);

        sgtPeppers.play();

    }

}

主方法


注意:

SgtPeppers类中已经没有 @Component 注解了。

SgtPeppers类 的实例化,在 CDPlayerConfig类中实现。

@Bean注解 会告诉Spring这个方法将会返回一个对象,该对象要注册为Spring应用上下文中的bean。

@Bean(name = "sssgtPeppers") 同时,我们还为bean 设置了id。

在默认情况下,bean 的id 与带有@Bean注解的方法名是一样的。


在CDPlayerConfig类中进行bean 实例化的时候,咱们还可以对它进行一些初始化的操作。

这些都是根据业务来看...我这里模拟的话,只是,传入两个参数。



六、借助Java Config实现注入

代码优先

package com.test.spring.server.test06;

/**
 * Created by CYX on 2018/4/17.
 */
public interface CompactDisc {

    void play();

}
package com.test.spring.server.test06;

/**
 * @author CYX
 * @create 2018-04-30-17:09
 */
public class SayHello {

    public SayHello() {
        System.out.println("SayHello 默认构造器");
    }

    public void sayHelloWithName() {
        System.out.println("SayHello :hello cyx");
    }

    public void sayHelloWithAddress() {
        System.out.println("SayHello :hello 南京");
    }

}
package com.test.spring.server.test06;


import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author CYX
 * @create 2018-04-17-20:47
 */
public class SgtPeppers implements CompactDisc {

    private String title = "Sgt. Pepper's Lonely Hearts Club Band";
    private String artist = "The Beatles";

    @Autowired
    private SayHello sayHello;


    public SgtPeppers() {
    }

    public SgtPeppers(SayHello sayHello) {
        this.sayHello = sayHello;
    }

    public SgtPeppers(String title, String artist) {
        this.title = title;
        this.artist = artist;
    }

    @Override
    public void play() {
        System.out.println("Playing " + title + " by " + artist);
        sayHello.sayHelloWithAddress();
        sayHello.sayHelloWithName();
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getArtist() {
        return artist;
    }

    public void setArtist(String artist) {
        this.artist = artist;
    }
}
package com.test.spring.server.test06;

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

/**
 * @author CYX
 * @create 2018-04-17-20:57
 */
@Configuration
@ComponentScan(basePackages = "com.test.spring.server.test06")
public class CDPlayerConfig {

    @Bean(name = "sssgtPeppers")
    public SgtPeppers initializationSgtPeppers() {
        return new SgtPeppers("888888", "99999");
    }

    @Bean
    public SgtPeppers initializationSgtPeppersWithHello() {
        return new SgtPeppers(initializationSayHello());
    }

    @Bean
    public SayHello initializationSayHello() {
        return new SayHello();
    }

}
package com.test.spring.server.test06;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * 测试组件扫描
 *
 * @author CYX
 * @create 2018-04-17-21:08
 */
public class Test6App {


    public static void main(String[] args) {

        ApplicationContext context = new AnnotationConfigApplicationContext(CDPlayerConfig.class);
        SgtPeppers sgtPeppers = context.getBean("sssgtPeppers", SgtPeppers.class);

        sgtPeppers.play();

    }

}
运行结果:

在CDPlayerConfig类中我们增加了两个bean,一个是用来注入的,另一个是被注入的。

在initializationSgtPeppersWithHello()方法中,并没有像之前那样子,传入参数,而是传入一个对象(方法)

看起来,SayHello对象,是通过调用initializationSayHello()方法得来的,但情况并非如此。

因为 initializationSayHello()方法上添加了@Bean注解,Spring将会拦截所有对它的调用,并确保直接返回该方法所创建的bean,而不是每次都对其进行实际的调用。


Java Config类换一种写法:

package com.test.spring.server.test06;

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

/**
 * @author CYX
 * @create 2018-04-17-20:57
 */
@Configuration
@ComponentScan(basePackages = "com.test.spring.server.test06")
public class CDPlayerConfig {

    @Bean(name = "sssgtPeppers")
    public SgtPeppers initializationSgtPeppers() {
        return new SgtPeppers("888888", "99999");
    }

    @Bean
    public SgtPeppers initializationSgtPeppersWithHello(SayHello sayHello) {
        return new SgtPeppers(sayHello);
    }

    @Bean
    public SayHello initializationSayHello() {
        return new SayHello();
    }

}

在这里,initializationSgtPeppersWithHello(SayHello sayHello) 方法直接将对象传入,这样子,好理解一点。


参考文章:

https://www.cnblogs.com/Ming8006/p/6323633.html

https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/stereotype/Component.html

《Spring 实战》

【液晶专题】从外挂到In-Cell/ On-Cell/ OGS再也不会傻傻分不清楚了 触控技术经历了从外挂式到内嵌式的革新,主要方案包括外挂式(Out-Cell)、OGS、On-Cell和In-Cell。 阅读详情

相关推荐

SVA学习笔记(1)——property基本语法及巧用define与property

巧用define与property,让代码更加灵活

craftsman2018ship的博客 1574

初学Spring之自动装配 Bean

如果显示定义了 Autowired 的 required 属性为 false,说明这个对象可以为 null,否则不允许为空。@Qualifier(value="xx") 可以指定唯一的 bean 对象注入(需和 @Autowired 搭配使用)会自动在容器上下文中查找和自己对象 set 方法后面的值对应的 bean id。Spring 会在上下文中自动寻找,并自动给 bean 装配属性。并且这个 bean 需要和自动注入的属性的。1.都是用来自动装配的,都可以放在属性字段上。需要保证所有 bean 的。

m0_58838332的博客 900

5分钟搞定CST周期性结构仿真:共面波导色散曲线快速生成技巧

本文提供了一套在CST Studio Suite中快速仿真周期性结构并生成色散曲线的标准化流程。针对共面波导设计,重点解析了周期性边界条件的核心设置、本征模求解器(JDM算法)的优化技巧,以及通过参数扫描与后处理模板实现自动化,旨在帮助射频与微波工程师在5分钟内高效完成关键性能验证。

blue的专栏 1248

Bean的自动装配

1.通过xml自动配置 2.通过注解自动配置

醉卧雕龙舫,的博客 3583

亲友圈棋牌的逆向工程_棋牌透视分析研究(一)

透视也能实现,有些台子就自带控制,这种东西权限只是存在于平台运营者手中,即便逆向破解也很难发现,但也有那么一些棋牌 比如斗牛或者跑得快,会先发给你的内存中存放当前的牌的信息,这样你就可以知道对面的牌是什么,但肯定也是加密了。明明是zip却无法解压,这很明显是属于被加密了, 那么我们就要开始找他的加密内容,一般加密的秘钥都会存储在so里面。写这一篇文章了,其实也想了很久,最终还是决定写了, 有些朋友找我做过菠菜的逆向,bjl,甚至是足球的!但很遗憾的告诉大家,这里是找不到的。

m0_63368535的博客 5811

UDPProbe UDP端口存活探测(发送特征包+响应分析)、UDP服务识别

qDebug() << "收到响应:" << ip.toString() << ":" << port << " 服务:" << serviceName << " 响应长度:" << response.size();qDebug() << "开始探测:" << targetIp << ":" << port << " 超时时间:" << timeout << "ms";qInfo() << "\n==================== UDP探测结果 ====================";

寒寒豆豆软件公司的博客 147

bean的自动装配

bean的自动装配、测试、ByName自动装配、ByType自动装配、使用注解实现自动装配(@Autowired、@Resource注解)

qq_46423017的博客 1447

华为面试手撕真题【最长不重复子串】

给定一个字符串s,请你找出其中不含有重复字符的最长子串的长度。

MISAYAONE的博客 1万+

spring学习系列(一):spring自动化装配bean

Spring容器负责创建应用程序中的bean并通过DI来协调这些对象之间的关 系。Spring具有非常大的灵活性,它提供了三种主要的装配机制: 在XML中进行显式配置。 在Java中进行显式配置。 隐式的bean发现机制和自动装配。 这里主要记录下自动化装配的方法。 1.Spring从两个角度来实现自动化装配: 组件扫描(component scanning):Spring会自动发现应用上下...

masterLSC的博客 530

VictoriaMetrics | 云原生监控新宠: 使用 vmagent 工具采集 VM 集群指标实战

摘要:本文介绍了vmagent工具的安装部署及配置实践,该工具是VictoriaMetrics官方提供的指标采集组件,支持兼容Prometheus指标采集与转发。文章详细讲解了从二进制方式部署vmagent的步骤,包括下载安装、创建运行环境、配置systemd服务等过程。通过8429端口可查看采集状态,并演示了将Prometheus配置迁移到vmagent的方法。vmagent可实现远程写入VictoriaMetrics集群,为云原生监控提供高效解决方案。

全栈工程师修炼指南-IT知识分享 446

《UDS协议从入门到精通》系列——图解0x2E:通过ID写数据

图解数据包格式+直观的通信示例,迅速认识0x2E服务!!

车载系统攻城狮 3602

a02-1 装配Bean-自动化装配(Spring实战)

创建对象之间的协作关系称为装配,这也就是依赖注入的本质 1、Spring在装配方式有三种: 1、在Xml中进行显示配置 2、在java中进行显示配置 3、隐式的bean发现机制和自动化装配 2、自动化装配bean 1、组件扫描:Spring会自动发现应用上下文所创建的bean 2、自动装配:Spring自动满足bean之间的依赖 一、自动化装配 1、定义接口...

brusion的博客 177

Backtrader Web:开源量化交易全栈平台

Backtrader Web是一个基于Backtrader框架的现代化量化交易全栈平台,将专业回测引擎与Web技术结合,提供从策略开发到实盘交易的完整解决方案。平台采用FastAPI+Vue3技术栈,具有118个内置策略模板、专业可视化分析工具和完整的策略生命周期管理功能。支持策略版本控制、参数优化、模拟/实盘交易及监控告警,通过RESTful API和WebSocket实现前后端交互。系统采用模块化设计,包含策略服务、回测服务等核心模块,支持SQLite/PostgreSQL等多种数据库。安装简单,5分钟

云金杞 643

Spring bean的装配方法

主要存在三种方法(1).在XML文件中进行显式配置(2).在java中进行显示配置(3).隐式的bean发现机制和自动装配。

weixin_42581560的博客 1779

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

Spring Bean之JavaConfig自动化装配bean

一、简介: Spring能够进行自动化的装配,它使用两种方式来进行自动化装配:   1、组件扫描:   Spring会自动发现应用上下文中所创建的bean   2、自动装配:   Spring会自动满足bean之间的依赖关系 比较有趣的是,Spring的自动化装配使用了零xml配置,而是使用全代码配置,其中代码的配置文件使用@Configuration注解进行标注。 二、

HappyHeng的博客 4925

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

【手撕 Spring】Spring 如何装配 Bean

在 Spring 中,对象无需自己查找或创建与其所关联的其他对象。相反,容器负责把需要相互协作的对象引用赋予各个对象。例如,一个订单管理组件需要信用卡认证组件,但它不需要自己创建信用卡认证组件。订单管理组件只需要表明自己两手空空,容器就会主动赋予它一个信用卡认证组件。

激流丶的博客 4557

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

Spring入门(二):自动化装配bean

关于CD和CD播放器关系的解释:如果你不将CD插入(注入)到CD播放器中,那么CD播放器其实是没有太大用处的。所以,可以这样说,CD播放器依赖于CD才能完成它的使命。

cxytony的博客 665

Spring中Bean的自动装配

Spring中Bean的自动装配 自动装配是使用spring满足bean依赖的一种方法 spring会在应用上下文中为某个bean寻找其依赖的bean。 Spring中bean有三种装配机制,分别是: 在xml中显式配置; 在java中显式配置; 隐式的bean发现机制和自动装配【重点】 本文主要介绍第三种,自动化的装配bean Spring的自动装配需要从两个角度来实现,或者说是两个操作: 4. 组件扫描(component scanning):spring会自动发现应用上下文中所创建的bean

qq_41359998的博客 2062

Spring-Bean自动装配

如果@Autowired自动装配的环境比较复杂(相同的类存在多个bean,不能准确注入我们需要的bean),自动装配无法通过一个注解@Autowired完成的时候,我们可以使用@Qualifier(value = “xxx”)通过beanId,配合@Autowired的使用,指定一个唯一的bean对象注入!ByName的时候,需要保证所有bean的id唯一,并且这个bean需要和自动注入的属性的set方法的值一致!byName:会自动在容器上下文中查找,和自己对象set方法后面的值对应的bean id!

weixin_53004298的博客 904

Spring中Bean的装配方式

Bean的装配就是Bean依赖注入。Spring容器提供了3中常用的Bean的装配方式,分别是 基于XML的装配 、基于注解的装配 和 自动装配 。

Nancy的博客 1565

实验1 Spring IoC中的Bean装配

实验1 Spring IoC中的Bean装配

派大星子的博客 2374

Spring中bean的三种装配机制之——自动装配

基本介绍  关于各种配置,从来没搞懂过。但是觉得框架发展这么快,而且结构这么宏大,其实现微观上来讲应该从来没有一种标准方式、更没有说是固定的几种。比如Spring专有的@Component、@Autowired注解,大多数情况下都可以被javax.inject包下的@Named和@Inject代替。本博文汇总《Spring 实战 第四版》2.1和2.2内容。   自动装配是使用spring满足be

beirdu的专栏 9271

【Spring Bean】装配和注入Bean

目录 基本模块 装配Bean 1、xml显式配置 2、java中显式配置 3、自动化装配Bean Bean注入方式 1、属性注入,也就是setter注入 2、构造器注入 3、工厂方法注入:静态工厂和实例工厂 企业日常开发中,几乎都是Spring系的框架,无论是SSM、还是现在大火的SpringBoot,使用最大的目的就是简化开发 基本模块 核心容器:Beans、Core...

凡是过往,皆为序章 907

Spring Bean的装配方式 第1关:基于 XML 的 Bean 装配

基于 XML 的 Bean 装配

于建章的博客 2279

Spring提供了三种装配bean的方式

转自:https://www.cnblogs.com/JimKing/p/8811119.html#s103 1.Spring提供了三种装配bean的方式: 2.自动装配bean: 3.通过Java代码装配bean 4.通过XML装配bean     前言:创建对象的协作关系称为装配,也就是DI(依赖注入)的本质。而在Spring容器中对bean的创建时就需要对它所依赖的对象进行注入...

armarm9的博客 2752

Spring——装配Bean

基本装配 在Spring容器内拼凑Bean叫做装配。装配Bean的时候,需要告诉哪些Bean以及容器如何使用依赖注入将它们配合在一起。 使用XML装配 XML是最常见的Spring应用系统配置源。 几种Spring容器都支持使用XML装配Bean,包括: XmlBeanFactory:调用ClassPathResource载入上下文定义文件(比如applicationContext...

Gavin 1542
上一篇: Spring - 01 - 通过XML装配Bean
下一篇: 异常 -- Constructor threw exception; nested exception is java.lang.IllegalArgumentException
Simba_cheng
博客等级 码龄11年 250粉丝 203原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值