spring-装配bean

摘要:
还需要显示配置一下spring,从而命令它寻找带有@Component注解的类,并为之创建bean。SgtPeppers-˃sgtPeppers手动设置id:@ComponentpublicclassSgtPeppersimplementsCompactDisc{@Overridepublicvoidplay(){System.out.println;}}2.3为装配的bean添加依赖注入使用@Autowired注解。

1.spring配置的可选方案

spring提供了三种装配bean的机制:

  • 在xml中显式配置
  • 在java中进行显式配置
  • 隐式的bean发现机制和自动转配

三种方式在适当的场合使用,当然你可以选择自己喜欢的方式转配bean。

2.自动化装配bean

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

  • 组件扫描(component scanning)spring会自动发现上下文中所创建的bean
  • 自动装配(autowiring):依赖注入DI

2.1 创建可被发现的bean

定义接口CompactDisc

public interfaceCompactDisc {
    voidplay();
}

带有@Component注解实现类:

@Component
public class SgtPeppers implementsCompactDisc {
    @Override
    public voidplay() {
        System.out.println("Playing Sgt....");
    }
}

使用@Component注解来告诉spring将该类当做组件类,并告诉spring要为这个类创建bean。这里就不要显示配置SgtPeppers的bean了。

不过组件扫描默认是不启动的。还需要显示配置一下spring,从而命令它寻找带有@Component注解的类,并为之创建bean。

接下来就要创建扫描组件的配置文件了,有两种方式:

  • 通过加载java配置文件,装配bean
importorg.springframework.context.annotation.Bean;
importorg.springframework.context.annotation.ComponentScan;
importorg.springframework.context.annotation.Configuration;
@Configuration
//扫描注解包,装配bean
@ComponentScan(basePackages = {"cn.getword.b_beans"})
//不扫描包,也可以通过@Bean装配bean
public classCDPlayerConfig {
    @Bean
    publicCompactDisc sgtPeppers(){
        return newSgtPeppers();
    }
}

注意:如果仅仅使用@ComponentScan的话,默认会扫描与配置类相同的包。

  • 通过加载xml配置文件,装配bean
    <?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"
           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"
    > <!--通过扫描基础包,自动装配bean--> <context:component-scan base-package="cn.getword.b_beans" /> </beans>

测试

@RunWith(SpringJUnit4ClassRunner.class)
//通过加载xml配置文件,装配bean
@ContextConfiguration(locations = "classpath:cn/getword/b_beans/CDPlayer.xml")
//通过加载java配置文件,装配bean
//@ContextConfiguration(classes = CDPlayerConfig.class)
public classBeansTest {
    //CompactDisc接口有多个实现类,必须指明id;如果变量名和id一致,也可以匹配
//@Qualifier("sgtPeppers")
@Autowired
    privateCompactDisc sgtPeppers;
    @Autowired
    privateCompactDisc blankDisc;
    @Test
    public voidplay(){
        sgtPeppers.play();
        blankDisc.play();
    }
}

2.2 为组件扫描的bean命名【id】

在前面的例子中,尽管没有明确的设置id,但是spring会根据类名为其指定一个id。SgtPeppers ->sgtPeppers

手动设置id:

@Component("sgtPeppers")
public class SgtPeppers implementsCompactDisc {
    @Override
    public voidplay() {
        System.out.println("Playing Sgt....");
    }
}

2.3 为装配的bean添加依赖注入

使用@Autowired注解。可以放在属性前或setter方法前。

3.通过xml装配bean

3.1 声明一个简单的bean:

免责声明:文章转载自《spring-装配bean》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇集成算法 ensemble method番茄花园Ghost Win10系统X64位10041装机版2015年4月下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

魅族推送 简介 集成 MD

Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina.com 目录 目录魅族推送简介FAQ简洁版自定义消息推送DemoReceiver配置文件AndroidManifest.xml 魅族推...

FlinkSQL源码阅读-schema管理

在Flink SQL中, 元数据的管理分为三层: catalog-> database-> table, 我们知道Flink SQL是依托calcite框架来进行SQL执行树生产,校验,优化等等, 所以本文讲介绍FlinkSQL是如何来结合Calcite来进行元数据管理的. calcite开放的接口 public interface Schem...

C# 配合 Easyui DataGrid 实现增删改查 通用模板

前端代码: <table id="DataGridEmployee"data-options="region:'center',title:'员工列表'"></table>$('#DataGridEmployee').datagrid({ title: '员工列表',...

spring自动注入的三种方式

所谓spring自动注入,是指容器中的一个组件中需要用到另一个组件(例如聚合关系)时,依靠spring容器创建对象,而不是手动创建,主要有三种方式: 1. @Autowired注解——由spring提供 2. @Resource注解——由JSR-250提供 3. @Inject注解——由JSR-330提供   @Autowired注解的使用方法 @Tar...

CAS 单点登陆

一、Tomcat配置SSL 1. 生成 server key 以命令方式换到目录%TOMCAT_HOME%,在command命令行输入如下命令:keytool -genkey -alias tomcat_key -keyalg RSA -storepass changeit -keystore server.keystore -validity 3600...

谷歌浏览器 html5的声音和视频不能自动播放处理

AI模型开发就选MindSpore!新特性、新工具上线!>>> 声音无法自动播放这个在IOS/Android上面一直是个惯例,桌面版的Safari在2017年的11版本也宣布禁掉带有声音的多媒体自动播放功能,紧接着在2018年4月份发布的Chrome 66也正式关掉了声音自动播放,也就是说<audio autopaly><...