Servlet中如何获得ApplicationContext , Spring

applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

	xmlns:aop="http://www.springframework.org/schema/aop"

	xmlns:tx="http://www.springframework.org/schema/tx"

	xsi:schemaLocation="

       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd

       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd

       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd"

	default-lazy-init="false">



	<!-- data source -->



	<bean id="dataSource"

		class="org.springframework.jndi.JndiObjectFactoryBean">

		<property name="jndiName"

			value="java:comp/env/jdbc/MyTestProject" />

	</bean>





	<!-- 配置数据源 供测试用-->

	<bean id="dataSourceDebug"

		class="org.springframework.jdbc.datasource.DriverManagerDataSource">

		<property name="driverClassName"

			value="com.microsoft.sqlserver.jdbc.SQLServerDriver">

		</property>

		<property name="url"

			value="jdbc:sqlserver://192.168.0.1\SQLEXPRESS:1433;dataBaseName=Test;loginTimeout=3;">

		</property>

		<property name="username" value="sa"></property>

		<property name="password" value="123"></property>

	</bean>



	<bean id="abstractDao" abstract="true">

		<!-- 

			<property name="dataSource" ref="dataSourceDebug"></property>

		-->

		<property name="dataSource" ref="dataSource"></property>

	</bean>



	<bean id="firstDao" class="com.wcg.FirstDaoImpl"

		parent="abstractDao">

	</bean>



</beans>

web.xml:
<?xml version="1.0" encoding="UTF-8"?>

<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"

	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 

	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">



	<!--  Spring context loader -->

	<listener>

		<listener-class>

			org.springframework.web.context.ContextLoaderListener

		</listener-class>

	</listener>

	<servlet>

		<description>测试FirstDao</description>

		<display-name>FirstServlet</display-name>

		<servlet-name>FirstServlet</servlet-name>

		<servlet-class>com.servlet.FirstServlet</servlet-class>

	</servlet>



	<servlet-mapping>

		<servlet-name>FirstServlet</servlet-name>

		<url-pattern>/servlet/FirstServlet</url-pattern>

	</servlet-mapping>



	<!-- jdbc resource -->

	<resource-ref>

		<res-ref-name>jdbc/MyTestProject</res-ref-name>

		<res-type>javax.sql.DataSource</res-type>

		<res-auth>Container</res-auth>

	</resource-ref>



</web-app>

//FirstDao.java



package com.wcg;



public interface FirstDao {



	public int Create(String name);

	

}



//FirstDaoImpl.java

package com.wcg;



import java.util.HashMap;

import java.util.Map;



import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;



public class FirstDaoImpl extends SimpleJdbcDaoSupport implements FirstDao {



	public int Create(String name) {

		String sql = "insert into T (Name) values(:Name)";

		Map<String, Object> param = new HashMap<String, Object>();

		param.put("Name", name);

		return this.getSimpleJdbcTemplate().update(sql, param);

	}



}



//FirstServlet.java

//...



	public void doGet(HttpServletRequest request, HttpServletResponse response)

			throws ServletException, IOException {



		response.setContentType("text/html");

		PrintWriter out = response.getWriter();



		ServletContext sc = request.getSession().getServletContext();

		WebApplicationContext ctx = (WebApplicationContext)sc.getAttribute("org.springframework.web.context.WebApplicationContext.ROOT");

		String name = request.getParameter("name");

		FirstDao fd = (FirstDao) ctx.getBean("firstDao");

		fd.Create(name);



		out.println("Done");

		out.flush();

		out.close();

	}

//...



测试,web容器之外的测试类.

//Test.java

package com.wcg;



import org.springframework.context.ApplicationContext;

import org.springframework.context.support.FileSystemXmlApplicationContext;



public class Test {



	/**

	 * @param args

	 */

	public static void main(String[] args) {



		//ApplicationContext ctx = new ClassPathXmlApplicationContext("..\\applicationContext.xml");

		ApplicationContext ctx = new FileSystemXmlApplicationContext("D:\\proj_ja\\java\\TestWeb\\WebRoot\\WEB-INF\\applicationContext.xml");

		

		

		FirstDao fd = (FirstDao) ctx.getBean("firstDao");

		fd.Create("username");



		System.out.println("done");

	}



}

你可能感兴趣的:(Servlet中如何获得ApplicationContext , Spring)