spring与spring mvc集成多种技术构建复杂工程.docx

上传人:b****7 文档编号:25270552 上传时间:2023-06-06 格式:DOCX 页数:20 大小:20.96KB
下载 相关 举报
spring与spring mvc集成多种技术构建复杂工程.docx_第1页
第1页 / 共20页
spring与spring mvc集成多种技术构建复杂工程.docx_第2页
第2页 / 共20页
spring与spring mvc集成多种技术构建复杂工程.docx_第3页
第3页 / 共20页
spring与spring mvc集成多种技术构建复杂工程.docx_第4页
第4页 / 共20页
spring与spring mvc集成多种技术构建复杂工程.docx_第5页
第5页 / 共20页
点击查看更多>>
下载资源
资源描述

spring与spring mvc集成多种技术构建复杂工程.docx

《spring与spring mvc集成多种技术构建复杂工程.docx》由会员分享,可在线阅读,更多相关《spring与spring mvc集成多种技术构建复杂工程.docx(20页珍藏版)》请在冰豆网上搜索。

spring与spring mvc集成多种技术构建复杂工程.docx

spring与springmvc集成多种技术构建复杂工程

spring与springmvc集成多种技术构建复杂工程

  使用spring集成其他技术,最基本的配置都是模板化的,比如配置视图模板引擎、数据库连接池、orm框架、缓存服务、邮件服务、rpc调用等,以spring的xml配置为例,我将这些配置过程整理出来,并不时更新,以备参考!

spring

  在普通的java工程中引入spring,只需要配置以下依赖

org.springframework

spring-context

3.2.17.RELEASE

  实际开发中,如果你用了log4j等日志框架,最好排除掉spring对commons-logging的依赖,写demo就不用了

commons-logging

commons-logging

  建一个spring-conf.xml,放在src/main/java目录(也就是所有包和类的根目录,即classpath),内容如下

xmlversion="1.0"encoding="UTF-8"?

>

//www.springframework.org/schema/beans"

xmlns:

xsi="http:

//www.w3.org/2001/XMLSchema-instance"

xsi:

schemaLocation="http:

//www.springframework.org/schema/beans

http:

//www.springframework.org/schema/beans/spring-beans.xsd">

  建一个Test.java类,这个类就是spring-conf.xml文件中定义的那个bean

packagecom.xmyself;

publicclassTest{

publicvoidtest(){

System.out.println("springisrunning");

}

}

  写带有main方法的主类

packagecom.xmyself;

importorg.springframework.context.ApplicationContext;

importorg.springframework.context.support.ClassPathXmlApplicationContext;

publicclassMain{

publicstaticvoidmain(String[]args){

@SuppressWarnings("resource")

ApplicationContextcontext=newClassPathXmlApplicationContext("spring-conf.xml");

Testtest=context.getBean(Test.class);

test.test();

}

}

  运行Main,就能看到“springisrunning”输出,spring配置完成

springmvc

  springmvc是配置在web工程中的,使用springmvc也只要配置一个依赖

org.springframework

spring-webmvc

3.2.17.RELEASE

  整一个Test.java类,这是个controller

packagecom.xmyself.controller;

importorg.springframework.stereotype.Controller;

importorg.springframework.web.bind.annotation.RequestMapping;

importorg.springframework.web.bind.annotation.ResponseBody;

@Controller

publicclassTest{

@RequestMapping("/test")

@ResponseBody

publicStringtest(){

return"springmvcrunning";

}

}

  在src/main/resources目录下建一个spring目录,里面放一个mvc-init.xml,内容如下

xmlversion="1.0"encoding="UTF-8"?

>

//www.springframework.org/schema/beans"

xmlns:

xsi="http:

//www.w3.org/2001/XMLSchema-instance"xmlns:

p="http:

//www.springframework.org/schema/p"

xmlns:

context="http:

//www.springframework.org/schema/context"

xsi:

schemaLocation="

http:

//www.springframework.org/schema/beans

http:

//www.springframework.org/schema/beans/spring-beans.xsd

http:

//www.springframework.org/schema/context

http:

//www.springframework.org/schema/context/spring-context.xsd">

component-scanbase-package="com.xmyself.controller"/>

  这个配置文件扫描了Test.java,最后修改web.xml来加载这个配置文件

xmlversion="1.0"encoding="UTF-8"?

>

xsi="http:

//www.w3.org/2001/XMLSchema-instance"

xmlns="xmlns:

web="

xsi:

schemaLocation="

version="2.5">

spring3mvc

spring

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath*:

/spring/mvc-init.xml

1

spring

/

  初始化DispatcherServlet如果没有指定xml文件地址,spring默认去WEB-INF目录找[servlet-name]-servlet.xml,这里就是spring-servlet.xml

  启动工程,浏览器访问http:

localhost:

8080/springmvc/test就能看到返回test值了,springmvc配置完成

freemarker模板引擎

  springmvc通常要配置模板引擎,因为jsp太复杂了,而且有损mvc模型

  freemarker是一种严格遵循mvc模型的模板引擎,现在介绍它的springmvc中的配置

  首先配置依赖

org.springframework

spring-context-support

3.2.17.RELEASE

org.freemarker

freemarker

2.3.23

  为什么要依赖spring-context-support?

因为freemarker使用的视图解析器是由spring提供的,这个解析器就在spring-context-support的jar中,下面的配置过程会让你明白,在springmvc的mvc-init.xml中配置freemarker

--freemarkerconfig-->

  配置freemarker的本质是初始化上面的两个bean,所以,你可以把这段配置写在任意的xml文件,只要springmvc能加载就行

  在WEB-INF目录建一个page目录,里面放一个freemarker.ftl文件,内容只有一句话“freemarkerpage中文乱码测试”

  最后,在控制器写一个方法

@RequestMapping("/freemarker")

publicStringfreemarker(){

return"freemarker";

}

  启动工程,访问http:

//localhost:

8080/springmvc/freemarker就能看到“freemarkerpage中文乱码测试”这句话了

thymeleaf模板引擎

  thymeleaf也是一个mvc模型的模板引擎,它的后缀是.html,所以thymeleaf模板的好处是不用渲染就能看到内容,现在spring也是推荐用thymeleaf作为springmvc的模板引擎

  配置依赖

org.thymeleaf

thymeleaf-spring3

2.1.5.RELEASE

  这是thymeleaf与spring连接用的jar,它依赖了thymeleaf的jar,对于spring3和spring4,thymeleaf的依赖是不同的,配置也稍有不同,这点要注意,下面就开始配置thymeleaf

--thymeleafconfig-->

  这段配置放在springmvc能读到的任意xml文件都可以

  在WEB-INF目录建一个page目录,里面放一个thymeleaf.html,内容只有一行“thymeleafpage中文乱码测试”

  最后,在控制器写一个方法

@RequestMapping("/thymeleaf")

publicStringthymeleaf(){

return"thymeleaf";

}

  启动工程,访问http:

//localhost:

8080/springmvc/thymeleaf就能看到“thymeleafpage中文乱码测试”这句话了

tomcatpool数据库连接池

  可能用dbcp、c3p0连接池的人多一点,其实tomcatpool更好,具体怎么好请google,下面介绍针对mysql数据库的配置过程

  配置依赖

mysql

mysql-connector-java

5.1.27

org.apache.tomcat

tomcat-jdbc

7.0.73

  tomcat-jdbc依赖了tomcat-juli,所以如果你在eclipse中为工程加入了tomcat的库,还需要单独加一个tomcat-juli的jar

  下面整一个Main.java来测试连接

packagecom.xmyself;

importjava.sql.Connection;

importjava.sql.ResultSet;

importjava.sql.Statement;

importorg.apache.tomcat.jdbc.pool.DataSource;

importorg.apache.tomcat.jdbc.pool.PoolProperties;

publicclassMain{

publicstaticvoidmain(String[]args)throwsException{

PoolPropertiesp=newPoolProperties();

p.setUrl("jdbc:

mysql:

//localhost:

3306/mysql");

p.setDriverClassName("com.mysql.jdbc.Driver");

p.setUsername("root");

p.setPassword("root");

p.setJmxEnabled(true);

p.setTestWhileIdle(false);

p.setTestOnBorrow(true);

p.setValidationQuery("SELECT1");

p.setTestOnReturn(false);

p.setValidationInterval(30000);

p.setTimeBetweenEvictionRunsMillis(30000);

p.setMaxActive(100);

p.setInitialSize(10);

p.setMaxWait(10000);

p.setRemoveAbandonedTimeout(60);

p.setMinEvictableIdleTimeMillis(30000);

p.setMinIdle(10);

p.setLogAbandoned(true);

p.setRemoveAbandoned(true);

p.setJdbcInterceptors("org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;"

+"org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer");

DataSourcedatasource=newDataSource();

datasource.setPoolProperties(p);

Connectioncon=null;

try{

con=datasource.getConnection();

Statementst=con.createStatement();

ResultSetrs=st.executeQuery("select*fromuser");

intcnt=1;

while(rs.next()){

System.out.println((cnt++)+".Host:

"+rs.getString("Host")

+"User:

"+rs.getString("User")+"Password:

"

+rs.getString("Password"));

}

rs.close();

st.close();

}finally{

if(con!

=null)

try{

con.close();

}catch(Exceptionignore){

}

}

}

}

  这个例子是tomcat官方文档给出的,运行结果如下

1.Host:

localhostUser:

rootPassword:

*81F5E21E35407D884A6CD4A731AEBFB6AF209E1B

2.Host:

127.0.0.1User:

rootPassword:

*81F5E21E35407D884A6CD4A731AEBFB6AF209E1B

3.Host:

:

:

1User:

rootPassword:

*81F5E21E35407D884A6CD4A731AEBFB6AF209E1B

4.Host:

localhostUser:

Password:

5.Host:

%User:

rootPassword:

*81F5E21E35407D884A6CD4A731AEBFB6AF209E1B

  那spring怎么集成tomcatpool呢?

配置xml让spring加载就好了

mysql:

//localhost:

3306/mysql"/>

  这里配置的属性与官方例子set方法中的属性完全相同,其余的我就不配置了

  然后写一个测试,这里以springmvc为例,写一个controller,具体代码与上例相同

@Controller

publicclassTest{

@Autowired

privateDataSourcedatasource;

@RequestMapping("/test")

@ResponseBody

publicStringtest()throwsException{

Connectioncon=datasource.getConnection();

//拿到con就可以操作了

return"springmvcrunning";

}

}

mybatis持久化框架

  mybatis轻量级,配置简单,使用方便,我们在tomcatpool基础上来配置,当然,使用tomcatpool配置只是为了得到dataSource,如果你用dbcp、c3p0等连接池,只要修改下这个dataSource的配置就好,mybatis本身的配置不用修改

  配置依赖

org.mybatis

mybatis

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 经管营销 > 人力资源管理

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1