1. 程式人生 > >關於在Eclipse中spring專案類的動態引入

關於在Eclipse中spring專案類的動態引入

在eclipse中建立spring專案,使用spring Webflux中的Router Function技術進行動態引入

如下面的程式碼進行動態引入

@Bean
	RouterFunction<ServerResponse> userRouter(UserHandler handler){
		return RouterFunctions.nest(RequestPredicates.path("/user"),
				RouterFunctions.route(RequestPredicates.GET("/"),
						handler::getAllUser));
	}

對上面的程式碼RouterFunctionsRequestPredicates類進行動態引入

  1. 在eclipse上工作列選擇Widow--->Preferences

sa

 

 

 

 

 

 

  2.進入後選擇 Java -->Editor-->Content Assist-->Favorties 過後選擇New Type

aaa

 

 

 

 

 

 

 

 

 

 

 

 

3.進入 選擇Browes

4.彈出框 輸入你要加入的類名

aa

 

 

 

 

 

5.點選加入 並點選ok退出

 

 

 

 

6.在專案中刪除相關的類名,這時會報錯,如圖

 

 

 

 

 

 

6.點選外面的紅叉 並選擇

 

 

 

 

 

依次在報錯的地方引入

 

最後前後兩次的程式碼對比:

package com.xf.routers;

import static org.springframework.web.reactive.function.server.RequestPredicates.DELETE;
import static org.springframework.web.reactive.function.server.RequestPredicates.GET;
import static org.springframework.web.reactive.function.server.RequestPredicates.POST;
import static org.springframework.web.reactive.function.server.RequestPredicates.accept;
import static org.springframework.web.reactive.function.server.RequestPredicates.path;
import static org.springframework.web.reactive.function.server.RouterFunctions.nest;
import static org.springframework.web.reactive.function.server.RouterFunctions.route;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.ServerResponse;

import com.xf.handler.UserHandler;

@Configuration
public class AllRouters {
	
	/*@Bean
	RouterFunction<ServerResponse> userRouter(UserHandler handler){
		return RouterFunctions.nest(RequestPredicates.path("/user"),
				RouterFunctions.route(RequestPredicates.GET("/"),
						handler::getAllUser));
	}*/

	
	//使用動態引入
	@Bean
	RouterFunction<ServerResponse> userRouter(UserHandler handler){
		return nest
				//相當於類上面的@RequestMapping("/user")
				(path("/user"),
				//下面的相當於類裡面的@RequestMapping
				//得到所有使用者
				route(GET("/"),handler::getAllUser)
				//建立使用者
				.andRoute(POST("/").and(accept(MediaType.APPLICATION_JSON_UTF8)),
						handler::createUser)
				//刪除使用者
				.andRoute(DELETE("/{id}"), handler::deleteUserById));		
	}
}

注:這裡使用的Spring WebFlux 不是使用Sring MVC

      Spring WebFlux詳細解析見:https://docs.spring.io/spring-framework/docs/5.0.0.BUILD-SNAPSHOT/spring-                 framework-reference/html/web-reactive.html