zoukankan      html  css  js  c++  java
  • Sping IOC容器

    Sping IOC容器

    package servlet;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.web.context.support.WebApplicationContextUtils;
    import service.IStudentService;
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import java.io.IOException;
    
    @WebServlet("/QueryStudentByIdServlet")
    public class QueryStudentByIdServlet extends javax.servlet.http.HttpServlet {
        private IStudentService studentService;
    
        public void setStudentService(IStudentService studentService) {
            this.studentService = studentService;
        }
    
        @Override
        public void init() throws ServletException {
            super.init();
            ApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
            studentService = (IStudentService) applicationContext.getBean("studentServiceId");
        }
    
        protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
                throws javax.servlet.ServletException, IOException {
            String name = studentService.queryStudentById();
            request.setAttribute("name", name);
            request.getRequestDispatcher("result.jsp").forward(request, response);
        }
    
        protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
                throws javax.servlet.ServletException, IOException {
            doGet(request, response);
        }
    }

    web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
             version="3.1">
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:applicationContext.xml, classpath:applicationContext-*.xml</param-value>
        </context-param>
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
    </web-app>

    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"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
        
    </beans>

    applicationContext-Controller.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"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
        <bean id="studentServlet" class="servlet.QueryStudentByIdServlet">
            <property name="studentService" ref="studentServiceId">  </property>
        </bean>
    </beans>

    applicationContext-Service.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"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
        <bean id="studentServiceId" class="service.impl.StudentServiceImpl">
            <property name="studentDao" ref="studentDaoId"></property>
        </bean>
    </beans>

    applicationContext-Dao.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"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
        <bean id="studentDaoId" class="dao.impl.StudentDaoImpl"></bean>
    </beans>
  • 相关阅读:
    史上最全的浏览器 CSS & JS Hack 手册
    JavaScript1.6数组新特性和JQuery的几个工具方法
    用jquery循环map
    javascript强大的日期函数
    用 javascript 判断 IE 版本号
    常见排序算法基于JS的实现
    JavaScript中callee,caller,argument的理解
    apply()方法和call()方法
    虽然我们可能不想对元素应用3D变换,可我们一样可以开启3D引擎
    在移动端上加上代码,让字体变得平滑
  • 原文地址:https://www.cnblogs.com/kikyoqiang/p/11827261.html
Copyright © 2011-2022 走看看