spring所需要的核心jar包:
spring-beans.jar : 访问配置文件、创建和管理bean 以及进行Inversion of Control / Dependency Injection(IoC/DI)操作相关的所有类spring-context.jar : 可以找到使用Spring ApplicationContext特性时所需的全部类,JDNI 所需的全部类,instrumentation组件以及校验Validation 方面的相关类spring-core.jar : Spring 框架基本的核心工具类spring-jdbc.jar : 包含对Spring 对JDBC 数据访问进行封装的所有类 spring-orm.jar : 对映射框架的一些支持spring-tx.jar : 提供对事务的支持spring-web:包含Web应用开发时,用到Spring框架时所需的核心类,包括自动载入这些都是spring的核心包。后面也带了详细解释。这些都是比较必要的,但是如果不是web项目,就可以不需要spring-web,不需要使用数据库或则hibernate就没必要用spring-jdbc,可以直接在dao层上面用jdbc。
我这里提供下我的Jar包
<groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>3.0.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>3.0.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>3.0.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>3.0.6.RELEASE</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.6.1</version> </dependency>当然与Struts2整合,还少不了一个重要的包,就是struts2-spring-plugin.jar
<dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-spring-plugin</artifactId> <version>2.1.8.1</version> </dependency>PS:我本来想着用Spring4的,结果一直报包冲突的错误。这个很坑爹,我就换成了Spring3了。
这文章是基于上一篇struts2详解与初步使用,我将两个内容整合起来了。在做这个前,先打起struts2哈。
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"> <bean id="loginService" class="service.LoginService"> </bean> </beans>这里beans 里面的内容,直接复制下来就好。
<bean id="这个id要与action中创建的对象名相同" class="这个就是该类的路径了"></bean>而这个就是给action注入service对象。另外还需要注意的是,action中的service对象,一定需要添加get,set方法。这个就是spring的注入机制。
总结下,spring使用的就是 1、配置web.xml 2、创建applicationContext.xml文件,进行对象注入 3、action中的对象进行get,set。web.xml是web项目的基础配置文件,spring在里面配置监听和告知applicationContext.xml文件在哪。
<!-- 配置Spring配置文件的名称和位置 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!-- 启动IOC容器的ServletContextListner --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- struts2的配置 --> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>jsp/index.jsp</welcome-file> </welcome-file-list>
我把之前的struts2的配置也一起带上,这个没什么难度,第一个context-param里面的value,就是指明,classpath:applicationContext.xml,这里就是说明,他在src包下。到时候整合Hibernate的时候,会创建多个包,在把这个classpath的多个路径整合。下面一个就是配置监听了。配置完这两个就可以使用了。
package service; /** * 2017-5-3 * author:饶为 * Administrator */ public class LoginService { public void dosomething(){ System.out.println("我开始做事了"); } } 这里没什么,就是用来测试的。
这里顺便使用说明下如何把后台的值取到前台,可以使用S标签,也可以直接$符号取值就可以了。 struts.xml文件并没有做修改。可以沿用原来的。
看到“我开始做事了”,就说明成功了。
其实spring最重要的作用就是创建个对象,不需要再去new了。直接使用就好了 spring还有aop,面向切面编程。可以参考spring面向切面AOP,这是我之前写的博文。
在整合这个的过程中,遇到几个常见问题,我列出下:
org.springframework.web.context.ContextLoaderListener class not find.答:这个是没有找到包,maven上的包没有部署到服务器上,可以去服务器上lib目录下查看是否包都部署上去了。
IOException parsing XML document from class path resource [applicationContext.xml]; nested exception is java.io.FileNotFoundException: class path resource [applicationContext.xml] cannot be opened because it does not exist回答:检查web.xml的classpath: 后面的地址是否正确,如果直接放在src下,就是classpath:applicationContext.xml,如果放在其他的包下,或则在其他地方就要加上绝对路径。
JavaWeb:java.lang.NoClassDefFoundError: org/springframework/core/OrderComparator$OrderSourceProvider回答:出现这个问题是因为我用的包是spring-core-4.0.5.RELEASE.jar,有人说改成了4.1.6就成功了。然而我并没有解决这个错误,所以我更换成了上文提供的3+版本。
Java.lang.NoSuchMethodError: org.springframework.core.CollectionFactory.createConcurrentMapIfPossible(I)Ljava/util/Map回答:这个异常一般都是jar包版本不对,你没有导入相关的包,或者你有不同版本的包,旧的版本覆盖了新的版本。多去看看服务器上的jar包是否重复。因为maven经常有依赖包。我在更换成spring3.0.6后不再有这个错误。
