web project 部署 spring bean工厂
前提:已经创建web project(接上一篇:eclipse 创建Dynamic web project)
一:准备spring依赖包
1:spring-core-4.1.6.RELEASE.jar
2:spring-context-4.1.6.RELEASE.jar (ApplicationContext、ApplicationContext)
3:spring-beans-4.1.6.RELEASE.jar
4:spring-expression-4.1.6.RELEASE.jar
5:spring-web-4.1.6.RELEASE.jar (ContextLoaderListener)
6:commons-logging-1.1.1.jar
mysql数据库依赖包:mysql-connector-java-5.1.9.jar
二:分别创建三层类
1:servlet:Student.java(extends HttpServlet)
2:service:StudentServiceImpl.java、StudentService.java(interface)
3:dao:StudentDaoImpl.java、StudentDao.java(interface)
三:配置文件
1:web.xml [beans路径、监听器]
2:applicationContext.xml (src目录下)[beans]
四:测试
1:http://localhost:post/web_name/student
ps:详情如下:
1:web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>test</display-name> <!-- 默认配置在WEB-INF目录下 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:/applicationContext.xml</param-value> <!-- <param-value>/WEB-INF/spring*.xml</param-value> --> <!-- <param-value>/WEB-INF/applicationContext.xml</param-value> --> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- OR USE THE CONTEXTLOADERSERVLET INSTEAD OF THE LISTENER <servlet> <servlet-name>context</servlet-name> <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> --> <servlet> <servlet-name>studentMap</servlet-name> <servlet-class>servlet.Student</servlet-class> </servlet> <servlet-mapping> <servlet-name>studentMap</servlet-name> <url-pattern>/student</url-pattern> </servlet-mapping> </web-app>
2:applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd"> <beans> <bean id="studentDao" class="dao.StudentDaoImpl"></bean> <bean id="studentService" class="service.StudentServiceImpl"> <property name="studentDao" ref="studentDao"></property> </bean> </beans>
3:servlet
package servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import service.StudentService; public class Student extends HttpServlet { @Override protected void service(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException { // super.service(arg0, arg1); // String springConfigUrl = this.getServletContext().getInitParameter("contextConfigLocation"); ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml"); StudentService service =(StudentService)ctx.getBean("studentService"); service.insertStudent(null); } }
4:service
package service; import dao.StudentDao; import entity.Student; public class StudentServiceImpl implements StudentService { private StudentDao studentDao; public void setStudentDao(StudentDao studentDao) { this.studentDao = studentDao; } public void insertStudent(Student student){ studentDao.insert(student); } }
5:dao
package dao; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; import entity.Student; public class StudentDaoImpl implements StudentDao { static final String driver = "com.mysql.jdbc.Driver"; static final String url = "jdbc:mysql://xxx:3306/test?autoReconnect=true&useUnicode=true&characterEncoding=utf8&logSlowQueries=false&slowQueryThresholdMillis=0"; static final String userName = "xxx"; static final String password = "xxx"; try{ Class.forName(driver); Connection con = DriverManager.getConnection(url,userName,password); Statement sql = con.createStatement(); sql.execute("insert student(name,math) values('2a','99')"); sql.close(); con.close(); }catch(java.lang.ClassNotFoundException e){ System.err.println("ClassNotFoundException:" + e.getMessage()); }catch(SQLException ex){ System.err.println("SQLException:" + ex.getMessage()); } } }
public void insert(Student student){