欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

详解Spring MVC4 纯注解配置教程

程序员文章站 2024-02-27 20:09:03
阅读本文需要又一定的sping基础,最起码要成功的运行过一个springmvc项目。 在传统的spring项目中,我们要写一堆的xml文件。而这些xml文件格式要求又很严...

阅读本文需要又一定的sping基础,最起码要成功的运行过一个springmvc项目。

在传统的spring项目中,我们要写一堆的xml文件。而这些xml文件格式要求又很严格,很不便于开发。而网上所谓的0配置,并不是纯粹的0配置,还是要写一些xml配置,只是用了几个@service,@controller注解而已。

在这里,我讲介绍一种新的配置方式,一行xml代码都不需要,什么web.xml,application-context.xml,beans.xml,统统去死吧!

首先建立一个maven项目,packageing方式为war,项目结构为标准maven webapp结构。

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>com.csonezp</groupid>
  <artifactid>springdemo</artifactid>
  <packaging>war</packaging>
  <version>1.0-snapshot</version>
  <name>dataplatform maven webapp</name>
  <url>http://maven.apache.org</url>
  <properties>
    <spring.version>4.0.1.release</spring.version>
  </properties>
  <dependencies>
    <dependency>
      <groupid>junit</groupid>
      <artifactid>junit</artifactid>
      <version>4.11</version>
    </dependency>

    <!-- spring dependencies -->
    <dependency>
      <groupid>asm</groupid>
      <artifactid>asm-commons</artifactid>
      <version>2.2.3</version>
    </dependency>
    <dependency>
      <groupid>asm</groupid>
      <artifactid>asm</artifactid>
      <version>2.2.3</version>
    </dependency>
    <dependency>
      <groupid>org.springframework</groupid>
      <artifactid>spring-core</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-orm</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-context</artifactid>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupid>org.springframework</groupid>
      <artifactid>spring-aop</artifactid>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupid>org.springframework</groupid>
      <artifactid>spring-expression</artifactid>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupid>org.springframework</groupid>
      <artifactid>spring-test</artifactid>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupid>org.springframework</groupid>
      <artifactid>spring-tx</artifactid>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupid>org.springframework</groupid>
      <artifactid>spring-webmvc</artifactid>
      <version>${spring.version}</version>
    </dependency>


    <dependency>
      <groupid>cglib</groupid>
      <artifactid>cglib</artifactid>
      <version>2.2.2</version>
    </dependency>
    <!-- servlet api, jstl -->
    <dependency>
      <groupid>javax.servlet</groupid>
      <artifactid>javax.servlet-api</artifactid>
      <version>3.0.1</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupid>jstl</groupid>
      <artifactid>jstl</artifactid>
      <version>1.2</version>
    </dependency>

    <dependency>
      <groupid>org.codehaus.jackson</groupid>
      <artifactid>jackson-core-asl</artifactid>
      <version>1.8.4</version>
    </dependency>

    <dependency>
      <groupid>org.codehaus.jackson</groupid>
      <artifactid>jackson-mapper-asl</artifactid>
      <version>1.8.4</version>
    </dependency>

    <dependency>
      <groupid>c3p0</groupid>
      <artifactid>c3p0</artifactid>
      <version>0.9.1.2</version>
    </dependency>


    <dependency>
      <groupid>log4j</groupid>
      <artifactid>log4j</artifactid>
      <version>1.2.8</version>
    </dependency>


    <dependency>
      <groupid>org.json</groupid>
      <artifactid>json</artifactid>
      <version>20090211</version>
    </dependency>


    <dependency>
      <groupid>mysql</groupid>
      <artifactid>mysql-connector-java</artifactid>
      <version>5.1.6</version>
    </dependency>

    <dependency>
      <groupid>spy</groupid>
      <artifactid>spymemcached</artifactid>
      <version>2.6</version>
    </dependency>

    <dependency>
      <groupid>org.slf4j</groupid>
      <artifactid>slf4j-api</artifactid>
      <version>1.6.6</version>
    </dependency>
  </dependencies>
  <build>
    <finalname>dataplatform</finalname>
  </build>
</project>

这个时候,就该进行spring配置了。按传统方式来的话,首先要去web.xml写一堆配置,然后建立个管理beab的beans.xml,管理spring mvc 的xml,再写一坨一坨bean。就是先进一点的(也就是很多人说的0配置),也就是自己的业务bean不用写进xml了,还是很麻烦。

而我这里讲的方式,则是完全不修改任何web.xml代码,不写一行xml代码的方式。

 首先,在项目立建立一个config.java文件:

/**
 * created by zhangpeng on 16-3-22.
 * 取代beans.xml,纯注解配置各种bean
 */
@configuration
@componentscan("com.csonezp")
@enablewebmvc
public class config {
  /**
   * jsp视图解析器的bean
   * @return
   */
  @bean
  public urlbasedviewresolver setupviewresolver() {
    urlbasedviewresolver resolver = new urlbasedviewresolver();
    resolver.setprefix("/web-inf/");
    resolver.setsuffix(".jsp");
    resolver.setviewclass(jstlview.class);
    return resolver;
  }

  /**
   * 配置数据源
   * @return
   */
  @bean(name = "datasource")
  public combopooleddatasource getdatasource() {
    try {

      combopooleddatasource datasource = new combopooleddatasource();
      datasource.setjdbcurl("jdbc:mysql://localhost:3306/mfdb");
      datasource.setdriverclass("com.mysql.jdbc.driver");
      datasource.setuser("root");
      datasource.setpassword("zp1228");
      datasource.setmaxpoolsize(75);
      return datasource;
    } catch (exception e) {
      return null;
    }
  }
}

@configuration注解就是告诉spring这个是一个配置文件类,这里配置的bean要交给spring去管理。这个就是用来取代beans.xml这种文件的。

@componentscan("com.csonezp")这个注解就是配置包扫描用的,不必多说了

@enablewebmvc ,启用spring mvc支持

这里面配置了两个bean,第一个就是jsp的视图解析器,第二个则是配置了一个数据源。这些都可以对应到xml文件里面去的。找一个传统spring项目,用xml文件对比着我这个类去看,会对这种配置方式有更深的了解。

下面要建立一个webinitializer类继承webapplicationinitializer,在这里添加一个servlet。这一步是用来取代在web.xml中添加servlet的步骤

import org.springframework.web.webapplicationinitializer;
import org.springframework.web.context.support.annotationconfigwebapplicationcontext;
import org.springframework.web.servlet.dispatcherservlet;

import javax.servlet.servletcontext;
import javax.servlet.servletexception;
import javax.servlet.servletregistration.dynamic;

public class webinitializer implements webapplicationinitializer {

  public void onstartup(servletcontext servletcontext) throws servletexception {
     
    annotationconfigwebapplicationcontext ctx = new annotationconfigwebapplicationcontext();
    ctx.register(config.class); 
    ctx.setservletcontext(servletcontext);  
    
    dynamic servlet = servletcontext.addservlet("dispatcher", new dispatcherservlet(ctx));
    servlet.addmapping("/"); 
    servlet.setloadonstartup(1); 
     
  } 

}

这样就ok啦!让我们写一些代码来测试一下吧! 

import org.springframework.stereotype.service;

/**
 * created by zhangpeng on 16-3-22.
 */
@service
public class helloservice {
  public string gethello(string name) {
    return name + ",hello!";
  }
}

import com.guduo.dataplatform.service.helloservice;
import com.guduo.dataplatform.service.movieservice;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.stereotype.controller;
import org.springframework.ui.modelmap;
import org.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.bind.annotation.requestparam;

/**
 * created by zhangpeng on 16-3-22.
 */
@controller
@requestmapping("/test")
public class testcontroller {

  @autowired
  helloservice helloservice;
  @autowired
  movieservice movieservice;


  @requestmapping("/hello")
  public string sayhello(@requestparam("name") string name, modelmap model) {
    model.put("hello", helloservice.gethello(name));
    return "hello";
  }
}

一个service,一个controller。

启动项目,在浏览器中输入地址http://localhost:8080/dp/test/hello?name=sss

页面显示sss,hello!

完工!

额,忘了演示confgig里配置的bean如何使用了。其实和xml里的一样。这里拿一个dao做演示吧,这里就注入了我们在config里配置的那个数据源

@repository
public class movieplayincdao {
  private datasource datasource;

  private jdbctemplate jdbctemplateobject;

  @autowired
  public void setdatasource(datasource datasource) {
    this.datasource = datasource;
    this.jdbctemplateobject = new jdbctemplate(datasource);
  }

  public list<movieplayincreament> getmovieplayinc(int movieid) {
    try {
      string sql = "select * from movieplayincreament where movieid=?";
      list<movieplayincreament> list = jdbctemplateobject.query(sql, new movieplayincmapper(), new object[]{movieid});
      return list;
    } catch (exception e) {
      return null;
    }
  }


}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。