Servlet中如何獲得ApplicationContext , Spring

來源:互聯網
上載者:User
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.javapackage com.wcg;public interface FirstDao {public int Create(String name);}//FirstDaoImpl.javapackage 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.javapackage 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");}}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.