简体   繁体   English

如何在JAX-RS中注入ApplicationContext

[英]How to inject ApplicationContext in JAX-RS

I am writing a web service using Spring and JAX-RS, and I'm kinda confused with the following 我正在使用Spring和JAX-RS编写Web服务,我对以下内容有些困惑

Here's my service exmaple 这是我的服务实例

@Path("/users")
public class UserService {

    @GET
    @Path("{id}")
    @Produces("application/xml")
    public User getById(@PathParam("id") int id) {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        UserDAO userDAO = (UserDAO) context.getBean("userDao");
        return userDAO.getById(id);
    }

}

And here's my beans.xml 这是我的beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <!-- Initialization for data source -->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/hospital-system"/>
        <property name="username" value="root"/>
        <property name="password" value="root"/>
    </bean>

    <bean id="userDao" class="dao.UserDAO">
        <property name="dataSource" ref="dataSource"/>
    </bean>

</beans>

I wonder if this is the right technique to load Application context each time the resource get's called, and if no, how should I change it? 我想知道这是否是每次调用资源时加载应用程序上下文的正确技术,如果没有,我应该如何更改它?

The Jersey reference manual describes how to integrate with Spring in detail: Jersey参考手册详细介绍了如何与Spring集成:

Jersey provides an extension to support Spring DI. Jersey提供了扩展以支持Spring DI。 This enables Jersey to use Spring beans as JAX-RS components (eg resources and providers) and also allows Spring to inject into Jersey managed components. 这使Jersey能够将Spring Bean用作JAX-RS组件(例如资源和提供程序),并且还允许Spring注入到Jersey托管的组件中。

The Spring extension module configuration is based on annotations. Spring扩展模块的配置基于注释。 Spring beans are injected and JAX-RS classes are made Spring managed using annotations. 使用注解注入Spring Bean,并使Spring管理JAX-RS类。 Injected Spring beans can have further dependencies injected using Spring XML configuration. 注入的Spring bean可以使用Spring XML配置注入更多的依赖项。 Spring singleton and request scopes are supported. 支持Spring单例和请求范围。

To enable JAX-RS resources to work Spring functionality that requires proxying, such as Spring transaction management (with @Transactional), Spring Security and aspect oriented programming (such as @Aspect), the resources must themselves be managed by Spring, by annotating with @Component, @Service, @Controller or @Repository: 为了使JAX-RS资源能够运行需要代理的Spring功能,例如Spring事务管理(使用@Transactional),Spring Security和面向方面的编程(例如@Aspect),这些资源本身必须由Spring通过注释来管理。 @ Component,@ Service,@ Controller或@Repository:

 import javax.ws.rs.GET; import javax.ws.rs.Path; import org.springframework.stereotype.Component; @Component @Path("/") public class SomeResource { @Transactional @GET public void updateResource() { // ... } } 

Limitations: 限制:

  • Spring beans can't be injected directly into JAX-RS classes by using Spring XML configuration 通过使用Spring XML配置,不能将Spring Bean直接注入到JAX-RS类中

Dependencies 依赖

If you want to use Jersey Spring DI support you will need to add the jersey-spring3 module into the list of your dependencies: 如果要使用Jersey Spring DI支持,则需要将jersey-spring3模块添加到依赖项列表中:

 <dependency> <groupId>org.glassfish.jersey.ext</groupId> <artifactId>jersey-spring3</artifactId> <version>2.18</version> </dependency> 

The above module adds transitive dependencies on Spring modules. 上面的模块在Spring模块上添加了传递依赖。 See jersey-spring3 module dependencies for more details about list and scope of dependencies. 有关依赖项列表和范围的更多详细信息,请参阅jersey-spring3模块依赖项。 Please note the module depends on The Spring/HK2 Bridge that is used to inject Spring services into HK2 services or inject HK2 services into Spring services. 请注意,模块取决于Spring / HK2桥,该桥用于将Spring服务注入HK2服务或将HK2服​​务注入Spring服务。

To see an example of Spring DI support in Jersey refer to the Spring DI Example . 要查看Jersey中的Spring DI支持示例,请参阅Spring DI Example

Creating the application factory instance everytime when a web service call is made to this method is definitely, not a good approach , 每次在对该方法进行Web服务调用时,都创建应用程序工厂实例绝对不是一个好方法,

as this method might get called maybe say 100 times, we do not want to have a over head of creating this context 100 times as nothing is going to change in it. 由于此方法可能会被调用100次,因此我们不想创建此上下文100次,因为其中没有任何变化。

you may implement the ApplicationContextAware interface 您可以实现ApplicationContextAware接口

and you can do 你可以做

ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

at some other place , maybe when your application starts up. 在其他地方,也许是在您的应用程序启动时。

hope this helps! 希望这可以帮助!

Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在JAX-RS资源中注入ConnectionFactory? - How to inject a ConnectionFactory in a JAX-RS resource? 如何@ fastfast.jackson.ObjectMapper注入JAX-RS资源? - How to @Inject fastxml.jackson.ObjectMapper into JAX-RS Resource? @Inject CDI无法与JAX-RS一起使用 - @Inject CDI not working with JAX-RS 将EJB注入JAX-RS(RESTful服务) - Inject an EJB into JAX-RS (RESTful service) 使用JAX-RS将现有的Spring ApplicationContext连接到REST服务 - Wire an existing spring ApplicationContext to REST service with JAX-RS 如何使用JAX-RS从标头“ accept-language”注入语言环境列表? - How to inject a List of Locales from header “accept-language” with JAX-RS? 我如何@Inject CDI @ApplicationScoped bean到@RequestScoped JAX-RS bean? - How do I @Inject a CDI @ApplicationScoped bean into a @RequestScoped JAX-RS bean? 如何在JAX-RS REST Web服务中注入JMS资源? - How do I inject a JMS resource in a JAX-RS REST Web Service? JAX-RS / jersey:客户端代理:如何从上下文注入路径参数而不是方法参数? - JAX-RS / jersey: client proxy: how to inject path parameter from context instead of method argument? 如何在JBoss 7.1中使用JAX-RS在Web服务中注入数据库管理器? - How to inject DataBase Manager inside a web service using JAX-RS in JBoss 7.1?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM