How to import Java-config class into XML-config so that both contexts have beans?

前端 未结 3 1758
青春惊慌失措
青春惊慌失措 2020-12-01 04:52

I have a project where I need to bootstrap @Configuration java-config classes into the XML configuration.

To do that, I\'m reading that I also need to include the fo

相关标签:
3条回答
  • 2020-12-01 05:41

    Alternatively to annotation-config you can use component-scan. Then you do not have to include the Configuration Bean in XML:

    <context:component-scan base-package="[fully qualified package path]" />
    

    See Difference between <context:annotation-config> vs <context:component-scan> for more details.

    0 讨论(0)
  • 2020-12-01 05:45

    Should be in:

    spring-javaconfig-<version>.jar
    
    0 讨论(0)
  • 2020-12-01 05:54

    This actually ended up being fairly simple. To get a Java-config bean definition into the xml-config, simply define the Java-config class as a bean within the XML-config. There are no extra jars necessary.

    @Configuration
    public class SomeJavaConfig {
    
        @bean
        ... [bean definition]
    }
    

    inside the XML-config, you define this class as a bean.

    <!-- needed to pick up the annotated java-config -->
    <context:annotation-config />
    
    <!-- Importing java-config class, which are annotated with @Configuration -->
    <bean name="SomeJavaConfig" class="[fully qualified path].SomeJavaConfig" />
    

    The XML-config, which may be part of a different context, now has all the bean definitions defined within the JavaConfig class.

    UPDATED - to included Alan Franzoni's comment below in the answer.

    0 讨论(0)
提交回复
热议问题