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>
<dependency><groupId>org.apache.struts</groupId><artifactId>struts2-spring-plugin</artifactId></dependency>
<?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>
<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>
@Componentpublic class HelloDao {public String sayHello(String name){return "你好,"+name;}}@Servicepublic class HelloService {@Autowiredprivate HelloDao helloDao;public String sayHello(int i){String name = "";if(i==1){name="张三";}else{name = "李四";}return helloDao.sayHello(name);}}@Autowiredprivate HelloService helloService;@Action(value = "say")public String say() {name = helloService.sayHello(1);System.out.println(name);this.setTemplate("body", "/hello.jsp");return "onePage";}