首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

Web架构学习(四)添加spring应用

2012-06-26 
Web架构学习(4)添加spring应用印象中似乎没做过不用spring的项目,因为它在web开发中的确属于那种百利而无

Web架构学习(4)添加spring应用
印象中似乎没做过不用spring的项目,因为它在web开发中的确属于那种百利而无一害的神奇..
首先添加需要的jar包

<!-- spring--><dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-aop</artifactId></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-tx</artifactId></dependency><dependency><groupId>org.aspectj</groupId><artifactId>aspectjrt</artifactId></dependency><dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId></dependency><dependency><groupId>cglib</groupId><artifactId>cglib-nodep</artifactId></dependency>

注意不要忘了与struts整合的插件包
<dependency><groupId>org.apache.struts</groupId><artifactId>struts2-spring-plugin</artifactId></dependency>

下一步在创建在src目录下创建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:jee="http://www.springframework.org/schema/jee"xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"default-lazy-init="true" default-autowire="byName"><context:component-scan base-package="dao,service,web" /></beans>

以上我们没用配置任何bean,配置了 component表示,spring的依赖注入可通过注解的方式来执行,而且指定spring只在dao service web 三个包下进行扫描

然后我们就可以在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><!-- Spring 刷新Introspector防止内存泄露 --><listener><listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class></listener>

配置基本完成,下面是一个基本的依赖注入
首先创建 HelloDao 并标注为@Component
@Componentpublic class HelloDao {public String sayHello(String name){return "你好,"+name;}}


其次servie
@Servicepublic class HelloService {@Autowiredprivate HelloDao helloDao;public String sayHello(int i){String name = "";if(i==1){name="张三";}else{name = "李四";}return helloDao.sayHello(name);}}

对,注解就是这么简单 只要添加@Autowired就搞定了,当然action层也是一样
@Autowiredprivate HelloService helloService;@Action(value = "say")public String say() {name = helloService.sayHello(1);System.out.println(name);this.setTemplate("body", "/hello.jsp");return "onePage";}

我喜欢注解的最大原因就是在这边了,配置非常简便
注:以上三个类分别在 dao service 和web包下,也就是component-scan指定的包

热点排行