先来看下Spring 的一些优点吧:
<!-- 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>
<!-- Struts 2 配置 -->
<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>*.action</url-pattern>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
<result
name="success">/index.jsp</result>
</action>
◆J2EE应该更加容易使用。
◆面向对象的设计比任何实现技术(比如J2EE)都重要。
◆面向接口编程,而不是针对类编程。Spring将使用接口的复杂度降低到零。(面向接口编程有哪些复杂度?)
◆代码应该易于测试。Spring框架会帮助你,使代码的测试更加简单。
◆JavaBean提供了应用程序配置的最好方法。
◆在Java中,已检查异常(Checked exception)被过度使用。框架不应该迫使你捕获不能恢复的异常
先搞个简单的整合示例,以此来剥析Spring带来的巨大好处:
1.先新建个Web项目
2.找到几个应用到了的jar包,如果你个MyEclipse的话,可以通过项目右键MyEclipse选项获得相关支持jar包
好吧,我直接上图:
将struts.xml文件和applicationContext.xml文件添加到项目的src目录下。
接下来就是向Web应用程序进行注册:在WEB-INF目录的web.xml中添加如下信息:
-->
<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>
<!-- Struts 2 配置 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteF
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
配置web.xml文件之后,我们来写几个接口来玩玩:
DAO层接口:
public interface
IStuffDao {
IStuffDao {
public abstract void
add();
add();
}
DAO层接口实现类(采用注解方式):
@Component("stuffDao")
public class
StuffDaoImpl implements IStuffDao {
StuffDaoImpl implements IStuffDao {
public void
add(){
add(){
System.out.println("我是 StuffDAO");
}
}
BIZ层接口:
public interface
IStuffBiz {
IStuffBiz {
public abstract void
add();
add();
}
BIZ层接口实现类(采用注解方式实现--Spring的依赖注入):
@Component("stuffBIZ")
public class
StuffBizImpl implements IStuffBiz {
StuffBizImpl implements IStuffBiz {
private IStuffDao
stuffDao;
stuffDao;
public IStuffDao
getStuffDao() {
getStuffDao() {
return
stuffDao;
stuffDao;
}
@Resource(name="stuffDao")
public void
setStuffDao(IStuffDao stuffDao) {
setStuffDao(IStuffDao stuffDao) {
this.stuffDao =
stuffDao;
stuffDao;
}
public void add()
{
{
stuffDao.add();
}
}
好吧,我得在写个Action,来实现整合(也是注解):
@Service
public class
UserAction {//spring控制反转,默认ID为userAction
UserAction {//spring控制反转,默认ID为userAction
private IStuffBiz
stuffBiz;
stuffBiz;
public IStuffBiz
getStuffBiz() {
getStuffBiz() {
return
stuffBiz;
stuffBiz;
}
@Resource
public void
setStuffBiz(IStuffBiz stuffBiz) {
setStuffBiz(IStuffBiz stuffBiz) {
this.stuffBiz =
stuffBiz;
stuffBiz;
}
public String
add(){
add(){
stuffBiz.add();
return
"success";
"success";
}
}
来来来,我们配置下struts.xml文件:
<package name="system" namespace="/"
extends="struts-default">
extends="struts-default">
<action name="user" class="userAction"
method="add">
method="add">
name="success">/index.jsp</result>
</package>
配置下applicationContext.xml文件:
<?xml
version="1.0" encoding="UTF-8"?>
version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd"
>
>
<!--
配置Spring上下文的注解 扫描包的注解-->
配置Spring上下文的注解 扫描包的注解-->
<context:component-scan
base-package="com.lovesmile.oa" />
base-package="com.lovesmile.oa" />
</beans>
接下来就是测试了,你会了吗...