读取Spring配置文件的3种方式
2013-06-19 14:49
344 查看
在servlet中读取:
在Java类方法中读取:
public void init() throws ServletException { WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext()); sinaService = (SinaService) context.getBean("sinaService"); }
在Java类方法中读取:
public static void main(String[] args) { //说明:applicationContext.xml文件在src目录下 ApplicationContext context = new FileSystemXmlApplicationContext("src\\applicationContext.xml"); //下面这个也可以 // ApplicationContext context = new FileSystemXmlApplicationContext("src/applicationContext.xml"); SinaService sina = (SinaService)context.getBean("sinaService"); System.out.println(sina); }
public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); //在此种方式下"src/applicationContext.xml"或"src\\applicationContext.xml"都不行 SinaService sina = (SinaService)context.getBean("sinaService"); System.out.println(sina); }
相关文章推荐
- spring基于xml加载和读取properties配置文件的方式
- Spring中配置和读取多个Properties文件的方式方法
- Spring-boot中读取config配置文件的两种方式
- Spring 读取XML配置文件的两种方式
- springBoot使用Controller读取配置文件两种方式&读取自定义配置方法
- Spring读取配置文件,获取bean的几种方式
- Spring-boot中读取config配置文件的两种方式
- springboot读取自己定义的配置文件的方式以及使用joda_time来处理时间日期
- Spring读取配置文件,获取bean的几种方式
- Spring-boot中读取config配置文件的两种方式
- Spring读取配置文件的几种方式
- springboot读取配置文件的三种方式
- Spring读取配置文件的几种方式及依赖
- spring boot 在不同环境下读取不同配置文件的一种方式
- Spring-boot中读取config配置文件的两种方式
- springboot读取配置文件的三种方式
- spring中读取xml配置文件、获取bean的几种方式
- spring读取配置文件,获取bean的几种方式
- Spring读取配置文件的方式
- Spring中读取bean配置文件的几种方式