1.前言
習慣了spring注解風格,方便好用,現在用vert.x框架,怎么使用spring注解呢?
2.maven安裝依賴包
<!--spring注解依賴包--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.1.5.RELEASE</version> </dependency>
3.注冊bean有兩個方法:xml注冊,注解注冊
方法1:xml注冊
新建一個xml配置文件

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 默認單例,,加上scope="prototype"為多例--> <bean name="eatService" class="xue.myVertX.service.serviceImpl.EatServiceImpl" scope="prototype"/> </beans>
接口和他的實現類在springMVC里該怎么寫還是怎么寫,不變,這里舊省略了
使用方法:

package xue.myVertX; import io.vertx.core.AbstractVerticle; import io.vertx.core.Vertx; import io.vertx.core.http.HttpMethod; import io.vertx.core.http.HttpServer; import io.vertx.ext.web.Router; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import xue.myVertX.likeController.IndexHandle; import xue.myVertX.service.serviceImpl.EatServiceImpl; /** * 簡單的路由使用 */ public class SimpleRouter extends AbstractVerticle { @Override public void start() throws Exception { //讀取bean配置文件,注冊所有bean,獲取上下文對象 //方法1: //xml文件手動注冊bean方法 ApplicationContext context = new ClassPathXmlApplicationContext("spring-context.xml"); // 創建HttpServer HttpServer server = vertx.createHttpServer(); // 創建路由對象 Router router = Router.router(vertx); // 監聽/index地址 ,就像spring注解@RequestMapping注冊虛擬路徑,然后調用controller方法 router.route("/index").handler(new IndexHandle(context)); // 把請求交給路由處理--------------------(1) //舊版寫法 server.requestHandler(router::accept); //新版寫法,需要版本在4以上才可以 // server.requestHandler(router); server.listen(8080); } public static void main(String[] args) { Vertx.vertx().deployVerticle(new SimpleRouter()); } }

package xue.myVertX.likeController; import io.vertx.core.Handler; import io.vertx.ext.web.RoutingContext; import org.springframework.context.ApplicationContext; import xue.myVertX.service.EatService; import xue.myVertX.service.serviceImpl.EatServiceImpl; /** * 這個類就像spring注解controller, */ public class IndexHandle implements Handler<RoutingContext> { private EatService eatService; public IndexHandle(final ApplicationContext context) { //讀取全局bean配置文件,實例bean對象 //方法1,配合xml手動注冊,可不寫注解 eatService = (EatService) context.getBean("eatService"); } @Override public void handle(RoutingContext routingContext) { // //獲取參數,其實就是類似於request // String user = routingContext.request().getParam("user"); // String pass = routingContext.request().getParam("pass"); // routingContext.response() // .putHeader("Content-type", "text/html;charset=utf-8") // .end("接收到的用戶名為:" + user + " 接收到的密碼為:" + pass); // // String str =eatService.eatApply(); System.out.println("可以吃啥?"+str); routingContext.response().putHeader("Content-type", "text/html;charset=utf-8").end(str); } }
方法2:注解注冊
在實現類添加注解
使用方法

package xue.myVertX; import io.vertx.core.AbstractVerticle; import io.vertx.core.Vertx; import io.vertx.core.http.HttpMethod; import io.vertx.core.http.HttpServer; import io.vertx.ext.web.Router; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import xue.myVertX.likeController.IndexHandle; import xue.myVertX.service.serviceImpl.EatServiceImpl; /** * 簡單的路由使用 */ public class SimpleRouter extends AbstractVerticle { @Override public void start() throws Exception { //讀取bean配置文件,注冊所有bean,獲取上下文對象 //方法1: //xml文件手動注冊bean方法 // ApplicationContext context = new ClassPathXmlApplicationContext("spring-context.xml"); // //方法2: //使用注解自動注冊 ApplicationContext context = new AnnotationConfigApplicationContext(EatServiceImpl.class); // 創建HttpServer HttpServer server = vertx.createHttpServer(); // 創建路由對象 Router router = Router.router(vertx); // 監聽/index地址 ,就像spring注解@RequestMapping注冊虛擬路徑,然后調用controller方法 router.route("/index").handler(new IndexHandle(context)); // 把請求交給路由處理--------------------(1) //舊版寫法 server.requestHandler(router::accept); //新版寫法,需要版本在4以上才可以 // server.requestHandler(router); server.listen(8080); } public static void main(String[] args) { Vertx.vertx().deployVerticle(new SimpleRouter()); } }

package xue.myVertX.likeController; import io.vertx.core.Handler; import io.vertx.ext.web.RoutingContext; import org.springframework.context.ApplicationContext; import xue.myVertX.service.EatService; import xue.myVertX.service.serviceImpl.EatServiceImpl; /** * 這個類就像spring注解controller, */ public class IndexHandle implements Handler<RoutingContext> { private EatService eatService; public IndexHandle(final ApplicationContext context) { //讀取全局bean配置文件,實例bean對象 //方法1,配合xml手動注冊,可不寫注解 // eatService = (EatService) context.getBean("eatService"); //方法2,使用注解,需要在實現類加@Service才可以被調用,參數為實現類的名字 eatService = context.getBean(EatServiceImpl.class); } @Override public void handle(RoutingContext routingContext) { // //獲取參數,其實就是類似於request // String user = routingContext.request().getParam("user"); // String pass = routingContext.request().getParam("pass"); // routingContext.response() // .putHeader("Content-type", "text/html;charset=utf-8") // .end("接收到的用戶名為:" + user + " 接收到的密碼為:" + pass); // // String str =eatService.eatApply(); System.out.println("可以吃啥?"+str); routingContext.response().putHeader("Content-type", "text/html;charset=utf-8").end(str); } }
3.測試結果
網頁
控制台打印