这篇文章主要介绍了SpringBoot怎么使用applicationContext.xml配置文件的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇springBoot怎么使用applicationContext.
这篇文章主要介绍了SpringBoot怎么使用applicationContext.xml配置文件的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇springBoot怎么使用applicationContext.xml配置文件文章都会有所收获,下面我们一起来看看吧。
SpringBoot默认是通过Java代码进行依赖注入,但也为xml形式的依赖注入提供了入口,就是@ImportResource注解。
我们可以在SpringBoot的启动类上添加这个注解并在注解的locations属性中指定xml配置文件。(可以使用一个文件集合也可以只引入主配置文件然后在主配置文件中使用标签引入其他子配置文件,个人更喜欢第二中方式)。
这样容器在启动时配置在xml文件中的BeanDefination也可以被解析。
ApplicationContext 理解为spring容器的上下文,通过上下文操作容器中bean.
ClassPathXmlApplicationContext
:加载classpath下的配置文件创建一个容器实例
FileSystemXmlApplicationContext
: 加载文件系统中任意目录下的配置文件,创建一个容器实例
ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("spring.xml"); //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"});
关于“SpringBoot怎么使用applicationContext.xml配置文件”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“SpringBoot怎么使用applicationContext.xml配置文件”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注编程网精选频道。
--结束END--
本文标题: SpringBoot怎么使用applicationContext.xml配置文件
本文链接: https://www.lsjlt.com/news/342068.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-05-15
2024-05-15
2024-05-15
2024-05-15
2024-05-15
2024-05-15
2024-05-15
2024-05-15
2024-05-15
2024-05-15
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0