Spring提供了三种装配方式
  1.XML文件进行显式装配
  2.java中进行显示装配
  3.自动化装配
1.自动化装配的两种实现方式
  1.组件扫描:Spring会自动发现应用上下文中创建的bean
  2.自动装配:Spring自动满足bean之间的依赖

装配方式

 组件扫描

  创建可被发现的类

    类上@Component注解

    

package soundsystem;
import org.springframework.stereotype.Component;

/*Spring默认的bean id为类名首字母小写,在此处你可以自定义,也可以使用JDI提供的@Name注解为bean设置id*/
@Component("whateverYouWant")  
public class SgtPeppers implements CompactDisc { private String title = "Sgt. Pepper's Lonely Hearts Club Band";
private String artist = "The Beatles"; public void play() {
System.out.println("Playing " + title + " by " + artist);
} }

    @ComponentScan注解启用了组件扫描

    @Configuration注解表明这个类是一个配置类,该类应该包含在Spring应用上下文中如何创建bean的细节。

 

package soundsystem;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; @Configuration
@ComponentScan(bacePackages = "soundsystem")
public class CDPlayerConfig {
}

    @ComponentScan默认会扫描与配置类相同的包。因为CDPlayerConfig类位于soundsystem包中,因此Spring将会扫描这个包以及这个包下的所有子包,查找带有@Component注解的类。这样的话,就能发现CompactDisc,并且会在Spring中自动为其创建一个bean。

    如果你更倾向于使用XML来启用组件扫描的话,那么可以使用Spring context命名空间的<context:component-scan>元素。程序清单2.4展示了启用组件扫描的最简洁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:c="http://www.springframework.org/schema/c"
xmlns:p="http://www.springframework.org/schema/p"
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"> <context:component-scan base-package="soundsystem" /> </beans>

设置扫描基础包

  按照默认规则,@ComponentScan会以配置类所在的包作为基础包(basepackage)来扫描组件。如欲把配置类放在单独的包中,可在@ComponentScan后加value属性如上面代码所示。还可以通过bacePackages属性明确地表示你所设置的包是基础包。bacePackages可设置多个。

  

package soundsystem;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; @Configuration
@ComponentScan(bacePackages = {"soundsystem","audio"})
public class CDPlayerConfig {
}

Spring随笔-bean装配的更多相关文章

  1. Spring随笔-bean装配-自动装配

    Spring提供了三种装配方式 1.XML文件进行显式装配 2.java中进行显示装配 3.自动化装配 1.自动化装配的两种实现方式 1.组件扫描:Spring会自动发现应用上下文中创建的bean 2 ...

  2. Spring(004)-Bean装配

    一,问题,Bean找不到 代码 @Component public class DemoClass { public int doSth() { ; } } 测试代码 @RunWith(SpringJ ...

  3. 【Spring】Spring的bean装配

    前言 bean是Spring最基础最核心的部分,Spring简化代码主要是依赖于bean,下面学习Spring中如何装配bean. 装配bean Spring在装配bean时非常灵活,其提供了三种方式 ...

  4. Spring对Bean装配详解

    1.Spring提供了三种装配bean的方式: 2.自动装配bean: 3.通过Java代码装配bean 4.通过XML装配bean 前言:创建对象的协作关系称为装配,也就是DI(依赖注入)的本质.而 ...

  5. spring 使用@Bean装配Bean

    通过@Component装配Bean,但是@Component只能注解在类上,不能注解到方法上.对于Java而言,大部分的开发都需要引入第三方的包(jar文件),而且往往并没有这些包的源码,这时候将无 ...

  6. Spring boot Bean装配

    . Spring boot bean 默认创建的bean 为singleton模式 . @Component 注解 . @Value 为属性初始化 . @Value("${}") ...

  7. spring Bean装配的几种方式简单介绍

    Spring容器负责创建应用程序中的bean同时通过ID来协调这些对象之间的关系.作为开发人员,我们需要告诉Spring要创建哪些bean并且如何将其装配到一起. spring中bean装配有两种方式 ...

  8. Spring总结 1.装配bean

    本随笔内容要点如下: 依赖注入 Spring装配bean的方式 条件化装配 一.依赖注入 我理解的依赖注入是这样的:所谓的依赖,就是对象所依赖的其他对象.Spring提供了一个bean容器,它负责创建 ...

  9. Spring学习笔记—装配Bean

    在Spring中,对象无需自己负责查找或创建与其关联的其他对象.相反,容器负责把需要相互协作的对象引用赋予各个对象.创建应用对象之间协作关系的行为通常称为装配(wiring),这也是依赖注入的本质. ...

随机推荐

  1. 自定义solr域中的配置

    <!-- IKAnalyzer--> <fieldType name="text_ik" class="solr.TextField"> ...

  2. 天照(amaterasu)

    天照(amaterasu) 有些时候,出题人真的不想写背景. 总而言之,天照现在有一个长度为 $ N $ 序列,她有 $ M $ 次询问,对于第 $ i $ 次询问 $ l_i,r_i,x_i $ 你 ...

  3. Display与 Visibility的区别

    隐藏元素的方法有: display:none或visibility:hidden visibility:hidden可以隐藏某个元素,但隐藏的元素仍需占用与未隐藏之前一样的空间.也就是说,该元素虽然被 ...

  4. 19、Page Object 实例

    项目目录介绍: CalcuatorPage.java文件代码: package example; import io.appium.java_client.android.AndroidDriver; ...

  5. MySQL数据库(一)—— 数据库介绍、MySQL安装、基础SQL语句

    数据库介绍.MySQL安装.基础SQL语句 一.数据库介绍 1.什么是数据库 数据库即存储数据的仓库 2.为什么要用数据库 (1)用文件存储是和硬盘打交道,是IO操作,所以有效率问题 (2)管理不方便 ...

  6. SQlite 学习资料

      很有用的开源跨平台数据库,可以作为客户端的小型内存数据库使用,据说它有N多用户(Nokia's Symbian,Mozilla,Abobe,Google,阿里旺旺,飞信,Chrome,FireFo ...

  7. Haproxy负载均衡/动静分离(haproxy各选项详细解释)

    在前端领域做负载均衡,动静分离的程序有很多,比较常用的是nginx和Haproxy,今天就说一下 Haproxy在这两方面的表现,文章参考很多网文写成,再加上自己的实验成果,文中所有解释都经过实际环境 ...

  8. 天道神诀--IPSAN(iscsi配置)

    数据存储技术 DSA(Direct Attacted Storage 直接附加存储)本地硬盘 NAS(Network Attacted Storage 网络附加存储)网络服务共享:文件夹 SAN(St ...

  9. str2int HDU - 4436 后缀自动机求子串信息

    题意: 给出 n 个串,求出这 n 个串所有子串代表的数字的和. 题解; 首先可以把这些串构建后缀自动机(sam.last=1就好了), 因为后缀自动机上从 root走到的任意节点都是一个子串,所有可 ...

  10. mysql的下载

    怎样从Mysql官网下载mysql.tar.gz版本的安装包 原创 2016年10月20日 21:06:41 10854       今天学习在Linux上部署项目,用到了Mysql,因此想要下载适用 ...