SPING IOCコンテナ
パッケージサーブレット。 輸入org.springframework.context.ApplicationContext; 輸入org.springframework.web.context.support.WebApplicationContextUtils。 輸入service.IStudentService。 輸入javax.servlet.ServletException; 輸入javax.servlet.annotation.WebServlet; インポートにjava.io.IOException; @WebServlet( "/ QueryStudentByIdServlet" ) パブリック クラス QueryStudentByIdServletは延びjavax.servlet.http.HttpServlet { プライベートIStudentService studentServiceと、 公共 ボイドsetStudentService(IStudentService studentService){ この .studentService = studentService。 } @オーバーライド 公共 空のinit()スローServletExceptionが{ スーパー.INITを(); ApplicationContextのApplicationContextの = WebApplicationContextUtils.getWebApplicationContext(この.getServletContext())。 studentService =(IStudentService)applicationContext.getBean( "studentServiceId" )。 } 保護された ボイドのdoGet(要求するjavax.servlet.http.HttpServletRequest、javax.servlet.http.HttpServletResponseの応答)が スローjavax.servlet.ServletException、IOExceptionが{ 文字列名 = studentService.queryStudentById()。 request.setAttribute( "名前" 、名); request.getRequestDispatcher( "はresult.jsp" )の.forward(リクエスト、レスポンス)。 } 保護された ボイドdoPostメソッド(要求するjavax.servlet.http.HttpServletRequest、javax.servlet.http.HttpServletResponseの応答)が スローjavax.servlet.ServletException、IOExceptionが{ doGet(リクエスト、レスポンス)。 } }
web.xmlの
<?xmlのバージョン= "1.0"エンコード= "UTF-8" ?> < ウェブアプリのxmlns = "http://xmlns.jcp.org/xml/ns/javaee" のxmlns:XSI = "のhttp://www.w3 .ORG / 2001 / XMLスキーマ・インスタンス" のxsi:schemaLocationの=" http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd " バージョン=" 3.1" > < 文脈のparam > < PARAM名> contextConfigLocation </ PARAM名> < PARAM-値>クラスパス:applicationContext.xmlを、クラスパス:applicationContext- *。 </ コンテキストのparam > < 聞き手> < リスナークラス> org.springframework.web.context.ContextLoaderListener </ リスナクラス> </ リスナー> </ ウェブアプリ>
applicationContext.xmlを
<?= "1.0"エンコード= "UTF-8" XMLバージョン?> < 豆のxmlns = "http://www.springframework.org/schema/beans" のxmlns:XSI = "http://www.w3.org/2001 / XMLスキーマ・インスタンス" のxsi:schemaLocationの=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdのhttp://www.springframework。 ORG /スキーマ/コンテキストhttp://www.springframework.org/schema/context/spring-context.xsd」> </ 豆>
ApplicationContextの-Controller.xml
<?= "1.0"エンコード= "UTF-8" XMLバージョン?> < 豆のxmlns = "http://www.springframework.org/schema/beans" のxmlns:XSI = "http://www.w3.org/2001 / XMLスキーマ・インスタンス" のxsi:のschemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" > < ビーンID =" studentServlet " クラス=" servlet.QueryStudentByIdServlet」> < プロパティ名= "studentService" REF = "studentServiceId" > < </ 豆>
ApplicationContextの-service.xmlに
<?= "1.0"エンコード= "UTF-8" XMLバージョン?> < 豆のxmlns = "http://www.springframework.org/schema/beans" のxmlns:XSI = "http://www.w3.org/2001 / XMLスキーマ・インスタンス" のxsi:のschemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" > < ビーンID =" studentServiceId " クラス=" service.impl.StudentServiceImpl」> < プロパティ名= "studentDao" REF = "studentDaoId" > </ プロパティ> >
ApplicationContextの-Dao.xml
<?= "1.0"エンコード= "UTF-8" XMLバージョン?> < 豆のxmlns = "http://www.springframework.org/schema/beans" のxmlns:XSI = "http://www.w3.org/2001 / XMLスキーマ・インスタンス" のxsi:のschemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" > < ビーンID =" studentDaoId " クラス=" dao.impl.StudentDaoImpl」> </ 豆> </ 豆>