Spring4.X——搭建

时间:2022-05-21 06:00:04

一,Spring概念总结

spring是一个集成了许多第三方框架的大杂烩,其核心技术是IOC(控制反转,也称依赖注入)和AOP(面向切面编程),所以spring既是一个IOC容器,也是一个AOP框架。我们知道没有Spring,Struts和Hibernate可以很好的工作,在开篇中我把没有Spring的架构称为“独木桥”,有Spring的架构称为“阳光大道”。说白了,Spring解决了企业应用开发的复杂性,用基本的javaBean来完成EJB的事情,从大小和开销方向说,Spring都是轻量级的。Spring具有很多的优点:

1、使我们的分层更好。

SSH框架系统从职责上分为四层:表示层、业务逻辑层、数据持久层和域模块层(实体层)。

Spring4.X——搭建

从上图我们可以看到Spring处于业务逻辑层,担任连接Struts和Hibernate桥梁的角色。系统的整个层次关系可以一目了然。

2、对象管理更好。

从上图,我们看到Spring将Transactions、Session以及业务层服务都放到了业务逻辑层来管理。系统的条理变得更加清晰,不仅使持久化层的职责更加单一,而且提高了系统的灵活性。

3、AOP

面向切面编程,AOP让开发人员创建非行为性的关注点,并将它们插入到应用代码红。公共服务(比如日志、持久性、事务等)就可以就可以分解成方面并应用到域对象上,同时不会增加域对象的对象模型的复杂性。

二,Spring框架的搭建

1.到spring官网http://projects.spring.io/spring-framework/下载spring插件,这里使用的是spring-framework-4.2.2.RELEASE版本;

2.导入spring插件jar包到项目的lib文件夹下:

Spring4.X——搭建

注意:其中struts2-spring-plugin-2.3.30.jar和commons-logging-1.1.3.jar在Struts2的jar包里;

2.导入包后,到web.xml中配置spring的监听器:

   <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

3.然后我们要把spring和Struts连起来,在Struts.xml中配置如下代码:

 <!-- include文件用于分割,实现多人并发不冲突 -->
<struts>
<!-- 告知Struts2运行时使用Spring来创建对象 -->
<!--指定Struts 2默认的ObjectFactory Bean,该属性默认值是spring-->
<constant name="struts.objectFactory" value="spring" />//name属性值不能随便乱写
<include file="s001.xml" />
<include file="s002.xml" />
<include file="s003.xml" />
</struts>

4.配置好Struts.xml后,还要到Action类中进行依赖注入(set注入\构造注入\接口注入)代码如下:

     //声明service,但不给它创建具体的实现类的实例,
//因为:action不应该关注具体是谁来实现service
//具体service实现类是谁,我们使用spring注入进来
private IndexService is = null;
public void setIs(IndexService is) {
System.out.println("有人帮我们注入了service的实例:"+is);
this.is = is;
}

5.配置applicationContext.xml文件;

在解压后的文件夹路径下:spring-framework-2.5.6\samples\petclinic\test\org\springframework\samples\petclinic\jpa

找到applicationContext.xml文件并Copy到项目src路径下,并进行如下配置:

 <?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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">
<!-- 类似于财务部门一样,类就是钱,所有需要类的实例都由srping去管理 -->
<bean id="myIndexAction" class="ssh.action.IndexAction" scope="prototype">
<!-- setIs(myIndexService) -->
<property name="is" ref="myIndexService"/>
</bean> <!-- myIndexService = new ssh.service.IndexServiceImpl() -->
<bean id="myIndexService" class="ssh.service.IndexServiceImpl" scope="prototype">
<property name="id" ref="myIndexDao"/>
</bean> <bean id="myIndexDao" class="ssh.dao.IndexDaoImpl" scope="prototype">
<property name="c" ref="myConnection"></property>
</bean> <bean id="myConnection" class="ssh.util.MyConnectionImpl_SQLSERVER" scope="prototype">
</bean>
</beans>

在这里基本上搭建完成啦,可以去试试运行吧!