SpringBoot怎么使用applicationContext.xml配置文件
作者:WarmHope
时间:2023-05-14
浏览:1
使用applicationContext.xml配置文件SpringBoot默认是通过Java代码进行依赖注入,但也为xml形式的依赖注入提供了入口,就是@ImportResource注解。我们可以在SpringBoot的启动类上添加这个注解并在注解的locations属性中指定xml配置文件。(可以使用一个文件集合也可以只引入主配置文件然后在主配置文件中使用标签引入其他子配置文件,个人更喜欢第二中方式)。这样容器在启动时配置在xml文件中的BeanDefination也可以被解析。application
使用applicationContext.xml配置文件
SpringBoot默认是通过Java代码进行依赖注入,但也为xml形式的依赖注入提供了入口,就是@ImportResource注解。
我们可以在SpringBoot的启动类上添加这个注解并在注解的locations属性中指定xml配置文件。(可以使用一个文件集合也可以只引入主配置文件然后在主配置文件中使用标签引入其他子配置文件,个人更喜欢第二中方式)。
这样容器在启动时配置在xml文件中的BeanDefination也可以被解析。
applicationContext 加载配置文件
ApplicationContext 理解为spring容器的上下文,通过上下文操作容器中bean.
ClassPathXmlApplicationContext:加载classpath下的配置文件创建一个容器实例FileSystemXmlApplicationContext: 加载文件系统中任意目录下的配置文件,创建一个容器实例
案例
/*方式一 :ClassPathXmlApplicationContext*/
ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("spring.xml");
/*方式二 FileSystemXmlApplicationContext */
//FileSystemXmlApplicationContext ioc= new FileSystemXmlApplicationContext("E://1804_2//20180827spring//config//spring.xml");
User u = (User) ioc.getBean("user1");
System.out.println(u);多文件的加载方法
/*方式一*/
//ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("spring.xml,spring-mvc.xml");
/*方式二*/
//ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext(new String[]{"spring.xml,spring-mvc.xml"});
/*方式三*/
//ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("spring-*.xml");
/*方式四*/
//ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext(new String []{"classpath:spring-*.xml","mybatis.xml"});
/*方式五*/
//ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("classpath:*.xml");
/*方式六*/
//ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("classpath*:*.xml");
/*方式七*/
//ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext(new String []{"classpath:*.xml","classpath:springmvc/beans.xml"});
作者最新文章
Photoshop自定义图层样式的保存、导入与管理技巧
2026-10-05 14:17
Photoshop蒙版在哪里调出来?蒙版按钮位置及使用教程
2026-09-29 09:53
Photoshop图层阵列怎么做?复制多个图层并整齐排列
2026-09-22 16:42
3dmax动画技巧总结:动画制作步骤与渲染视频教程
2026-09-22 14:47
在线PDF转TXT操作步骤与乱码排查指南
2026-09-04 13:02
热门文章
更多
精品专题
更多
Mac软件
更多
WINDOWS
更多










































