likes
comments
collection
share

SpringBoot3使用xml配置注入

作者站长头像
站长
· 阅读数 12

SpringBoot3使用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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

spring6 sringboot3版本开头是这样的

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd">

http://www.springframework.org/schema/beans/spring-beans.xsdhttp换成了httpshttps://www.springframework.org/schema/beans/spring-beans.xsd

启用(起用)xml注入

@ImportResource注解启用xml注入

如在某个组件类上或主类上添加 @ImportResource("classpath:beans.xml")

application.properties 的 spring.config.import 不能用于引入xml

例子

例子240311

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd">
 
    <bean id="LifeCycleTest2403110139" class="z24dec.msbtLifeCycle.LifeCycleTest2403110139" 
    	init-method="initMethod" 
    	destroy-method="destroyMethod"
    	>
    </bean>
</beans>

@ImportResource

在Spring Boot中,@ImportResource是一个注解,它用于引入一个或多个Spring XML配置文件。这个注解可以用在@Configuration类上,以便将XML中定义的bean整合到Spring Boot应用程序的上下文中。

以下是如何使用@ImportResource注解的示例:

首先,你需要创建一个XML配置文件,例如legacy-config.xml,并将其放置在src/main/resources目录下:

<!-- src/main/resources/legacy-config.xml -->
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="legacyService" class="com.example.LegacyService">
        <!-- 配置属性和依赖 -->
    </bean>

    <!-- 其他bean定义 -->

</beans>

然后,在你的Spring Boot主类或其他@Configuration类上使用@ImportResource注解来导入XML配置文件:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;

@SpringBootApplication
@ImportResource("classpath:legacy-config.xml")
public class MySpringBootApplication {

    public static void main(String[] args) {
        SpringApplication.run(MySpringBootApplication.class, args);
    }

}

在上面的示例中,@ImportResource("classpath:legacy-config.xml")告诉Spring Boot在启动时要加载legacy-config.xml文件中定义的bean。这样,legacyService bean以及其他在XML文件中定义的bean将被注入到Spring应用程序上下文中,可以像使用其他Spring Bean一样使用它们。

虽然Spring Boot支持XML配置,但它鼓励开发者使用基于Java的配置和注解,因为这样可以更好地利用Spring Boot的自动配置特性,并提供更清晰、更类型安全的配置方式。不过,如果你正在迁移旧的Spring应用程序到Spring Boot,或者需要集成第三方库提供的XML配置,@ImportResource可以是一个有用的工具。

application.properties 的 spring.config.import 不能用于引入xml

在 Spring Boot 2.4 及更高版本中,spring.config.import 属性被引入,用于提供一种新的方式来导入配置数据,包括来自其他文件、目录或数据源的属性。然而,spring.config.import 主要是用于导入属性配置,而不是直接用于导入 Spring XML 配置。

尽管 spring.config.import 可以指向文件、目录或 URI,但它预期的是属性文件格式(如 .properties.yml/.yaml),而不是 Spring 的 XML 配置文件。因此,你不能直接使用 spring.config.import 来导入 XML 配置。

如果你需要导入 XML 配置,应该继续使用 @ImportResource 注解,这是专门为此目的而设计的。@ImportResource 允许你在 Spring Boot 的 Java 配置类中引入一个或多个 XML 配置文件。

例如:

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;

@Configuration
@ImportResource({"classpath:legacy-config1.xml", "classpath:legacy-config2.xml"})
public class XmlConfigImport {
    // 此类不需要额外的代码,它的存在只是为了导入 XML 配置
}

在上面的例子中,XmlConfigImport 类使用 @ImportResource 注解导入了两个 XML 配置文件。然后,你可以确保这个配置类被 Spring Boot 扫描到,或者通过其他方式将其包含在你的应用程序上下文中。

总之,spring.config.import 属性不适用于导入 XML 配置;相反,你应该使用 @ImportResource 注解来实现这一目的。

springboot有几种方法使用spring的xml注入bean

在Spring Boot中使用Spring的XML来注入bean主要有以下几种方法:

  1. 使用@ImportResource注解: 这是最常见的方法,可以在Spring Boot的@Configuration类上使用@ImportResource注解来指定XML配置文件的路径。Spring Boot在启动时将会加载这些XML文件,并将其中定义的bean注入到应用程序上下文中。

    @SpringBootApplication
    @ImportResource("classpath:beans.xml")
    public class MyApplication {
        public static void main(String[] args) {
            SpringApplication.run(MyApplication.class, args);
        }
    }
    
  2. 使用ApplicationContextInitializer: 可以创建一个实现ApplicationContextInitializer接口的类,并在该类中编程方式加载XML配置文件。然后,将这个初始化器注册到Spring Boot应用程序中。

    public class MyApplicationContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
        @Override
        public void initialize(ConfigurableApplicationContext applicationContext) {
            XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader((BeanDefinitionRegistry) applicationContext.getBeanFactory());
            reader.loadBeanDefinitions("classpath:beans.xml");
        }
    }
    

    注册ApplicationContextInitializer

    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(MyApplication.class);
        app.addInitializers(new MyApplicationContextInitializer());
        app.run(args);
    }
    

    或者在spring.factories文件中配置:

    org.springframework.context.ApplicationContextInitializer=com.example.MyApplicationContextInitializer
    
  3. 使用Spring的ClassPathXmlApplicationContext: 虽然这不是Spring Boot特有的方法,但你可以在任何需要的地方使用ClassPathXmlApplicationContext来加载XML配置文件。然而,这种方法通常不推荐在Spring Boot主应用程序中使用,因为它会绕过Spring Boot的自动配置和其他特性。

    ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
    MyBean myBean = context.getBean(MyBean.class);
    
  4. 通过Spring Boot的application.propertiesapplication.yml: 尽管spring.config.import主要用于导入属性配置,但理论上你可以通过一些间接的方式使用它来加载资源,比如通过自定义的PropertySourceLocatorEnvironmentPostProcessor。然而,这并不是spring.config.import的直接用途,且通常不推荐用于加载XML配置。

需要注意的是,尽管Spring Boot支持XML配置,但其设计初衷是为了简化Spring应用程序的初始化和开发过程,通过约定优于配置的原则来减少必要的配置。因此,在可能的情况下,最好使用基于Java的配置和注解来代替XML配置。

转载自:https://juejin.cn/post/7345350913656537114
评论
请登录