boot中groovy替代java_让Spring Boot完美融合Groovy作为可热替换的Bean实现步骤

1. 修改build.gradle文件

用apply plugin: 'groovy'替换掉apply plugin: 'java'

2. 编写java接口类

在src\main\java\目录下

例如:src\main\java\wjw\test\springboot\service\IUserService.java

package wjw.test.springboot.service;

public interface IUserService {

public String getUser();

}

3. 编写Groovy实现类

在src\main\webapp\WEB-INF\groovy\目录下

例如: src\main\webapp\WEB-INF\groovy\service\UserServiceImpl.groovy

package groovy.service

import wjw.test.springboot.service.IUserService

class UserServiceImpl implements IUserService {

public String getUser() {

return "你好,世界,Groovy Spring Boot!"

}

}

4. 编写Spring Bean配置文件

注意:需要采用file:src/main/webapp/作为前缀

xmlns="http://www.springframework.org/schema/beans" xmlns:lang="http://www.springframework.org/schema/lang"

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.2.xsd

http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.2.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd

http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd

http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">

script-source="file:src/main/webapp/WEB-INF/groovy/service/UserServiceImpl.groovy">

5. 编写GroovyServletEx类

package groovy.servlet;

import java.io.IOException;

import javax.servlet.annotation.WebInitParam;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

@SuppressWarnings("serial")

@WebServlet(urlPatterns = "*.gdo", loadOnStartup = 2, initParams = { @WebInitParam(name = "verbose", value = "false"),

@WebInitParam(name = "logGROOVY861", value = "true"),

@WebInitParam(name = "resource.name.regex", value = "gdo$"),

@WebInitParam(name = "resource.name.replacement", value = "groovy") })

public class GroovyServletEx extends GroovyServlet {

@Override

public void service(HttpServletRequest request, HttpServletResponse response) throws IOException {

request.setCharacterEncoding(this.encoding); // @wjw_note:

// 解决form提交数据的编码问题!

super.service(request, response);

}

}

6. 修改Spring Boot启动文件,引入GroovyServletEx类

添加@ServletComponentScan(basePackages=["groovy.servlet"])

7. 修改Spring Boot启动文件,引入xml配置文件

添加@ImportResource(locations=["file:src/main/webapp/WEB-INF/service-manage.xml"])

注意:需要采用file:src/main/webapp/作为前缀

8. 用gradle来启动应用

gradlew.bat bootRun -PappArgs="--spring.profiles.active=dev"

注意:不能采用WAR的方式在其他web容器下运行!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值