前言
本博客为连载博客,搭建框架spring上篇为
http://blog.csdn.net/zcjfzcjf/article/details/51233469
spring
1.配置spring扫描
修改src/main/resources下的spring/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:context="http://www.springframework.org/schema/context"
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-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-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/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 采用注释的方式配置bean -->
<context:annotation-config />
<!-- 配置要扫描的包 -->
<context:component-scan base-package="com.my1"></context:component-scan>
</beans>
2.增加数据库访问层代码
增加src/main/java下的dao包DemoDao.java
package com.my1.dao;
import javax.annotation.Resource;
import org.springframework.stereotype.Repository;
@Repository
public class DemoDao {
public void show()
{
System.out.println("dao");
}
}
3.增加业务层代码
增加src/main/java下的service包DemoService.java
package com.my1.service;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.my1.dao.DemoDao;
@Service
@Transactional
public class DemoService {
@Resource
DemoDao userDao;
public void show()
{
System.out.println("service");
userDao.show();
}
}
4.修改控制层代码
修改src/main/java下的controller包DemoController.java
package com.my1.controller;
import java.io.IOException;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.my1.service.DemoService;
@Controller
public class DemoController {
@Resource
DemoService demoService;
@RequestMapping(value = "/toPage1")
public ModelAndView toPage1() {
System.out.println("controller");
demoService.show();
return new ModelAndView("page1");
}
}
5.增加相关jar包
修改pom
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>mavenSpringMybatisDemo</groupId>
<artifactId>mavenSpringMybatisDemo</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>mavenSpringMybatisDemo Maven Webapp</name>
<url>http://maven.apache.org</url>
<!-- 声明版本 -->
<properties>
<spring.version>4.0.2.RELEASE</spring.version>
<junit.version>4.11</junit.version>
</properties>
<dependencies>
<!-- maven包 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-api</artifactId>
<version>2.0.4-b09</version>
</dependency>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-impl</artifactId>
<version>2.0.4-b09</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<!-- springjar包 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- spring -->
<!-- 事务 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- over -->
</dependencies>
<build>
<finalName>mavenSpringMybatisDemo</finalName>
</build>
</project>
6.启动访问
启动tomcat,访问http://localhost:8080/mavenSpringMybatisDemo/toPage1.htmls
这时控制台会打印出
说明spring的依赖注入已经完成
目录结构
7.demo下载
http://download.csdn.net/detail/zcjfzcjf/9507288
ps
上学的时候学的都是struts2,但是工作之后用到的都是springmvc,希望大家比较比较,看看两者区别哪个更好。