我正在使用 Jersey 2.10 和 jersey-spring3 和 Spring 4。
我想在球衣资源以及其他地方实现 DI(基本服务),并希望通过 Java 配置创建 Spring Bean。
目前,我无法找到任何方法来做到这一点。
知道怎么做吗?
我的 web.xml 看起来像这样
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | <display-name>Restful Web Application</display-name> <servlet> <servlet-name>jersey-serlvet</servlet-name> <servlet-class> org.glassfish.jersey.servlet.ServletContainer </servlet-class> <init-param> <param-name> jersey.config.server.provider.packages </param-name> <param-value>com.xyz</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/application-context.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet-mapping> <servlet-name>jersey-serlvet</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> </web-app> |
网络应用程序:
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | <param-name>contextClass</param-name> <param-value> org.springframework.web.context.support.AnnotationConfigWebApplicationContext </param-value> </context-param> <context-param> <param-name>contextConfigLocation</param-name> <param-value>xxx.xxx.configuration.ApplicationConfiguration</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>SpringApplication</servlet-name> <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> <init-param> <param-name>jersey.config.server.provider.classnames</param-name> <param-value>xxx.xxx.controllers.HelloController</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>SpringApplication</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> |
基于 Java 的配置:
2 3 4 5 6 7 | public class ApplicationConfiguration { @Bean HelloService helloService () { return new HelloServiceImpl(); } } |
和简单的控制器:
2 3 4 5 6 7 8 9 10 11 12 13 14 15 | @Path("/helloController") public class HelloController { @Autowired @Qualifier("helloService") private HelloService helloService ; @GET @Path("/hello") public String hello() { helloService.service(); } } |
用于测试:
localhost:8080/[AppName]/helloController/hello
记住要排除旧的 Spring 依赖项,否则可能会产生一些冲突。您可以按照下面的示例或通过 DependencyManagement 执行此操作。
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | <!-- Jersey --> <dependency> <groupId>org.glassfish.jersey.ext</groupId> jersey-spring3</artifactId> <version>2.11</version> <exclusions> <exclusion> spring-context</artifactId> <groupId>org.springframework</groupId> </exclusion> <exclusion> spring-beans</artifactId> <groupId>org.springframework</groupId> </exclusion> <exclusion> spring-core</artifactId> <groupId>org.springframework</groupId> </exclusion> <exclusion> spring-web</artifactId> <groupId>org.springframework</groupId> </exclusion> <exclusion> jersey-server</artifactId> <groupId>org.glassfish.jersey.core</groupId> </exclusion> <exclusion> jersey-container-servlet-core </artifactId> <groupId>org.glassfish.jersey.containers</groupId> </exclusion> <exclusion> hk2</artifactId> <groupId>org.glassfish.hk2</groupId> </exclusion> </exclusions> </dependency> <!-- Spring 4 dependencies --> <dependency> <groupId>org.springframework</groupId> spring-core</artifactId> <version>4.0.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> spring-context</artifactId> <version>4.0.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> spring-beans</artifactId> <version>4.0.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> spring-web</artifactId> <version>4.0.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> spring-aspects</artifactId> <version>4.0.6.RELEASE</version> </dependency> </dependencies> |
由于您已经初始化了
2 | SomeBean someBean = (SomeBean) ctx.getBean("someBean"); |
泽西支持:
或者你可以使用基于注解的发现,因为 Jersey 已经支持 Spring DI。您必须在主应用程序入口点下注册您的 bean。在下面的示例中,该入口点将是
2 3 4 5 6 7 8 9 | <servlet-name>SpringApplication</servlet-name> <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> <init-param> <param-name>javax.ws.rs.Application</param-name> <param-value>some.package.MyApplication</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> |
在你的应用程序中注册你的bean:
2 3 4 5 6 7 8 9 10 11 12 | import org.glassfish.jersey.server.ResourceConfig; import org.glassfish.jersey.server.spring.scope.RequestContextFilter; public class MyApplication extends ResourceConfig { public MyApplication () { register(RequestContextFilter.class); register(SomeBean.class); // ... } } |
在这里,您可以查看 Jersey Git repo 中的准备运行示例。
对于那些尝试使用 Java 配置的人:
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | HttpServer server = new HttpServer(); NetworkListener listener = new NetworkListener("grizzly2","localhost", 2088); server.addListener(listener); WebappContext ctx = new WebappContext("ctx","/"); final ServletRegistration reg = ctx.addServlet("spring", new SpringServlet()); reg.addMapping("/*"); ctx.addContextInitParameter("contextClass","org.springframework.web.context.support.AnnotationConfigWebApplicationContext" ); ctx.addContextInitParameter("contextConfigLocation","com.example.AppConfig" ); ctx.addListener("org.springframework.web.context.ContextLoaderListener" ); ctx.addListener("org.springframework.web.context.request.RequestContextListener"); ctx.deploy(server); server.start(); System.in.read(); } |
这是我从各种教程中发现的。结合其他答案,您应该有一个完整的示例。
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | import javax.servlet.ServletException; import javax.servlet.ServletRegistration; import com.sun.jersey.spi.spring.container.servlet.SpringServlet; import org.springframework.web.WebApplicationInitializer; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; public class WebInitializer implements WebApplicationInitializer { @Override public void onStartup(ServletContext servletContext) throws ServletException { AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext(); ctx.register(AppConfig.class); ctx.setServletContext(servletContext); servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, ctx); ServletRegistration.Dynamic servlet = servletContext.addServlet("jersey-serlvet", new SpringServlet()); servlet.addMapping("/"); servlet.setLoadOnStartup(1); } } |
我有一个在Linux服务器上运行的ruby脚本。它不使用rails或任何东西。它基本上是一个命令行ruby脚本,可以像这样传递参数:./ruby_script.rbarg1arg2如何将参数抽象到配置文件(例如yaml文件或其他文件)中?您能否举例说明如何做到这一点?提前谢谢你。 最佳答案 首先,您可以运行一个写入YAML配置文件的独立脚本:require"yaml"File.write("path_to_yaml_file",[arg1,arg2].to_yaml)然后,在您的应用中阅读它:require"yaml"arg
我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/
我正在使用Ruby2.1.1和Rails4.1.0.rc1。当执行railsc时,它被锁定了。使用Ctrl-C停止,我得到以下错误日志:~/.rvm/gems/ruby-2.1.1/gems/spring-1.1.2/lib/spring/client/run.rb:47:in`gets':Interruptfrom~/.rvm/gems/ruby-2.1.1/gems/spring-1.1.2/lib/spring/client/run.rb:47:in`verify_server_version'from~/.rvm/gems/ruby-2.1.1/gems/spring-1.1.
我已经在Sinatra上创建了应用程序,它代表了一个简单的API。我想在生产和开发上进行部署。我想在部署时选择,是开发还是生产,一些方法的逻辑应该改变,这取决于部署类型。是否有任何想法,如何完成以及解决此问题的一些示例。例子:我有代码get'/api/test'doreturn"Itisdev"end但是在部署到生产环境之后我想在运行/api/test之后看到ItisPROD如何实现? 最佳答案 根据SinatraDocumentation:EnvironmentscanbesetthroughtheRACK_ENVenvironm
我在app/helpers/sessions_helper.rb中有一个帮助程序文件,其中包含一个方法my_preference,它返回当前登录用户的首选项。我想在集成测试中访问该方法。例如,这样我就可以在测试中使用getuser_path(my_preference)。在其他帖子中,我读到这可以通过在测试文件中包含requiresessions_helper来实现,但我仍然收到错误NameError:undefinedlocalvariableormethod'my_preference'.我做错了什么?require'test_helper'require'sessions_hel
我一直很高兴地使用DelayedJob习惯用法:foo.send_later(:bar)这会调用DelayedJob进程中对象foo的方法bar。我一直在使用DaemonSpawn在我的服务器上启动DelayedJob进程。但是...如果foo抛出异常,Hoptoad不会捕获它。这是任何这些包中的错误...还是我需要更改某些配置...或者我是否需要在DS或DJ中插入一些异常处理来调用Hoptoad通知程序?回应下面的第一条评论。classDelayedJobWorker 最佳答案 尝试monkeypatchingDelayed::W
我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www
我只想对我一直在思考的这个问题有其他意见,例如我有classuser_controller和classuserclassUserattr_accessor:name,:usernameendclassUserController//dosomethingaboutanythingaboutusersend问题是我的User类中是否应该有逻辑user=User.newuser.do_something(user1)oritshouldbeuser_controller=UserController.newuser_controller.do_something(user1,user2)我
什么是ruby的rack或python的Java的wsgi?还有一个路由库。 最佳答案 来自Python标准PEP333:Bycontrast,althoughJavahasjustasmanywebapplicationframeworksavailable,Java's"servlet"APImakesitpossibleforapplicationswrittenwithanyJavawebapplicationframeworktoruninanywebserverthatsupportstheservletAPI.ht
导读:随着叮咚买菜业务的发展,不同的业务场景对数据分析提出了不同的需求,他们希望引入一款实时OLAP数据库,构建一个灵活的多维实时查询和分析的平台,统一数据的接入和查询方案,解决各业务线对数据高效实时查询和精细化运营的需求。经过调研选型,最终引入ApacheDoris作为最终的OLAP分析引擎,Doris作为核心的OLAP引擎支持复杂地分析操作、提供多维的数据视图,在叮咚买菜数十个业务场景中广泛应用。作者|叮咚买菜资深数据工程师韩青叮咚买菜创立于2017年5月,是一家专注美好食物的创业公司。叮咚买菜专注吃的事业,为满足更多人“想吃什么”而努力,通过美好食材的供应、美好滋味的开发以及美食品牌的孵