如何在JAX-RS中注入ApplicationContext [英] How to inject ApplicationContext in JAX-RS

查看:272
本文介绍了如何在JAX-RS中注入ApplicationContext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Spring和JAX-RS编写一个Web服务,我对以下内容感到困惑



这是我的服务例子

  @Path(/ users)
公共类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);
返回userDAO.getById(id);
}

}

这是我的beans.xml

 <?xml version =1.0encoding =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\">

<! - 数据源初始化 - >
< bean id =dataSourceclass =org.springframework.jdbc.datasource.DriverManagerDataSource>
< property name =driverClassNamevalue =com.mysql.jdbc.Driver/>
< property name =urlvalue =jdbc:mysql:// localhost:3306 / hospital-system/>
< property name =usernamevalue =root/>
< property name =passwordvalue =root/>
< / bean>

< bean id =userDaoclass =dao.UserDAO>
< property name =dataSourceref =dataSource/>
< / bean>

< / beans>

我想知道这是否是每次调用资源时加载Application上下文的正确技术,如果不,我该怎么改呢?

解决方案

每次在对此方法进行Web服务调用时创建应用程序工厂实例肯定不是一个好方法



因为这个方法可能被称为100次,我们不希望创建这个上下文100次,因为没有任何改变。<你可以实现 ApplicationContextAware 界面





你可以做



ApplicationContext context = new ClassPathXmlApplicationContext(beans.xml );



在其他地方,也许当您的应用程序启动时。



<希望这有帮助!



祝你好运!


I am writing a web service using Spring and JAX-RS, and I'm kinda confused with the following

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

<?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?

解决方案

Creating the application factory instance everytime when a web service call is made to this method is definitely, not a good approach ,

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.

you may implement the ApplicationContextAware interface

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!

这篇关于如何在JAX-RS中注入ApplicationContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆