关于在Eclipse中spring项目类的动态引入

版权声明:RemainderTime https://blog.csdn.net/qq_39818325/article/details/82823508

在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

猜你喜欢

转载自blog.csdn.net/qq_39818325/article/details/82823508