19

Spring Frameworkはアノテーションベースの構成をXMLベースの構成でオーバーライドできますか?アノテーションを介してすでに定義されているBeanの依存関係を変更する必要がありますが、私はBeanの作成者ではありません。

4

2 に答える 2

18

春が構成を混ぜることができることを私は知りませんでした。これが詳細で非常に便利な例です。

Bean1は、構成している実際のBeanです。

package spring;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

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

@Component
public class Bean1 {

    private String naber;

    @Autowired
    @Qualifier("FireImpl1")
    private Fire fire;

    @PostConstruct
    public void init() {
        System.out.println("init");
        getFire().fire();
    }

    @PreDestroy
    public void destroy() {
        System.out.println("destroy");
    }

    public void setNaber(String naber) {
        this.naber = naber;
    }

    public String getNaber() {
        return naber;
    }

    public void setFire(Fire fire) {
        this.fire = fire;
    }

    public Fire getFire() {
        return fire;
    }
}

火は依存関係のインターフェースです

package spring;

public interface Fire {

    public void fire();
}

およびダミーの実装1

package spring;

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

@Component
@Qualifier("FireImpl1")
public class FireImpl1 implements Fire {

    public void fire() {
        System.out.println(getClass());
    }
}

およびダミーの実装2

package spring;

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

@Component
@Qualifier("FireImpl2")
public class FireImpl2 implements Fire {

    public void fire() {
        System.out.println(getClass());
    }
}

config.xml

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
            http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
    <context:component-scan base-package="spring" />
    <bean id="bean1" class="spring.Bean1">
        <property name="naber" value="nice" />
        <property name="fire" ref="fireImpl2" />
    </bean>
</beans>

とメインクラス

package spring;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Spring {

    public static void main(String[] args) {

        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("/spring/config.xml");
        applicationContext.registerShutdownHook();
        Bean1 bean = (Bean1) applicationContext.getBean("bean1");
        System.out.println(bean.getNaber());
    }
}

これが出力です

init
class spring.FireImpl2
nice
destroy

アノテーションはFireImpl1への依存関係を解決しますが、xml構成はFireImpl2でオーバーライドされます。非常に素晴らしい。

于 2011-02-09T13:02:58.190 に答える
15

これは問題ないはずです。Spring Beanコンテキストを使用すると、「後の」定義が「前の」定義をオーバーライドして、Beanを再定義できます。これは、XMLで定義されたBeanと、アノテーションで定義されたBeanが混在している場合でも、それらに適用されます。

たとえば、

@Configuration
public class MyAnnotatedConfig {
   @Bean 
   public Object beanA() {
      ...
   }
}

<bean class="com.xyz.MyAnnotatedConfig"/>

<bean id="beanA" class="com.xyz.BeanA"/>

この場合、のXML定義がbeanA優先されます。

于 2011-02-09T10:35:56.570 に答える