如何在 JAX-RS 中注入 ApplicationContext

How to inject ApplicationContext in JAX-RS

我正在使用 Spring 和 JAX-RS 编写 Web 服务,但我对以下内容有些困惑

这是我的服务示例

@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);
    }

}

这是我的 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>

我想知道这是否是每次调用资源时加载应用程序上下文的正确技术,如果不是,我应该如何更改它?

每次调用此方法时都创建应用程序工厂实例绝对不是一个好方法,

由于此方法可能会被调用 100 次,我们不希望创建此上下文 100 次,因为它不会发生任何变化。

您可以实现 ApplicationContextAware 接口

  • How to inject ApplicationContext itself

你可以做到

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

在其他地方,可能是在您的应用程序启动时。

希望对您有所帮助!

祝你好运!

Jersey 参考手册 describes 如何与 Spring 集成的详细信息:

Jersey provides an extension to support Spring DI. This enables Jersey to use Spring beans as JAX-RS components (e.g. resources and providers) and also allows Spring to inject into Jersey managed components.

The Spring extension module configuration is based on annotations. Spring beans are injected and JAX-RS classes are made Spring managed using annotations. Injected Spring beans can have further dependencies injected using Spring XML configuration. Spring singleton and request scopes are supported.

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:

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

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:

<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. See jersey-spring3 module dependencies for more details about list and scope of dependencies. 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.

To see an example of Spring DI support in Jersey refer to the Spring DI Example.