第3章—高级装配—创建自定义的限定符

创建自定义的限定符

? 我们可以为bean设置自己的限定符,而不是依赖于将bean ID作为限定符.在这里所需要做的就是在bean声明上添加@Qualifier注解来进行更加明确的区分.例如:

Animal接口:

package com.home.quar;

public interface Animal {
    public void eat();

}

Cat:

package com.home.quar;

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

@Component
@Qualifier("cat")
public class Cat implements Animal {
    @Override
    public void eat() {
        System.out.println("猫吃鱼");
    }
}

Panda:

package com.home.quar;

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

@Component
@Qualifier("panda")
public class Panda implements  Animal {
    @Override
    public void eat() {
        System.out.println("熊猫吃竹子");
    }
}

TestDemo:

package com.home.quar;

import org.junit.Test;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TeatDemo {

    @Autowired
    @Qualifier(value = "cat")
    private Animal animal;
    public void setAnimal(Animal animal) {
        this.animal = animal;
    }

    public Animal getAnimal() {
        return animal;
    }

    @Test
    public void demo() {
        ApplicationContext app = new ClassPathXmlApplicationContext("classpath:application.xml");
        TeatDemo bean = (TeatDemo) app.getBean("teatDemo");
        System.out.println(bean.getAnimal());
        Animal al = bean.getAnimal();
        if (al instanceof Cat) {
            System.out.println("Cat");
        } else if (al instanceof Panda) {
            System.out.println("Panda");
        }
        al.eat();

        try {
            app.getBean("panda");
        } catch (Exception e) {
            if (e instanceof NoSuchBeanDefinitionException) {
                System.out.println("@Qualifier不能作为Bean的标识符");
            }
            e.printStackTrace();
        }
    }

}

最终结果是被@Qualifier注解的类被注入了进来,当然我们也可以用 XML配置的方法来限定@Qualifier的注入:

<?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-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd" >
      <context:annotation-config/>
      <bean id="teatDemo" class="com.home.quar.TeatDemo"/>
      <bean id="cat" class="com.home.quar.Cat">
            <qualifier type="org.springframework.beans.factory.annotation.Qualifier"   value="cat"/>
      </bean>
      <bean id="panda" class="com.home.quar.Panda">
            <qualifier type="org.springframework.beans.factory.annotation.Qualifier"  value="panda"/>
      </bean>
</beans>

原文地址:https://www.cnblogs.com/charlypage/p/9237939.html

时间: 2024-08-03 22:23:08

第3章—高级装配—创建自定义的限定符的相关文章

【翻译】Yii2 第2章 用Yii2创建自定义应用(第1节)

在这一章里,我们将看到Yii2怎样帮助我们创建web应用.示例虽然很简单,但整个过程都符合软件工程思想.我们将完成应用开发的每一个步骤,并且每一步都会根据权威书籍中的最佳实践来进行: 创建领域模型:这本书解释了领域驱动,Tackling Complexity in the Heart of Software, Eric Evans, Addison-Wesley Professional 设置测试装置:我们遵照验收测试驱动实践,Growing Object-oriented Software,

第3章 高级装配

3.1 环境与profile 在 3.1 版本中, Spring 引入了 bean profile 的功能.要使用 profile ,你首先要将所有不同的 bean 定义整理到一个或多个 profile 之中,在将应用部署到每个环境时,要确保对应的 profile 处于激活( active )的状态. 在 Java 配置中,可以使用 @Profile 注解指定某个 bean 属于哪一个 profile . @Service @Profile("prod") public class T

第三章 高级装配

Spring profile 条件化的bean声明 自动装配与歧义性 bean的作用域 Spring表达式语言 配置profile bean 在3.1版本中, Spring引入了bean profile的功能. 要使用profile, 你首先要将所有不同的bean定义整理到一个或多个profile之中, 在将应用部署 到每个环境时, 要确保对应的profile处于激活(active) 的状态. 在Java配置中, 可以使用@Profile注解指定某个bean属于哪一个profile. 例如, 在

spring(3)高级装配

[0]README 0)本文部分文字描述转自:"Spring In Action(中/英文版)",旨在review  spring(3)高级装配 的相关知识: [1]环境与profile(考虑数据库配置) 1)使用嵌入式数据库 @Bean(destroyMethod="shutdown") public DataSource dataSource() { return new EmbeddedDatabaseBuilder() .addScript("cl

spring—高级装配

1. 配置profile Bean 在3.1版本中,Spring引入了bean frofile的功能.要使用profile,你首先要将所有不同的bean定义整理到一个或多个profile之中,在将应用部署到每个环境时,要确保对应的profile处于激活(active)状态. @profile("dev") @profile注解应用在类级别上.他会告诉Spring这个配置类中的bean只有在dev profile激活时才会创建.如果dev profile没有激活的话,那么带有@Bean注

spring学习总结——高级装配学习一(处理自动装配的歧义性)

我们已经看到如何使用自动装配让Spring完全负责将bean引用注入到构造参数和属性中.自动装配能够提供很大的帮助.不过,spring容器中仅有一个bean匹配所需的结果时,自动装配才是有效的.如果不仅有一个bean能够匹配结果的话,Spring此时别无他法,只好宣告失败并抛出异常.更精确地讲,Spring会抛出NoUniqueBeanDefinitionException. 当确实发生歧义性时,Spring提供了多种可选方案来解决这样的问题.你可以将可选bean中的某一个设为首选(primar

Knockout应用开发指南 第五章:创建自定义绑定

创建自定义绑定 你可以创建自己的自定义绑定 – 没有必要非要使用内嵌的绑定(像click,value等).你可以你封装复杂的逻辑或行为,自定义很容易使用和重用的绑定.例如,你可以在form表单里自定义像grid,tabset等这样的绑定. 重要:以下文档只应用在Knockout 1.1.1和更高版本,Knockout 1.1.0和以前的版本在注册API上是不同的. 注册你的绑定 添加子属性到ko.bindingHandlers来注册你的绑定: ko.bindingHandlers.yourBin

带你走近AngularJS - 创建自定义指令

为什么使用AngularJS 指令? 使用过 AngularJS 的朋友应该最感兴趣的是它的指令.现今市场上的前端框架也只有AngularJS 拥有自定义指令的功能,并且AngularJS 是目前唯一提供Web应用可复用能力的框架. 目前有很多JavaScript 产品提供插件给Web开发人员.例如, Bootstrap 就是当前比较流行的提供样式和JavaScript插件的前端开发工具包.但是开发人员在使用Booostrap中的插件时, 必须切换到JavaScript 模式来写 jQuery

深入理解Magento - 第六章 - 高级Magento模型

我们讲过Magento有两种模型,简单模型和EAV(Entity Attribute Value)模型.上一章我们讲过所有的Magento模型都是继承自Mage_Core_Model_Abstract / Varien_Object.简单模型和EAV模型的区别在于资源模型(Model Resource).虽然所有的资源模型都最终继承“Mage_Core_Model_Resrouce_Abstract”,但是简单模型是直接继承“Mage_Core_Model_Mysql4_Abstract”,而E