2013-04-15 137 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