使用WebContainer中的Spring 常用方法
2009-05-18 19:41
603 查看
Spring 的初始化
一、Spring在web.xml的配置
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:spring/*.xml
</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
如上配置就可以在web container中使用Spring了,但有时还会有另一种需求,就是我不要Spring显式或隐式的注入,而是需要在一个工具类中,来获取Spring的相关信息,直接调用Spring Container的相关的方法,以下是一种简单的实现方法.
二、构建自己的工具类
1.创建Container接口
package com.iman.demo.framework;
public interface Container {
public abstract Object getComponent(String name);
}
2.创建Engine 接口
package com.iman.demo.framework;
import javax.servlet.ServletContext;
public interface Engine {
public void startEngine();
public void init(ServletContext servletContext);
public void stopEngine();
public void reloadEngine();
public abstract Container getContainer();
}
3.创建 AbstractEngine 实现 Engine接口的抽象类
package com.iman.demo.framework.spring;
import com.iman.demo.framework.Container;
import com.iman.demo.framework.Engine;
public abstract class AbstractEngine implements Engine{
public AbstractEngine() {
}
}
4.创建AbstractEngine的派生类 SpringEngine
package com.iman.demo.framework.spring;
import javax.servlet.ServletContext;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.iman.demo.framework.Container;
import com.iman.demo.framework.Engine;
public class SpringEngine extends AbstractEngine{
private Container container;
private static ApplicationContext applicationContext;
public SpringEngine() {
System.out.println("Spring Engine is starting...");
}
public void init(ServletContext servletContext){
applicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
container = new SpringContainer(applicationContext);
}
public void reloadEngine() {
}
public void setContainer(Container container) {
this.container = container;
}
public void startEngine() {
}
public void stopEngine() {
}
public Container getContainer() {
return container;
}
}
5.创建 Container 的实现类 SpringContainer
package com.iman.demo.framework.spring;
import org.springframework.context.ApplicationContext;
import com.iman.demo.framework.Container;
public class SpringContainer implements Container {
private ApplicationContext applicationContext;
public SpringContainer(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
public Object getComponent(String name) {
return applicationContext.getBean(name);
}
}
6.创建 Framework 工具类
package com.iman.demo.framework;
import javax.servlet.ServletContext;
import com.iman.demo.framework.spring.SpringEngine;
public class Framework {
private String webroot;
private static Engine engine = new SpringEngine();
private Container container;
public static Engine getEngine(){
return engine;
}
public static Engine createEngine(Class clazz,ServletContext servletContext){
try {
engine = (Engine)clazz.newInstance();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
}
engine.init(servletContext);
return engine;
}
}
7.创建 Servlet Listener
package com.iman.demo.framework.spring;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import com.iman.demo.framework.Engine;
import com.iman.demo.framework.Framework;
public class FrameworkStartListener implements ServletContextListener{
public void contextDestroyed(ServletContextEvent arg0) {
}
public void contextInitialized(ServletContextEvent event) {
String start = event.getServletContext().getInitParameter("engine");
try {
Class clazz = Class.forName(start);
ServletContext servletContext = event.getServletContext();
Framework.createEngine(clazz, servletContext);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
好啦,所有的接口以及实现类,工具类等都建好了,现在就配置一下web.xml文件
添加内容如下:
<context-param>
<param-name>engine</param-name> <param-value>com.iman.demo.framework.spring.SpringEngine</param-value>
</context-param>
<listener> <listener-class>com.iman.demo.framework.spring.FrameworkStartListener</listener-class>
</listener>
一切OK,WebContainer 启动后,就可以调用Framework中的方法了。
FrameworkStartList 根据 engine的配置参数来调用 Framework 中的createEngine方法来初始化Spring的相关信息到该工具类中。这只是一个简单配置和方法,具体的需求可以以此为基础,或作为参考,来完成更加复杂和灵活的用法。
一、Spring在web.xml的配置
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:spring/*.xml
</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
如上配置就可以在web container中使用Spring了,但有时还会有另一种需求,就是我不要Spring显式或隐式的注入,而是需要在一个工具类中,来获取Spring的相关信息,直接调用Spring Container的相关的方法,以下是一种简单的实现方法.
二、构建自己的工具类
1.创建Container接口
package com.iman.demo.framework;
public interface Container {
public abstract Object getComponent(String name);
}
2.创建Engine 接口
package com.iman.demo.framework;
import javax.servlet.ServletContext;
public interface Engine {
public void startEngine();
public void init(ServletContext servletContext);
public void stopEngine();
public void reloadEngine();
public abstract Container getContainer();
}
3.创建 AbstractEngine 实现 Engine接口的抽象类
package com.iman.demo.framework.spring;
import com.iman.demo.framework.Container;
import com.iman.demo.framework.Engine;
public abstract class AbstractEngine implements Engine{
public AbstractEngine() {
}
}
4.创建AbstractEngine的派生类 SpringEngine
package com.iman.demo.framework.spring;
import javax.servlet.ServletContext;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.iman.demo.framework.Container;
import com.iman.demo.framework.Engine;
public class SpringEngine extends AbstractEngine{
private Container container;
private static ApplicationContext applicationContext;
public SpringEngine() {
System.out.println("Spring Engine is starting...");
}
public void init(ServletContext servletContext){
applicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
container = new SpringContainer(applicationContext);
}
public void reloadEngine() {
}
public void setContainer(Container container) {
this.container = container;
}
public void startEngine() {
}
public void stopEngine() {
}
public Container getContainer() {
return container;
}
}
5.创建 Container 的实现类 SpringContainer
package com.iman.demo.framework.spring;
import org.springframework.context.ApplicationContext;
import com.iman.demo.framework.Container;
public class SpringContainer implements Container {
private ApplicationContext applicationContext;
public SpringContainer(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
public Object getComponent(String name) {
return applicationContext.getBean(name);
}
}
6.创建 Framework 工具类
package com.iman.demo.framework;
import javax.servlet.ServletContext;
import com.iman.demo.framework.spring.SpringEngine;
public class Framework {
private String webroot;
private static Engine engine = new SpringEngine();
private Container container;
public static Engine getEngine(){
return engine;
}
public static Engine createEngine(Class clazz,ServletContext servletContext){
try {
engine = (Engine)clazz.newInstance();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
}
engine.init(servletContext);
return engine;
}
}
7.创建 Servlet Listener
package com.iman.demo.framework.spring;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import com.iman.demo.framework.Engine;
import com.iman.demo.framework.Framework;
public class FrameworkStartListener implements ServletContextListener{
public void contextDestroyed(ServletContextEvent arg0) {
}
public void contextInitialized(ServletContextEvent event) {
String start = event.getServletContext().getInitParameter("engine");
try {
Class clazz = Class.forName(start);
ServletContext servletContext = event.getServletContext();
Framework.createEngine(clazz, servletContext);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
好啦,所有的接口以及实现类,工具类等都建好了,现在就配置一下web.xml文件
添加内容如下:
<context-param>
<param-name>engine</param-name> <param-value>com.iman.demo.framework.spring.SpringEngine</param-value>
</context-param>
<listener> <listener-class>com.iman.demo.framework.spring.FrameworkStartListener</listener-class>
</listener>
一切OK,WebContainer 启动后,就可以调用Framework中的方法了。
FrameworkStartList 根据 engine的配置参数来调用 Framework 中的createEngine方法来初始化Spring的相关信息到该工具类中。这只是一个简单配置和方法,具体的需求可以以此为基础,或作为参考,来完成更加复杂和灵活的用法。
相关文章推荐
- WebView的简单使用和常用方法、属性!
- JavaWEB springmvc 使用定时任务(即实例化两次解决方法)
- IOS中过滤HTML标签&&WebView的三种常用的使用方法
- Spring Boot 与 kotlin 使用Thymeleaf模板引擎渲染web视图的方法
- 在使用selenium webdriver进行元素定位时,通常使用findElement或findElements方法结合By类返回的元素句柄来定位元素。其中By类的常用定位方式共八种,现分别介绍如下
- Web.config中常用语句的使用方法和调用方法:
- Web项目中加载Spring配置的常用方法
- 注解的力量 -----Spring 2.5 JPA hibernate 使用方法的点滴整理(六): 一些常用的数据库 注解
- 注解的力量 -----Spring 2.5 JPA hibernate 使用方法的点滴整理(六): 一些常用的数据库 注解
- [WEB开发][数据库开发/应用]SQL常用命令使用方法
- JAVA EE项目常用知识 之 整合了spring 项目中使用log4j的一种方法
- mongodb java api常用方法的使用以及和spring的集成使用
- Spring Web MVC注解应用及一些常用方法技巧
- Spring常用注解 使用注解来构造IoC容器的方法
- SDWebImage 的常用方法使用
- 注解的力量 -----Spring 2.5 JPA hibernate 使用方法的点滴整理(六): 一些常用的数据库 注解
- JavaWeb常用开发技术总结-js、spring、javabean、mysql数据库、dwr的使用等
- web---firebug常用功能使用方法
- 注解的力量 -----Spring 2.5 JPA hibernate 使用方法的点滴整理(六): 一些常用的数据库 注解
- 转载:注解的力量 -----Spring 2.5 JPA hibernate 使用方法的点滴整理(六): 一些常用的数据库 注解