目录使用applicationContext.xml配置文件applicationContext 加载配置文件案例多文件的加载方法使用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"});以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
您可能感兴趣的文章:Spring使用AspectJ注解和XML配置实现AOP java配置context.xml文件的方法图解java由JABXContext注解读取xml配置文件方式
