`
kim_miao
  • 浏览: 188951 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

spring中加载Bean配置文件的常用方式

阅读更多
Spring中加载Bean配置文件的常用方式有两种,一种是通过实例化FileSystemXmlApplicationContext类的方式加载Bean,
另一种是通过实例化ClassPathXmlApplicationContext类的方式加载Bean.现举例如下,已做记录.

1.FileSystemXmlApplicationContext

(1)默认从项目工作路径开始查找,是相对路径
ApplicationContext applicationContext1 = new FileSystemXmlApplicationContext(
				"src/main/java/org/springframework/abc/demo/beans.xml");

(2)如果加上file前缀,则表示绝对路径
ApplicationContext  applicationContext2 = new FileSystemXmlApplicationContext(
				"file:E:/SpringSources/src/main/java/org/springframework/abc/demo/beans.xml");


2.ClassPathXmlApplicationContext

(1)没有前缀,默认为项目的classpath下相对路径
ApplicationContext applicationContext3 = new ClassPathXmlApplicationContext("beans.xml");

(2)加上classpath前缀,表示项目的classpath下相对路径
ApplicationContext applicationContext4 = new ClassPathXmlApplicationContext(
				"classpath:beans.xml");


3.DEMO代码如下

public class Test {
	
	public static void main(String[] args) {

		ApplicationContext applicationContext1 = new FileSystemXmlApplicationContext(
				"file:E:/SpringSources/src/main/java/org/springframework/abc/demo/beans.xml");
		System.out.println(applicationContext1.getBean("person"));

		ApplicationContext applicationContext2 = new FileSystemXmlApplicationContext(
				"src/main/java/org/springframework/abc/demo/beans.xml");
		System.out.println(applicationContext2.getBean("person"));

		ApplicationContext applicationContext3 = new FileSystemXmlApplicationContext(
				"classpath:beans.xml");
		System.out.println(applicationContext3.getBean("person"));

		ApplicationContext applicationContext4 = new ClassPathXmlApplicationContext(
				"beans.xml");
		System.out.println(applicationContext4.getBean("person"));

		ApplicationContext applicationContext5 = new ClassPathXmlApplicationContext(
				"classpath:beans.xml");
		System.out.println(applicationContext5.getBean("person"));

	}
}


0
0
分享到:
评论

相关推荐

    Spring bean 动态注册,jar包热替换

    Spring bean 一般通过配置文件和注解进行加载,如果要实现jar或class文件,动态实现spring bean 的动态加载,并通过UrlClassLoader完成jar和class文件的加载。可以实现jar的热替换。spring的bean动态加载则需要对...

    spring的bean加载顺序样例项目

    该项目展示spring配置文件的那些bean的加载顺序

    加载spring配置文件,提供getBean接口.

    加载spring配置文件,提供getBean接口.

    加载jar包中的spring配置文件

    尚硅谷]_佟刚_Spring IOC 容器中 Bean 的生命周期.pdf

    3、import用于导入其他配置文件的Bean定义,这是为了加载多个配置文件,当然也可以把这些配置文件构造为一个数组(new String[] {“config1.xml”, config2.xml})传给ApplicationContext实现进行加载多个配置文件,...

    spring配置文件详解--真的蛮详细

    spring配置文件是用于指导Spring工厂进行Bean生产、依赖关系注入(装配)及Bean实例分发的"图纸"。Java EE程序员必须学会并灵活应用这份"图纸"准确地表达自己的"生产意图"。Spring配置文件是一个或多个标准的XML文档...

    5.2、spring-refresh-obtainFreshBeanFactory-parseDefaultElement加载解析bean.vsdx

    本次为spring解析配置文件中spring.xml的过程,并且根据解析的spring.xml中的<bean>节点加载bean到map中。

    spring 配置文件简单说明

    1. 1)default-init-method ="方法名" 定义在此配置文件中的bean都会执行指定的init方法。 2)default-destroy-...3)default-lazy-init ="false|true" 定义在此配置文件中的bean都会延迟加载。 ....................

    spring-mvc代码示例+注释+文件解析(可直接运行)

    2、spring-mvc配置文件包括两部分,一个是spring传统配置文件,在代码中为“applicationContext.xml”,主要配置代码中各种bean以及依赖关系;另外一个是spring-mvc特有的配置文件,处理跳转的代理类得(相当于...

    spring jar 包详解

    (2) spring-beans.jar 这个jar文件是所有应用都要用到的,它包含访问配置文件、创建和管理bean以及进行Inversion of Control / Dependency Injection(IoC/DI)操作相关的所有类。如果应用只需基本的IoC/DI支持,...

    Spring 自定义注解注入properties文件的值jar包

    在xml配置文件中,这样加载properties文件 <bean id="propertyConfigurer" class="com.better517na.propertiesComponent.business.ExtendedPropertyPlaceholderConfigurer"> <value>classpath:...

    Spring中文帮助文档

    9.5.4. 为不同的bean配置不同的事务语义 9.5.5. <tx:advice/> 有关的设置 9.5.6. 使用 @Transactional 9.5.7. 事务传播 9.5.8. 通知事务操作 9.5.9. 结合AspectJ使用 @Transactional 9.6. 编程式事务管理 ...

    Spring框架生态流程框架图-执行运行路程图

    配置加载:Spring框架会读取并加载应用程序的配置文件,例如XML配置文件或基于注解的配置类。 依赖注入:Spring框架使用依赖注入(Dependency Injection)机制来管理组件之间的依赖关系。它会通过配置或注解获取...

    17-IoC配置-import导入配置文件

    Spring容器加载多个配置文件(了解) new classPathxmlApplicationcontext ( "config1.xml " , "config2.xml" ); Spring容器中的bean定义冲突问题 同id的bean,后定义的覆盖先定义的 导入配置文件可以理解为...

    高级开发spring面试题和答案.pdf

    SPI 机制(Java SPI 实际上是“基于接口的编程+策略模式+配置文件”组合实现的动态加载机制), 很多地方有用到: AOP Spring的AOP的底层实现原理; 为什么jdk动态代理是必须是接口 两种动态代理的区别 AOP实现方式:...

    Spring攻略(第二版 中文高清版).part1

    3.10 Spring中的AspectJ加载时织入aspect 140 3.10.1 问题 140 3.10.2 解决方案 141 3.10.3 工作原理 141 3.11 在Spring中配置AspectJ aspect 146 3.11.1 问题 146 3.11.2 解决方案 146 3.11.3 工作...

    struts1.2+spring2.0+hibernate3.1框架整合实例

    采用在web.xml中加载spring配置文件的方法降低struts和spring的耦合度,使用自定义的MyDelegatingRequestProcessor类来代替spring的DelegatingRequestProcessor类来解决spring中action的bean的重复配置问题。...

    groovy-loader:在文件目录中动态加载Groovy脚本

    使用spring配置文件来管理注册groovy bean:每一个spring配置文件作为一个ApplicationContext,管理一个namespace下的groovy bean spring配置文件使用标签lang:groovy,通过指定script-source来加载指定路径下的...

    Spring入门.docx

    所有在xml里配置的Bean标签最后都会转换为BeanDefinition对象存放在beanFactory里的beanDefinitionMap中。实例化完成的Bean对象存放在singletonObjects当中。 FileSystemXmlApplicationContext是加载磁盘里具体位置...

    从零开始学Spring Boot

    1.32 Spring boot 文件上传(多文件上传) 1.33 导入时如何定制spring-boot依赖项的版本 1.34 Spring Boot导入XML配置 1.35 Spring Boot使用@SpringBootApplication注解 1.36 Spring Boot 监控和管理生产环境 1.37 ...

Global site tag (gtag.js) - Google Analytics