搭建Spring:
上个星期天开始做老师给的东西,这次打算用上刚学习了得MyBatis,然后就还想要要能上JAVA里面很强大的很好用的Spring框架,但是我以前也没有学过,直接是硬着头皮来做的。因为我采用的是mybatis-3.0.6-bundle+spring-3.0.6.RELEASE这两个版本的整合,都比较新,所以网上资料还是很少的。所以遇到了很多问题,故作此次总结,希望以后能帮助到各位。
准备工作:
1. 工欲善其事必先利其器,首先我们需要准备好我们需要的东西。列表如下:
文件
|
说明
|
spring-framework-3.0.6.RELEASE-with-docs.zip
|
Spring的开发框架
|
mysql-connector-java-5.1.13-bin.jar
|
Mysql的驱动包
|
commons-logging-1.1.1-bin.zip
|
Spring开发的依赖包,这个包很重要,我刚开始的时候因为这个包没有加入,一直启动tomcat出错。
|
mybatis-3.0.6-bundle.zip
|
Mybatis的开发框架
|
mybatis-spring-1.0.3-SNAPSHOT-bundle
|
Mybatis和spring的整合包。因为spring3.0比mybatis3.0早出来,所以spring3.0并不支持mybatis3.0。所以这个包是mybatis出的,为了让spring3.0支持mybatis3.0
|
Commons-dbcp.jar
|
这两个是spring的数据源支持包。就是为了在spring中使用数据源。
|
Commons-pool.jar
|
jakarta-log4j-1.2.8.zip
|
日志记录包
|
上面的东西大家都可以去官网下载,我就不给地址了,一个个的google吧,肯定能下到的。
2. 解压spring-framework-3.0.6.RELEASE-with-docs.zip,找到dist目录,下面的就是spring会用到的jar包了,具体jar包得作用如下:
org.springframework.aop-3.0.6.RELEASE
|
Spring的面向切面编程,提供AOP(面向切面编程)实现
|
org.springframework.asm- 3.0.6.RELEASE
|
Spring独立的asm程序,Spring2.5.6的时候需要asmJar 包3.0.6开始提供他自己独立的asmJar
|
org.springframework.aspects- 3.0.6.RELEASE
|
Spring提供对AspectJ框架的整合
|
org.springframework.beans-3.0.6.RELEASE
|
SpringIoC(依赖注入)的基础实现
|
org.springframework.context.support-3.0.6.RELEASE
|
Spring-context的扩展支持,用于MVC方面
|
org.springframework.context-3.0.6.RELEASE
|
Spring提供在基础IoC功能上的扩展服务,此外还提供许多企业级服务的支持,如邮件服务、任务调度、JNDI定位、EJB集成、远程访问、缓存以及各种视图层框架的封装等
|
org.springframework.core-3.0.6.RELEASE
|
Spring3.0.6的核心工具包
|
org.springframework.expression-3.0.6.RELEASE
|
Spring表达式语言
|
org.springframework.instrument.tomcat-3.0.6.RELEASE
|
Spring3.0.6对Tomcat的连接池的集成
|
org.springframework.instrument-3.0.6.RELEASE
|
Spring3.0.6对服务器的代理接口
|
org.springframework.jdbc-3.0.6.RELEASE
|
对JDBC的简单封装
|
org.springframework.jms-3.0.6.RELEASE
|
为简化JMS API的使用而作的简单封装
|
org.springframework.orm-3.0.6.RELEASE
|
整合第三方的ORM框架,如hibernate,ibatis,jdo,以及 spring的JPA实现
|
org.springframework.oxm-3.0.6.RELEASE
|
Spring 对Object/XMl的映射支持,可以让Java与XML之间来回切换
|
org.springframework.test-3.0.6.RELEASE
|
对Junit等测试框架的简单封装
|
org.springframework.transaction-3.0.6.RELEASE
|
为JDBC、Hibernate、JDO、JPA等提供的一致的声明式和编程式事务管理
|
org.springframework.web.portlet-3.0.6.RELEASE
|
基于protlet的MVC实现
|
org.springframework.web.servlet-3.0.6.RELEASE
|
基于servlet的MVC实现
|
org.springframework.web.struts-3.0.6.RELEASE
|
整合Struts的时候的支持
|
org.springframework.web-3.0.6.RELEASE
|
SpringWeb下的工具包
|
搭建Spring步骤:
3. 在myeclipse中新建一个web项目,名字叫做TestSpring2,为了省事,直接将上面所有的spring的jar包导入lib文件夹。具体也可以根据上面有选择性的导入。
4. 新建spring的配置文件,名字可以随便起,但是要注意,这个文件的路径是需要在web.xml中配置的,我起名叫做ApplicationContext.xml,放在了web-inf目录下,内容如下:
<?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:context="http://www.springframework.org/schema/context"
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.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- 这个地方开始放置所有bean的配置信息 -->
</beans>
|
记得如果拷贝这里的内容最好先拷贝到记事本,再拷贝到myeclipse下面去,就不会报错了。
5. 找到web.xml文件,打开,加入如下的代码:
<!-- spring的监听以及配置文件加载信息 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<!-- 上面配置的spring配置文件的路径,区分大小写 -->
<param-value>WEB-INF/ApplicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
|
按理来上,做好上面的配置,spring就应该配置好了。但是,如果此时部署到tomcat,在tomcat的config目录中server.xml的最后倒数第三行左右,在</Host>这个结束标签之前加入如下配置:
<Context path="/course" docBase="D:\Workspaces\MyEclipse8.5\TestSpring2\WebRoot" reloadable="true" />
|
Path的值是浏览器的访问项目路径,docBase是项目WebRoot的绝对路径
10. 先配置spring的数据源,也就是配置数据库的连接。
11. 将mysql的驱动包拷贝进lib文件夹,在新建一个properties文件在web-inf文件夹下面,内容如下:
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/projectsystem
user=root
password=root
|
12. 配置spring的配置文件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:context="http://www.springframework.org/schema/context"
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.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- 这个地方开始放置所有bean的配置信息 -->
<!--配置jdbc.properties文件的位置信息,路径还是区分大小写 -->
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" value="WEB-INF/jdbc.properties" />
</bean>
<!-- 读取jdbc.properties文件,配置数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="${user}" />
<property name="password" value="${password}" />
<!-- 数据库连接池配置 -->
<property name="initialSize" value="60" /><!-- 初始化连接数量 -->
<property name="maxActive" value="100" /><!-- 最大连接数量 -->
<property name="maxIdle" value="50" /><!-- 最大空闲连接数量 -->
<property name="minIdle" value="10" /><!-- 最小空闲连接数量 -->
</bean> </beans>
|
这个里面的value使用了${}表达式,{}中的名称就是上面jdbc.properties文件中对应的字段名称。
但是这个时候需要将Commons-dbcp.jar和Commons-pool.jar导入进lib包下。
到这里,Spring的数据源就配置成功了。
13. 然后我们写一个vo类 TestTable.java,内容如下:
package org.yhb.vo;
public class TestTable {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
|
14. ITestTableDAO.java这是一个接口文件,如下:
package org.yhb.vo.dao;
public interface ITestTableDAO {
public void doInsert();
}
|
15. ITestTableDAOImpl.java实现类,实现了上面的接口:
package org.yhb.vo.impl;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.yhb.vo.dao.ITestTableDAO;
public class ITestTableDAOImpl extends JdbcDaoSupport implements ITestTableDAO {
//这个类继承了JdbcDaoSupport类
public void doInsert() {
String sql="insert into testtable (id,name) values(1,'yuanmomo')";
this.getJdbcTemplate().execute(sql);
}
}
|
16. 在写一个service类,调用该实现类,实现业务逻辑处理:
package org.yhb.vo.service;
import org.yhb.vo.dao.ITestTableDAO;
import org.yhb.vo.impl.ITestTableDAOImpl;
public class ITestTableDAOService implements ITestTableDAO {
private ITestTableDAOImpl testImpl;
public ITestTableDAOImpl getTestImpl() {
return testImpl;
}
//这个setter和重要,setter方式只spring在实现注入的时候调用的方法
public void setTestImpl(ITestTableDAOImpl testImpl) {
this.testImpl = testImpl;
}
public void doInsert() {
this.testImpl.doInsert();
}
}
|
17. 再次配置spring的配置文件ApplicationContext.xml,添加入下列的bean的配置信息:
<!-- 配置实现类,注入数据源 -->
<bean id="ITestTableDAOImpl" class="org.yhb.vo.impl.ITestTableDAOImpl">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 配置service类,注入上面的实现类 -->
<bean id="ITestTableDAOService" class="org.yhb.vo.service.ITestTableDAOService">
<property name="testImpl" ref="ITestTableDAOImpl" />
</bean>
|
18. 此时可以启动一下tomcat,会发现正常启动,无报错。
19. 接下来写一个servlet,TestServlet.java在org.yhb.test包下。代码如下:
package org.yhb.test;
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.web.context.support.WebApplicationContextUtils;
import org.yhb.vo.service.ITestTableDAOService;
public class TestServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
this.doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
//初始化Spring的容器,得到一个ApplicationContext对象
ApplicationContext context=WebApplicationContextUtils.
getWebApplicationContext(this.getServletContext());
//从context的得到一个bean,实例化对象
ITestTableDAOService dao=(ITestTableDAOService)context.getBean("ITestTableDAOService");
System.out.println("开始插入数据.....");
dao.doInsert();
System.out.println("插入成功!!!!!!!!!");
}
}
|
20. 在web.xml中配置该servlet,如下:
<!-- 配置TestServlet -->
<servlet>
<servlet-name>testServlet</servlet-name>
<servlet-class>org.yhb.test.TestServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>testServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
|
分享到:
相关推荐
* 日志记录包(jakarta-log4j-1.2.8.zip) Spring 框架的 jar 包 在解压 Spring 框架的压缩包后,可以找到 dist 目录,下面的 jar 包都是 Spring 框架所需的: * org.springframework.aop-3.0.6.RELEASE:提供了...
SpringJUnit4ClassRunner允许开发者在JUnit测试中轻松注入Spring Bean,而Mockito和Spring TestContext Framework则为模拟对象和上下文管理提供了便利。 综上所述,Spring Framework 3.0.6.RELEASE不仅是一个功能...
4. **数据访问/集成**:Spring提供了对JDBC、ORM(Object-Relational Mapping)框架如Hibernate、MyBatis的集成,简化了数据库操作。 二、Spring MVC 1. **Spring MVC**:Spring的Web应用开发模块,提供了一种模型...
在3.0.6.RELEASE中,测试支持得到了进一步增强,如对JUnit 4的全面支持,以及更便捷的模拟对象创建。 8. **模块化设计**:Spring Framework 3.0.6.RELEASE强调模块化,允许开发者根据项目需求选择必要的模块,避免...
log4j-core-2.0-rc1.jar log4j-core-2.3.jar log4jdbc-remix-0.2.7.jar logback-classic-1.0.0.jar logback-core-1.0.0.jar mvel2-2.0.11.jar mybatis-3.1.1.jar mybatis-3.2.2.jar mybatis-3.2.7.jar mybatis-...
这是一个关于bonson的网络框架。 它使用组件: Junit 4.12 春天 3.1.0 ... Log4j 1.2.6 网络.sf.json 2.4 并且它也使用maven来管理项目。你可以在pom.xml中看到详细信息。 希望你能理解我,欢迎fork。