[JavaWeb基础] 008.Spring初步配置

框架简介:

Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson 在其著作Expert One-On-One J2EE Development and Design中阐述的部分理念和原型衍生而来。它是为了解决企业应用开发的复杂性而创建的。框架的主要优势之一就是其分层架构,分层架构允许使用者选择使用哪一个组件,同时为 J2EE 应用程序开发提供集成的框架。Spring使用基本的JavaBean来完成以前只可能由EJB完成的事情。然而,Spring的用途不仅限于服务器端的开发。从简单性、可测试性和松耦合的角度而言,任何Java应用都可以从Spring中受益。Spring的核心是控制反转IoC)和面向切面(AOP)。简单来说,Spring是一个分层的JavaSE/EEfull-stack(一站式) 轻量级开源框架。

需要的lib包:

下载spring3.2的所有包,还要包括struts2-spring-plugin-2.3.24.jar

还有3个可选配置包,如果有用到AOP切面的时候需要引入aspectjweaver.jar,cglib-nodep-2.1_3.jar,com.springsource.org.aopalliance-1.0.0.jar

配置过程:

1.首先要在src下面创建Spring的配置文件.

<?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: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.0.xsd 
 http://www.springframework.org/schema/tx 
 http://www.springframework.org/schema/tx/spring-tx-2.0.xsd 
 http://www.springframework.org/schema/aop 
 http://www.springframework.org/schema/aop/spring-aop-2.0.xsd ">

  <bean id="manageServlet" class="com.babybus.sdteam.bo.ManageServlet">  
  </bean>  
  <bean id="aopClass" class="com.babybus.sdteam.bo.AopClass">  
  </bean>  
  <!-- loginaction -->
  <bean id="LoginAction" class="com.babybus.sdteam.action.LoginAction">
     <property name="manageServlet" ref="manageServlet"></property>
  </bean>
  <!-- 切面配置 -->
  <aop:config proxy-target-class="true">
     <!-- 切面对应的类 -->
     <aop:aspect id="myAop" ref="aopClass">
       <!-- 切点配置 -->
       <aop:pointcut id="ap" expression="execution(* com.babybus.sdteam.bo.*.*(..))"/>
       <aop:before method="methodBefore" pointcut-ref="ap"/>
       <aop:after method="methodAfter" pointcut-ref="ap"/>
     </aop:aspect>
 </aop:config>
 
</beans>

2.接着要在web.xml进行spring监听配置

<!--spring配置文件位置--> 
<context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/classes/applicationContext.xml</param-value>
 </context-param>
<!--监听器--> 
 <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>

3.注意事项

当报错的时候,请确认<aop:config proxy-target-class="true">和本文开头说的3个可选包的导入,导入后对项目进行clean一遍再执行,然后项目就可以启动成功了。

到这边,我们已经讲了JAVAWEB的所有后端技术基础了,可以灵活的应对后端的工作了。

结语

  • 受益,掌握了Spring的初级应用

 

本站文章为 宝宝巴士 SD.Team 原创,转载务必在明显处注明:(作者官方网站: 宝宝巴士 

转载自【宝宝巴士SuperDo团队】 原文链接: http://www.cnblogs.com/superdo/p/4641670.html

原文地址:https://www.cnblogs.com/superdo/p/4641670.html