ApplicationContext AnnotationConfigApplicationContext WebApplicationContext ConfigurablwWebApplicat

来源:互联网 发布:圣骑士挖矿软件 编辑:程序博客网 时间:2024/06/06 03:54

ApplicationContext

ApplicationContext是由BeanFactory派生出来的,主要实现类是ClassPathXmlApplicationContext和FileSystemXmlApplicationContext,二者的区别在于配置文件的默认加载路径不同。

AnnotationConfigApplicationContext

<beans><bean id="course" class="demo.Course"><property name="module" ref="module"/>  </bean><bean id="module" class="demo.Module"><property name="assignment" ref="assignment"/>  </bean><bean id="assignment" class="demo.Assignment" /></beans>

以上的XML就是你使用Spring配置bean时通常会编写的代码,现在要删除这段XML,编写同等效果的Java代码。

@Configurationpublic class AppContext {@Beanpublic Course course() {Course course = new Course();course.setModule(module());return course;}@Beanpublic Module module() {Module module = new Module();module.setAssignment(assignment());return module;}@Beanpublic Assignment assignment() {return new Assignment();}}
AnonotationConfigApplicationContext类是ApplicationContext接口的一个实现,使你能够注册所注解的配置类。

配置类使用@Configuration注解,注册的类使用@Bean注解。

public static void main(String[] args) {  ApplicationContext ctx = new AnnotationConfigApplicationContext(AppContext.class);  Course course = ctx.getBean(Course.class);  course.getName();}


<web-app><context-param><param-name>contextConfigLocation</param-name><param-value>/WEB-INF/applicationContext.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><servlet><servlet-name>sampleServlet</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class></servlet>...</web-app>
通常使用XmlWebApplicationContext上下文来配置Spring Web应用程序,XmlWebApplicationContext是web应用程序 使用的的默认上下文类。

<web-app><context-param><param-name>contextClass</param-name><param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value></context-param><context-param><param-name>contextConfigLocation</param-name><param-value>demo.AppContext</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><servlet><servlet-name>sampleServlet</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextClass</param-name><param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value></init-param></servlet>...</web-app>

修改后web.xml现在定义了AnnotationConfigWebApplicationContext上下文类,并将其作为上下文参数和servlet元素的一部分。

WebApplicationContext

专门为web应用准备,它允许从相对于web根目录的路径中加载配置文件完成初始化工作。它扩展了ApplicationContext,定义了一个常量ROOT_WEB_APPLICATION_CONTEXTL_ATTRIBUTE,通过下面语句可以获取WebApplicationContext.

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


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等格式。



0 0