WEB应用通过Spring注入实现类
来源:互联网 发布:笨方法学python第三版 编辑:程序博客网 时间:2024/05/29 17:54
众所周知Spring实现了控制反转和依赖注入
下面以一简单Web应用来说明如何通过Spring注入实现类代码如下
代码1:IDateFormat
//接口定义
package com.doit8.common.util;
public interface IDateFormat {
public String getNowDate();
}
代码2:DateFormatImpl
//实现类,该实现类作用只是取得当前日期并格式化它.
package com.doit8.common.util;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateFormatImpl implements IDateFormat{
public String getNowDate() {
Date date=new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String dateString = formatter.format(date);
return dateString;
}
}
代码3:MyBeanFactory
//Bean工产通过Spring取得注入的实现类方法,通过传入的request取得Web应用的上下文环境
package com.doit8.common.util;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.servlet.support.RequestContextUtils;
public class MyBeanFactory {
private static BeanFactory overrideFactory = null;
private static BeanFactory factory = null;
/**
* 获取spring实例
* @return
* @throws InstantiationException
*/
public static Object getInstance(HttpServletRequest request,String beanName) throws InstantiationException {
try {
if (overrideFactory != null) {
Object reply = overrideFactory.getBean(beanName);
return reply;
}
if (factory == null) {
factory = getBeanFactory(request);
overrideFactory = factory;
}
Object reply = factory.getBean(beanName);
return reply;
} catch (RuntimeException ex) {
throw ex;
} catch (Exception ex) {
throw new InstantiationException(ex.toString());
}
}
/**
* 获取spring bean工厂
*
* @return
*/
private static BeanFactory getBeanFactory(HttpServletRequest request) {
ServletContext srvCtx = request.getSession().getServletContext();
if (request != null) {
return RequestContextUtils.getWebApplicationContext(request, srvCtx);
} else {
return WebApplicationContextUtils.getWebApplicationContext(srvCtx);
}
}
}
代码4:TestLoadClass
//取得注入实现类的JAVABEAN
package test;
import javax.servlet.http.HttpServletRequest;
import com.doit8.common.util.DateFormatImpl;
import com.doit8.common.util.IDateFormat;
import com.doit8.common.util.MyBeanFactory;
public class TestLoadClass {
IDateFormat datefomat;
private String datestr;
public TestLoadClass(HttpServletRequest request)
{
try {
//根据Spring配置的BeanName取得实现类(Spring配置见下配置文件)
datefomat=(DateFormatImpl)MyBeanFactory.getInstance(request,"DateFormat");
datestr=datefomat.getNowDate();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public String getDate()
{
return datestr;
}
}
配置文件1:web.xml
需要在web.xml文件中指定Spring的配置文件侦听类
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<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>
</web-app>
配置文件2:applicationContext.xml
该配置文件指定实现类
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="DateFormat" class="com.doit8.common.util.DateFormatImpl">
</bean>
</beans>
完成以上配置后就可以在Web应用中用到Spring配置中配置的实现类了。见下JSP代码
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ page import="test.TestLoadClass"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'testjsp.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<%
TestLoadClass tlc=new TestLoadClass(request);
String datestr=tlc.getDate();
out.println(datestr);
%>
</body>
</html>
运行结果在页面上显示当前格式化后的日期
- WEB应用通过Spring注入实现类
- spring 通过中间层工具类SpringBeanFactoryUtils 实现service 注入
- spring web应用中new出来的Bean实现属性注入的方法
- spring通过构造注入
- 非web的JAVA应用使用Spring的依赖注入
- 通过读取Properties文件实现仿照Spring的注入
- 通过Junit和Spring-Test测试SpringMVC的web应用
- Spring注入方式四:通过扫描类路径来把类对象注入Spring容器
- 自己实现的Spring父类注入
- spring接口实现类 依赖注入
- Spring使用注解注入接口实现类
- Spring注入实现接口的某个类
- spring 注解实现注入
- 通过 Terracotta实现基于Tomcat的Web应用集群
- 通过 Terracotta实现基于Tomcat的Web应用集群
- 通过 Terracotta实现基于Tomcat的Web应用集群
- Spring零配置通过注解实现Bean依赖注入总结<转>
- Spring架构学习(2)通过Properties文件实现依赖注入
- Winsock编程宝典
- asp.net中的模态对话框
- WTL 具体介绍,及基础知识
- 通往AJAX之捷径:DWR(Direct web Remoting)
- 异步Socket编程I
- WEB应用通过Spring注入实现类
- 一道关于时间复杂度的数据结构题目
- 安装Vista小小心得
- JSTL教程
- 判断DataTable中某一行某列的数据为空值的办法
- 关于jet db的连接字串,以及加密后的字串
- 我的前途
- Lie, lay 的用法
- 劝忍百箴