讓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/作爲前綴

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	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">

    <lang:defaults refresh-check-delay="60000" />

    <!-- 用戶服務 -->
    <lang:groovy id="userService"
      script-source="file:src/main/webapp/WEB-INF/groovy/service/UserServiceImpl.groovy">
    </lang:groovy>
</beans>

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容器下運行!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章