Spring拥有xml配置文件和JavaConfig并存的情况

1:启动Spring时,加载xml配置文件,但是要把JavaConfig引入到xml配置文件

<?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.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">

    <import resource="classpath:applicationContext1.xml" />//引入别的xml配置文件
   <bean class="JavaConfig"></bean>//在这里以bean的方式引入JavaConfig类即可

</beans> 

2:启动Spring时,加载JavaConfig类,但是要把xml配置文件引入到JavaCofig类中

@Configuration 
@ImportResource("classpath:spring2.xml")//在这里引入xml配置文件
public class FirstConfig {

    @Bean
    public FirstServiceImpl firstServiceImpl(SecondService secondService){
        FirstServiceImpl fImpl = new FirstServiceImpl();
         fImpl.setSecondService(secondService);
         return fImpl;
    }
}

原文地址:https://www.cnblogs.com/hzcya1995/p/13302458.html