2013-04-15 95 views
1

我有自动装配弹簧自动装配问题在Servlet

首先有些问题我创建一个嵌入式服务器

Main.java

Server server = new Server(8080); 
    CXFNonSpringServlet cxf = new CXFNonSpringJaxrsServlet(); 
    ServletHolder servlet = new ServletHolder(cxf); 
    servlet.setInitParameter("javax.ws.rs.Application", "com.asd.dispatcher.rest.testApplication"); 
    servlet.setName("services"); 
    servlet.setForcedPath("services");  

    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS); 
    context.setContextPath("/hello"); 
    server.setHandler(context); 
    context.addServlet(servlet, "/*"); 
    server.start(); 

testApplication.java

public class testApplication extends Application { 
@Override 
public Set<Class<?>> getClasses() { 
    Set<Class<?>> classes = new HashSet<Class<?>>(); 
    classes.add(testServlet.class); 
    return classes; 
} 
} 

testServlet.java

@Path("/people") 
@Component 
@Scope("prototype") 
public class testServlet {  

@Autowired 
private StatsService statsService; 

@Produces({ "application/json" }) 
@GET 
public String getPeople(@QueryParam("page") @DefaultValue("1") final int page) { 
    System.out.println("======= getPeople"); 

    //statsService.printStats(); 

    return "Hello World"; 
} 
} 

现在我的问题是,我的statsService没有被自动装配在testServlet.java,但我可以自动装配成与@Service注解另一个类,

是因为我的这种使用CXFNonSpringServlet? 还是因为我尝试Autowire的方式?

回答

0

好吧,我得到它的工作

好吧,所以我固定我T(我会张贴此作为回答,但不能回答我的问题:/)

把答案在这里帮助别人同样的问题

在看看下面 Autowiring in servlet

我来到了一个帖子构造方法获得一个ApplicationContext和结论那么这个bean会工作

例如:我的代码会是这样

@Path("/people") 
@Component 
@Scope("prototype") 
public class testServlet {  

private StatsService statsService; 

@PostConstruct 
public void initStats() { 
    System.out.println("============================= Init");   
    ApplicationContext context = new GenericXmlApplicationContext("applicationContext.xml"); 
    statsService = context.getBean("statsService", StatsService.class); 
    } 


@Produces({ "application/json" }) 
@GET 
public String getPeople(@QueryParam("page") @DefaultValue("1") final int page) { 
    System.out.println("======= getPeople"); 

    statsService.printStats(); 

    return "Hello World"; 
} 
} 

尽管这不是自动装配,但它确实有效,如果有人知道如何使用自动装配来做到这一点,我很想知道它会比我找到的解决方案更清洁。

*在一个侧面说明我拿起了这个'解决方案'的新问题,我的问题在于,我也有其他bean自动装入,并且似乎虽然自动接线初始化这些bean的任何更改他们的状态在另一个阶层并没有反映在统计服务事实上,这些豆的状态保持不变(尽管这可能是我怀疑的行为,我仍然是新的春天,所以我不知道)

0

我不知道CXFNonSpringServelt是什么,但我的问题是:您是否在应用程序的context-config.xml文件中添加了上面的行?

<beans 
xmlns="http://www.springframework.org/schema/beans" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:context="http://www.springframework.org/schema/context" 
xsi:schemaLocation=" 
    http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.2.xsd"> 
... 
... 
<context:component-scan base-package="package of the classes with annotations" /> 

而在你的服务器类,你应该添加注释@Service

@Service("myService") 
public class MyService ... 

而且你可以使用@Authowire这样的:

public class Client{ 
    @Autowire 
    MyService myservice; 
    ... 
+0

是的,这正是我在我的XML配置文件,我也尝试注释我的服务,因为你建议(事实上,我的服务甚至定义为我的XML中的bean),但它仍然没有区别 – Daxxy

+0

好吧,但你如何检测到自动装置没有影响?当你使用变量'myservice'时你有一个空指针异常吗? – daniele

+0

如果您在客户端添加以下代码,会发生什么情况? 'BeanFactory factory = new XmlBeanFactory(new ClassPathResource(“application-context.xml”)); MyService myservice =(MyService)factory.getBean(“myservice”);' – daniele