×

读取WEB-INF 下applicationContext.xml配置文件

前端技术网 前端技术网 发表于2024-01-27 22:43:36 浏览987 评论0

抢沙发发表评论

一、applicationcontext提供了以下哪些扩展功能

如果说BeanFactory是spring的心脏,那么Application就是完整的身躯。ApplicationContext就是由BeanFactory派生出来的。

1、ApplicationContext

读取WEB-INF 下applicationContext.xml配置文件

ApplicationContext的主要实现类是ClassPathXmlApplicationContext和FileSystemXmlApplicationContext,前者默认从类路径加载配置文件,后者默认从文件系统加载文件。

如果配置文件放在类路径下,直接使用ClassPathXmlApplicationContext实现类:

ApplicationContext ctx=new ClassPathXmlApplicationContext("com/techman/context/beans.xml");

这里的参数等同于:"classpath:com/techman/context/beans.xml"

如果配置文件在文件系统的路径下,则可以优先考虑使用FileSystemXmlApplicationContext实现类:

ApplicationContext ctx=new FileSystemXmlApplicationContext("com/techman/context/beans.xml");

读取WEB-INF 下applicationContext.xml配置文件

这里的参数等同于:"file:com/techman/context/beans.xml".

还可以指定一组配置文件,Spring自动将多个配置文件在内存中整合成一个配置文件:

ApplicationContext ctx=new ClassPathXmlApplicationContext(new String[]{"conf/bean1.xml","conf/bean2.xml"});

2、AnnotationConfigApplicationContext

直接实例:

[java] view plaincopyprint?

package com.techman.context;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import com.techman.reflect.Car;

@Configuration//表示是一个配置信息提供类,这里是通过类注解的配置方式

public class Beans

{

@Bean(name="car")

public Car buildCar()

{

Car car=new Car();

car.setBrand("红旗CA72");

car.setMaxSpeed(340);

return car;

}

}

**代码

[java] view plaincopyprint?

package com.techman.context;

import org.springframework.context.ApplicationContext;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import com.techman.reflect.Car;

//这里需要spring-context.jar和spring-expression.jar的支持

public class AnnotationApplicationContext

{

public static void main(String []args)

{

//通过一个带@Configuration的POJO装载Bean配置

ApplicationContext ac=new AnnotationConfigApplicationContext(Beans.class);

Car car=ac.getBean("car",Car.class);

car.introduce();

}

}

**代码

AnnotationConfigApplicationContext将加载Beans.class中的Bean定义并调用Beans.class中实现的方法实例化Bean,启动容器并装配Bean.

3、WebApplicationContext

WebApplicationContext是专门为Web应用准备的,它允许从相对于web根目录的路径中加载配置文件完成初始化工作。

WebApplicationContext扩展了ApplicationContext,WebApplicationContext定义了一个常量ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,在上下文启动时,我们可以直接通过下面的语句从web容器中获取WebApplicationContext:

WebApplicationContext wac=(WebApplicationContext)servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);

4、ConfigurableWebApplicationContext

ConfigurableWebApplicationContext扩展了WebApplicationContext,它允许通过配置的方式实例化WebApplicationContext,它定义了两个重要的方法:

setServletContext(ServletContext servletContext):为Spring设置Web应用上下文,以便两者整合。

setConfigLocation(String[] configLocations)设置Spring配置文件地址,一般情况下,配置文件地址是相对于Web根目录的地址,如/WEB-INF/techman-dao.xml等。也可以使用classpath:com/techman/context/techman-dao.xml等格式。

5、Spring为使用WebApplicationContext的Servlet和Web容器监听器:

org.springframework.web.context.ContextLoaderServlet;

org.springframework.web.context.ContextLoaderListener;

这里是web.xml启动WebApplicationContext的配置:

[html] view plaincopyprint?

<!--从类路径下加载Spring配置文件,classpath关键字特指类路径下加载,如果多个文件则使用逗号或空格隔开-->

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath:applicationContext.xml,/WEB-INF/techman-dao.xml</param-value>

</context-param>

<!--负责启动Spring容器的监听器,它将引用上面的上下文参数获得Spring配置文件地址-->

<listener>

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

**代码

如果在不支持容器监听器的低版本Web容器中,我们可采用ContextLoaderServlet完成相同的工作:

[html] view plaincopyprint?

<!--从类路径下加载Spring配置文件,classpath关键字特指类路径下加载,如果多个文件则使用逗号或空格隔开-->

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath:applicationContext.xml,/WEB-INF/techman-dao.xml</param-value>

</context-param>

**代码

[html] view plaincopyprint?

<servlet>

<servlet-name>springContextLoaderServlet</servlet-name>

<servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>

<load-on-startup>1</load-on-startup>

</servlet>

**代码

6、Log4j的配置

由于WebApplicationContext需要使用日志功能,用户可以将log4j.properties放置在类路径下,这样就会自动启动。如果放在其他地方,必须在web.xml中指定Log4j配置文件的位置。Spring为启动Log4j引擎提供了两个类og4jConfigServlet和Log4jConfigListener,不管哪种方式都必须保证能够在装载Spring配置文件前先装载Log4J配置信息。

[html] view plaincopyprint?

<context-param>

<param-name>log4jConfigLocation</param-name>

<param-value>/WEB-INF/log4j.properties</param-value>

</context-param>

<servlet>

<servlet-name>log4jConfigServlet</servlet-name>

<servlet-class>org.springframework.web.util.Log4jConfigServlet</servlet-class>

<load-on-startup>1</load-on-startup>

</servlet>

**代码

7、使用标注@Configuration的Java类提供配置信息

方式如下:

[html] view plaincopyprint?

<!--通过指定context参数,让Spring使用AnnotationConfigWebApplicationContext而非XmlWebApplicationContext启动容器-->

<context-param>

<param-name>contextClass</param-name>

<param-value>

org.springframework.web.context.support.AnnotationConfigWebApplicationContext

</param-value>

</context-param>

<!--指定标注了@Configuration的配置类,多个可以使用逗号或空格分隔-->

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>com.**art.Beans,com.**art.AppConfig2</param-value>

</context-param>

<!-- ContextLoaderListener监听器将根据上面的配置使用AnnotationConfigWebApplicationContext根据contextConfigLocation指定的配置类启动Spring容器-->

<listener>

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

二、Spring MVC 配置文件讲解

使用@Controller定义一个控制器

使用@RequestMapping映射请求

使用@RequestParam绑定请求参数到方法参数

使用@ModelAttribute提供一个从模型到数据的链接

使用@SessionAttributes指定存储在会话中的属性

<context:annotation-config/>

他的作用是隐式地向 Spring容器注册

AutowiredAnnotationBeanPostProcessor、

CommonAnnotationBeanPostProcessor、

PersistenceAnnotationBeanPostProcessor、

RequiredAnnotationBeanPostProcessor

这 4个BeanPostProcessor。

例如:

如果想使用@ Resource、@ PostConstruct、@ PreDestroy等注解就必须声明CommonAnnotationBeanPostProcessor。

如果想使用@PersistenceContext注解,就必须声明PersistenceAnnotationBeanPostProcessor的Bean。

如果你想使用@Autowired注解,那么就必须事先在 Spring容器中声明 AutowiredAnnotationBeanPostProcessor Bean。传统声明方式如下:

<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>

如果想使用@Required的注解,就必须声明RequiredAnnotationBeanPostProcessor的Bean。同样,传统的声明方式如下:

<bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/>

记得,使用注解一般都会配置扫描包路径选项

<context:component-scan base-package=”XX.XX”/>

<servlet>

<servlet-name>dispatcherServlet</servlet-name>

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<init-param>

<param-name>contextConfigLocation</param-name>

<param-value>/WEB-INF/dispatcherServlet-servlet.xml</param-value>

</init-param>

<load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>dispatcherServlet</servlet-name>

<url-pattern>*.do</url-pattern>

</servlet-mapping>

这个配置常常见于web.xml文件中

<load-on-startup>1</load-on-startup>是启动顺序,让这个Servlet随Servletp容器一起启动。

<url-pattern>*.do</url-pattern>会拦截*.do结尾的请求。

<servlet-name>dispatcherServlet</servlet-name>这个Servlet的名字是dispatcherServlet,可以有多个DispatcherServlet,是通过名字来区分的。每一个DispatcherServlet有自己的WebApplicationContext上下文对象。同时保存的ServletContext中和Request对象中,关于key,以后说明。

在DispatcherServlet的初始化过程中,框架会在web应用的 WEB-INF文件夹下寻找名为[dispatcherServlet]-servlet.xml的配置文件,生成文件中定义的bean。

<init-param>

<param-name>contextConfigLocation</param-name>

<param-value>/WEB-INF/dispatcherServlet-servlet.xml</param-value>

</init-param>

指明了配置文件的文件名,不使用默认配置文件名,而使用springMVC.xml配置文件。

其中<param-value>**.xml</param-value>这里可以使用多种写法

1、不写,使用默认值:/WEB-INF/<servlet-name>-servlet.xml

2、<param-value>/WEB-INF/classes/springMVC.xml</param-value>

3、<param-value>classpath*:springMVC-mvc.xml</param-value>

4、多个值用逗号分隔

springMVC-mvc.xml配置文件片段讲解

<context:annotation-config/>

<!--自动扫描的包名-->

<context:component-scan base-package="com.iflysse"/>

<!--默认的注解映射的支持-->

<mvc:annotation-driven/>

<!--视图解释类-->

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">

<property name="prefix" value="/WEB-INF/jsp/"/>

<property name="suffix" value=".jsp"/><!--可为空,方便实现自已的依据扩展名来选择视图解释类的逻辑-->

<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>

</bean>

<mvc:annotation-driven/>是一种简写形式,完全可以手动配置替代这种简写形式,简写形式可以让初学都快速应用默认配置方案。<mvc:annotation-driven/>会自动注册DefaultAnnotationHandlerMapping与AnnotationMethodHandlerAdapter两个bean,是spring MVC为@Controllers分发请求所必须的。

并提供了:数据绑定支持,@NumberFormatannotation支持,@DateTimeFormat支持,@Valid支持,读写XML的支持(JAXB),读写JSON的支持(Jackson)。

后面,我们处理响应ajax请求时,就使用到了对json的支持。

后面,对action写JUnit单元测试时,要从spring IOC容器中取DefaultAnnotationHandlerMapping与AnnotationMethodHandlerAdapter两个bean,来完成测试,取的时候要知道是<mvc:annotation-driven/>这一句注册的这两个bean。

<!-- json支持-->

<bean id="mappingJacksonHttpMessageConverter"

class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">

<property name="objectMapper" ref="commonObjectMapper"/>

<property name="supportedMediaTypes">

<list>

<value>text/html;charset=UTF-8</value>

</list>

</property>

</bean>

<!-- ObjectMapper json转换-->

<bean id="commonObjectMapper" class="cn.com.starit.util.CommonObjectMapper"/>

三、spring test 怎么读取web-inf下的applicationcontext

假设Spring配置文件为applicationContext.xml

一、Spring配置文件在类路径下面

在Spring的java应用程序中,一般我们的Spring的配置文件都是放在放在类路径下面(也即编译后会进入到classes目录下)。

以下的项目,因为是用maven管理的,所以配置文件都放在“src/main/resources”目录下

这时候,在代码中可以通过

Java代码

ApplicationContextapplicationContext=newClassPathXmlApplicationContext("applicationContext.xml");

然后获取相应的bean。

如果代码想用Junit测试框架来测试,则Spring提供了对Junit支持,还可以使用注解的方式:

Java代码

@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration(locations={"classpath:applicationContext.xml"})

只需要在相应的Test类前面加上此两个注解(第二个注解用来指明Spring的配置文件位置),就可以在Junit Test类使用中Spring提供的依赖注入功能。

二、Spring配置文件在WEB-INF下面

当然在做J2EE开发时,有些人习惯把Spring文件放在WEB-INF目录(虽然更多人习惯放在类路径下面)下面;或者有些Spring配置文件是放在类路径下面,而有些又放在

WEB-INF目录下面,如下图。

这时候,在代码中就不可以使用ClassPathXmlApplicationContext来加载配置文件了,而应使用FileSystemXmlApplicationContext。

Java代码

ApplicationContextapplicationContext=newFileSystemXmlApplicationContext("src/main/webapp/WEB-INF/applicationContext.xml");

然后获取相应的bean。

如果代码想用Junit测试框架来测试,则Spring提供了对Junit支持,还可以使用注解的方式:

Java代码

@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration(locations={"file:src/main/webapp/WEB-INF/applicationContext.xml"})

只需要在相应的Test类前面加上此两个注解(第二个注解用来指明Spring的配置文件位置),就可以在Junit Test类使用中Spring提供的依赖注入功能。

下面是一个Spring管理下的Junit测试类:

Java代码

packagecom.sohu.group.service.external;

importjava.util.List;

importorg.junit.Test;

importorg.junit.runner.RunWith;

importorg.springframework.beans.factory.annotation.Autowired;

importorg.springframework.test.context.ContextConfiguration;

importorg.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration({"file:src/main/webapp/WEB-INF/applicationContext.xml"})

publicclassSuFriendServiceImplOverRMITest{

@Autowired

privateSuFriendServicesuFriendService;

@Test

publicvoidgetUserFollowerListTest(){

List<String>list=suFriendService.getUserFollowerList("liug_talk@163.com");

System.out.println("------"+list);

}

}

关于读取WEB-INF 下applicationContext.xml配置文件,spring test 怎么读取web-inf下的applicationcontext的介绍到此结束,希望对大家有所帮助。