読者です 読者をやめる 読者になる 読者になる

liguofeng29’s blog

個人勉強用ブログだっす。

spring4 - @Qualifier, alias

@Qualifierは、bean名を指定してDIを行う

package spring4.second;
public interface SomeService {
    public String getMessage();
}

package spring4.second;
import org.springframework.stereotype.Component;
@Component("SomeServiceAImpl")
public class SomeServiceAImpl implements SomeService {

    @Override
    public String getMessage() {
        return "AAA";
    }
}

package spring4.second;
import org.springframework.stereotype.Component;
@Component("SomeServiceBImpl")
public class SomeServiceBImpl implements SomeService {

    @Override
    public String getMessage() {
        return "BBB";
    }
}

package spring4.second;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
@Component
public class SomeObject {

    @Autowired
    @Qualifier("SomeServiceAImpl")
    private SomeService serviceA;

    @Autowired
    @Qualifier("SomeServiceBImpl")
    private SomeService serviceB;

    public void run() {
        System.out.println(serviceA.getMessage());
        System.out.println(serviceB.getMessage());
    }
}
package spring4.second;

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

@Configuration
@ComponentScan
public class App {
    public static void main(String[] args) {
        ApplicationContext context1 = new AnnotationConfigApplicationContext(App.class);
        context1.getBean(SomeObject.class).run();

    }
}

出力:
AAA
BBB

aliasを使い、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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:component-scan base-package="spring4.second" />

    <alias name="SomeServiceAImpl" alias="SomeServiceBImpl" />
</beans>
package spring4.second;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.FileSystemXmlApplicationContext;

@Configuration
@ComponentScan
public class App {
    public static void main(String[] args) {
        ApplicationContext context2 = new FileSystemXmlApplicationContext(
                "classpath:spring4/second/applicationContext.xml");
        context2.getBean(SomeObject.class).run();
    }
}

出力:
AAA
AAA
<alias name="SomeServiceAImpl" alias="SomeServiceBImpl" />
をすることで、
@Qualifier("SomeServiceBImpl") -> @Qualifier("SomeServiceAImpl")になる

git:
https://github.com/liguofeng29/java-project/tree/master/src/main/java/spring4/second