spring @import和@importResource
Spring import
2023-09-11 14:21:40 时间
@ImportResource in spring imports application xml in configuration file which is using @Configuration. All the beans and other properties defined in application xml can be imported. @ImportResource helps when we are moving our application from old style of defining bean in xml to modern way of defining bean in java file with the help of @Configuration.
In the below example we have one bean defined in xml and one bean defined in java file. We will import the xml with help of @ImportResource and will fetch both beans.
app-conf.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"> <bean id="subscription" class="com.concretepage.Subscription"> <property name="name" value="Subscription"/> <property name="type" value="Weekly"/> </bean> </beans>
AppConfig.java
package com.concretepage; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.ImportResource; @Configuration @ImportResource("classpath:/app-conf.xml") public class AppConfig { @Bean(name="entitlement") public Entitlement entitlement(){ Entitlement ent= new Entitlement(); ent.setName("Entitlement"); ent.setTime(20); return ent; } }
Entitlement.java
package com.concretepage; public class Entitlement { private String name; private int time; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getTime() { return time; } public void setTime(int time) { this.time = time; } }
Subscription.java
package com.concretepage; public class Subscription { private String name; private String type; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getType() { return type; } public void setType(String type) { this.type = type; } }
AppTest.java
package com.concretepage; import java.sql.SQLException; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class AppTest { public static void main(String[] args) throws SQLException { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(AppConfig.class); ctx.refresh(); Entitlement ent= (Entitlement)ctx.getBean("entitlement"); System.out.println(ent.getName()); System.out.println(ent.getTime()); Subscription sub= (Subscription)ctx.getBean("subscription"); System.out.println(sub.getName()); System.out.println(sub.getType()); } }
Output
Entitlement
20
Subscription
Weekly
参考文献:
【1】http://www.concretepage.com/spring/example_importresource_spring
相关文章
- Spring基础-05
- 【Spring Boot】Spring Boot之两种引入spring boot maven依赖的方式
- spring boot下为配置属性值加密的正确姿势
- Spring Cloud 应用 Proxyless Mesh 模式探索与实践
- Java - Spring
- Spring容器初始话原理图
- 为什么Spring Boot推荐使用logback-spring.xml来替代logback.xml来配置logback日志的问题分析
- Spring多数据源分布式事务管理/springmvc+spring+atomikos[jta]+druid+mybatis
- spring-data-redis遇到twemproxy
- 【项目实战】Spring的@Import注解介绍
- 一文带你搞懂从动态代理实现到Spring AOP
- Java学习路线-56:Spring与Ioc
- spring与mybatis集成和事务控制
- 【Spring注解驱动开发】在@Import注解中使用ImportBeanDefinitionRegistrar向容器中注册bean
- 【Spring注解驱动开发】在@Import注解中使用ImportSelector接口导入bean
- 020-Spring Boot 监控和度量
- 009-Spring Boot 事件监听、监听器配置与方式、spring、Spring boot内置事件
- Spring Cloud Alibaba-Spring Cloud Gateway-API 网关-使用路由网关统一访问接口
- Spring MVC使用SessionLocaleResolver实现用户自定义切换语言实例