1、spring中访问资源
spring通过Resource接口实现资源的访问,针对不同的资源,涉及了不同的实现。
接口的主要方法:
exists(): 判断资源是否存在 isOpen(): 判断资源是否已经打开 getURL(): getFile(): getInputStream():
接口的主要实现:
ByteArrayResource: 二进制数组表示的资源 ClassPathResource: 类路径下的资源 FileSystemResource: 文件系统资源,通过文件路径读取 InputStreamResource: ServletContextResource: 读取相对于web应用根目录的路径的资源 UrlResource: 读取web服务器或者ftp服务器上的资源读取资源时,默认采用系统编码读取资源文件,可以通过EncodedResource对资源进行编码:
Resource resource=new ClassPathResource(""); EncodedResource encodedRes=new EncodedResource(resource,"UTF-8"); String content=FileCopyUtils.copyToString(encodedRes.getReader())
2、spring加载资源
配置前缀:
classpath: 从类路径中加载资源, classpath*: 所有包含指定包名的路径 file: 使用URLResource从文件系统目录中装载资源,相对或绝对路径 file:/conf/beans.xml http:// 使用URLResource从web服务器中装载资源 http://www.xxx.com/resources/beans.xml ftp:// 使用URLResource从ftp服务器装载资源 无前缀: 根据ApplicationContext的具体实现类采用对应类型的resource
匹配符:
?: 匹配文件名中的一个字符 *: 匹配文件名中的任意个字符 **:匹配多层路径
3、BeanFactory和ApplicationContext
BeanFactory是spring框架的基础设施,面向spring本身;
BeanFactory是spring框架的基础设施,面向spring本身;
ApplicationContext面向使用spring框架的开发者,几乎所有的应用场合我们都直接使用ApplicationContext而非底层的BeanFactory!
通过BeanFactory启动ioc容器时,并不会初始化配置文件中定义的bean,初始化动作发生在第一次调用时。对于单实例bean来说,BeanFactory会缓存bean实例,再次调用时直接从ioc容器的缓存中获取bean实例。
通过BeanFactory启动ioc容器时,并不会初始化配置文件中定义的bean,初始化动作发生在第一次调用时。对于单实例bean来说,BeanFactory会缓存bean实例,再次调用时直接从ioc容器的缓存中获取bean实例。
WebApplicationContext是为web应用准备的,扩展了ApplicationContext。通过它可以实现web应用上下文和web容器的上下文互访。
BeanFactory
ResourcePatternResolver resolver=new PathMatchingResourcePatternResolver(); Resource res=resolver.getResource("classpath:com/beans.xml"); BeanFactory bf=new XmlBeanFactory(res); ...bf.getBean("xxx");ApplicationContext
//从类路径加载配置 ApplicationContext context=new ClassPathXmlApplicationContext("com/beans.xml"); //从文件系统路径加载配置 相对或绝对 ApplicationContext context=new FileSystemXmlApplicationContext(com/beans.xml"); ApplicationContext context=new FileSystemXmlApplicationContext(new String[]{"com/beans.xml","com/beans2.xml"); //注解方式 @Configuration表示是一个配置信息提供类 @Bean定义一个bean ApplicationContext context=new AnnotationConfigApplicationContext(Beans.class);
WebApplicationContext
ServletContext servletContext=ServletActionContext.getRequest().getSession().getServletContext(); //this.getServletContext(); ApplicationContext ctx = WebApplicationContextUtils. getWebApplicationContext( servletContext); spring中获取ServletContext WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext(); ServletContext servletContext = webApplicationContext.getServletContext();
WebApplicationContext初始化需要ServletContext实例,就是说必须要有web容器。方式是通过在web.xml中配置自启动的servlet或者监听器。
<!-- 指定spring配置文件位置--> <context-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/a.xml,/WEB-INF/b.xml </param-value> </context-param> <!-- 指定log4j配置文件位置,WebApplicationContext需要使用日志功能,必须在装载spring之前装载log4j--> <context-param> <param-name>log4jConfigLocation</param-name> <param-value> /WEB-INF/log4j.properties </param-value> </context-param> <!--方式一,自启动servlet 低版本的web容器不支持listener启动,应选择该方式--> <servlet> <servlet-name>log4jConfigServlet</servlet-name> <servlet-class>org.springframework.web.util.Log4jConfigServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet> <servlet-name>springContextLoaderServlet</servlet-name> <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class> <load-on-startup>2</load-on-startup> </servlet> <!--方式二,web容器监听启动--> <servlet> <servlet-name>log4jConfigServlet</servlet-name> <servlet-class>org.springframework.web.util.Log4jConfigServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
bean的作用域
singleton作用域:
<bean id="" class="" singleton="true"/>
spring ioc容器中只存在一个bean的实例。
prototype作用域:
spring ioc容器中只存在一个bean的实例。
prototype作用域:
<bean id="" class="" scope="prototype"/>
每次对该bean进行请求时,都会创建一个新的bean实例。
prototype作用域的任何bean,spring容器负责进行初始化、配置、装饰或者装配完一个实例后,就不闻不问了。
资源释放由客户端代码负责,spring容器不会调用相应处理来释放资源。
request作用域:
每次对该bean进行请求时,都会创建一个新的bean实例。
prototype作用域的任何bean,spring容器负责进行初始化、配置、装饰或者装配完一个实例后,就不闻不问了。
资源释放由客户端代码负责,spring容器不会调用相应处理来释放资源。
request作用域:
<bean id="" class="" scope="request"/>
每次请求创建一个实例,该实例仅在当前HTTP request内有效。
session作用域:
每次请求创建一个实例,该实例仅在当前HTTP request内有效。
session作用域:
<bean id="" class="" scope=""/>
实例仅在当前HTTP session内有效。
实例仅在当前HTTP session内有效。