Spring Boot开发之弄月吟风 (三)

今天来看下,如何在Spring Boot中使用热加载和集成Velocity,废话不多说,直接看下面的maven的pom依赖: 

<?xml version="1.0" encoding="UTF-8"?>  
<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/xsd/maven-4.0.0.xsd  
http://www.w3.org/2001/XMLSchema-instance " >  
    <modelVersion>4.0.0</modelVersion>  
    <groupId>serachrate</groupId>  
    <artifactId>serachrate</artifactId>  
    <version>1.0-SNAPSHOT</version>  
  
  
    <!--父依赖-->  
    <parent>  
        <groupId>org.springframework.boot</groupId>  
        <artifactId>spring-boot-starter-parent</artifactId>  
        <version>1.2.2.RELEASE</version>  
    </parent>  
  
  
    <!--指定编码-->  
    <properties>  
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
    </properties>  
  
    <build>  
        <pluginManagement>  
            <plugins>  
                <plugin>  
                    <groupId>org.springframework.boot</groupId>  
                    <artifactId>spring-boot-maven-plugin</artifactId>  
                </plugin>  
            </plugins>  
        </pluginManagement>  
  
        <plugins>  
            <plugin>  
                <!--  
                比较常用的打包命令  
                mvn clean package spring-boot:repackage  
                只有使用下面的maven命令在cmd窗口启动,  
                热加载才能生效,如果直接Run运行的Application是  
                不支持热加载的  
                mvn clean spring-boot:run  
                -->  
                <groupId>org.springframework.boot</groupId>  
                <artifactId>spring-boot-maven-plugin</artifactId>  
                <version>1.2.2.RELEASE</version>  
                <dependencies>  
                    <!--引入springloaded来实现动态加载-->  
                    <dependency>  
                        <groupId>org.springframework</groupId>  
                        <artifactId>springloaded</artifactId>  
                        <version>1.2.0.RELEASE</version>  
                    </dependency>  
                </dependencies>  
                <executions>  
                    <execution>  
                        <goals>  
                            <goal>repackage</goal>  
                        </goals>  
                        <configuration>  
                            <classifier>exec</classifier>  
                        </configuration>  
                    </execution>  
                </executions>  
                <configuration>  
                    <!--  <mainClass>com.dhgate.rate.ApplicationSearch</mainClass> -->  
                    <!--指定监听的端口号-->  
                    <jvmArguments>  
                        -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=7777  
                    </jvmArguments>  
                </configuration>  
            </plugin>  
        </plugins>  
    </build>  
  
  
    <dependencies>  
        <!--web容器支持-->  
        <dependency>  
            <groupId>org.springframework.boot</groupId>  
            <artifactId>spring-boot-starter-web</artifactId>  
            <!--本例中散仙排除了tomcat,而使用jetty作为容器-->  
            <exclusions>  
                <exclusion>  
                    <groupId>org.springframework.boot</groupId>  
                    <artifactId>spring-boot-starter-tomcat</artifactId>  
                </exclusion>  
            </exclusions>  
        </dependency>  
        <dependency>  
            <groupId>org.springframework.boot</groupId>  
            <artifactId>spring-boot-starter-jetty</artifactId>  
  
        </dependency>  
  
  
        <!-- 安全验证  
        <dependency>  
            <groupId>org.springframework.boot</groupId>  
            <artifactId>spring-boot-starter-security</artifactId>  
        </dependency>  
       -->  
        <!--Velocity引擎的支持,如果你需要其他的模板引擎,则-->  
        <!--可以直接引入其他的即可-->  
        <dependency>  
            <groupId>org.springframework.boot</groupId>  
            <artifactId>spring-boot-starter-velocity</artifactId>  
        </dependency>  
  
        <!--性能指标和监测-->  
        <!--<dependency>-->  
        <!--<groupId>org.springframework.boot</groupId>-->  
        <!--<artifactId>spring-boot-starter-actuator</artifactId>-->  
        <!--</dependency>-->  
        <!--<dependency>-->  
        <!--<groupId>org.springframework.boot</groupId>-->  
        <!--<artifactId>spring-boot-starter-remote-shell</artifactId>-->  
        <!--</dependency>-->  
        <!--<dependency>-->  
        <!--<groupId>org.springframework.boot</groupId>-->  
        <!--<artifactId>spring-boot-starter-security</artifactId>-->  
        <!--</dependency>-->  
  
  
        <!--Guava包的依赖支持-->  
        <dependency>  
            <groupId>com.google.guava</groupId>  
            <artifactId>guava</artifactId>  
            <version>16.0.1</version>  
        </dependency>  
  
        <!--Gson支持-->  
        <dependency>  
            <groupId>com.google.code.gson</groupId>  
            <artifactId>gson</artifactId>  
            <version>2.3.1</version>  
        </dependency>  
  
        <!--Jsoup支持-->  
        <dependency>  
            <groupId>org.jsoup</groupId>  
            <artifactId>jsoup</artifactId>  
            <version>1.8.1</version>  
        </dependency>  
        <!--solrj的依赖包 -->  
        <dependency>  
            <groupId>org.apache.solr</groupId>  
            <artifactId>solr-solrj</artifactId>  
            <version>4.3.1</version>  
        </dependency>  
  
    </dependencies>  
  
  
</project>  

上面已经引入了Spring Loaded框架以及Velocity模板引擎,所以项目就会支持这两种技术,需要注意的是在pom里面关于Sprig Loaded的配置,否则将不会生效,另外建议把IDEA的自动编译保存功能打开,这样一来,Velocity的模板也会自动刷新。 

在Spring Boot中不管集成任何东西,都是这么简单的事情,只需稍微变更pom依赖,即可完成,非常便利。 


关于Velocity的标签的使用,散仙抽空会在补充一下用法,整体感觉,比Struts和el表达式标签或者jsp里面的标签,简单得多,而且支持宏定义,用起来非常给力。 

在Spring Boot中,我们只需要在resource文件夹下,新建一个tmplates的文件夹来存放vm模板即可,然后我们就可以在后台的控制器里面,控制返回到那个vm了,而且数据也是从后台传过来的。 

一个简单的例子: 

@RequestMapping("/hello")  
public String hello(Map<String, Object> model){  
    List<String> l= Lists.newArrayList();  
    l.add("哈喽,hadoop");  
    l.add("哈喽,hbase");  
    l.add("哈喽,hive");  
    l.add("哈喽,pig");  
    l.add("哈喽,zookeeper");  
    l.add("哈喽,三劫散仙");  
    //将数据存放map里面,可以直接在velocity页面,使用key访问  
    model.put("data",l);  
  
    return "hello";  
}  

模板部分: 

 
velocity代码: 

#foreach($s in $data)  
    <h1 style="color: crimson">访问数据:$s</h1>  
#end  

结果如下: 



关于动态加载和编译的部分,大家可以自行在自己的环境中测试。 
关于spring boot打包和maven启动的两个命令如下: 

//打包编译该spring boot项目  
   mvn clean package spring-boot:repackage  
//以maven方式启动该项目,spring loaded必须使用此启动方法,才能生效  
   mvn clean spring-boot:run  

 

 

 

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值