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>
  • 相关阅读:
    csv导入数据到mongodb3.2
    [转]教你十分钟下载并破解IntelliJ IDEA(2017)
    Richard Stallman:让我们关注和尊敬自由软件教父
    约翰·卡马克和他的id Software
    fvwm:还是觉得你最好
    《程序员修炼之道》读书心得
    平铺式窗口管理器 Musca 初体验
    最小主义:我的Musca桌面环境
    Vim,Emacs排名不分先后
    在Emacs中画思维导图
  • 原文地址:https://www.cnblogs.com/kikyoqiang/p/11827261.html
Copyright © 2011-2022 走看看