您好,欢迎访问一九零五行业门户网

SpringBoot怎么使用applicationContext.xml配置文件

使用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"});
以上就是springboot怎么使用applicationcontext.xml配置文件的详细内容。
其它类似信息

推荐信息