2017-10-18 139 views
0

我一直在試圖將Spring 4與Jersey 2一起使用,並注意到使用jersey-spring3擴展沒有辦法讓@Named由Spring管理註釋資源。他們必須註明@Component由Spring管理。在Jersey 2 + Spring中使用JSR-330註解

如果資源註釋爲@Named HK2似乎「接管」管理該資源。

資源

import com.example.jersey.spring.Greeting; 
import java.util.logging.Logger; 
import javax.inject.Inject; 
import javax.inject.Named; 
import javax.inject.Singleton; 
import javax.ws.rs.GET; 
import javax.ws.rs.Path; 
import javax.ws.rs.Produces; 
import javax.ws.rs.core.MediaType; 

@Named 
@Singleton 
@Path("resource") 
public class Resource { 

    private static final Logger logger = Logger.getLogger("Resource"); 

    public void init() { 
     logger.info("Creating -> " + this + " injected with -> " + greeting); 
    } 

    @Inject 
    private Greeting greeting; 

    @GET 
    @Produces(MediaType.TEXT_PLAIN) 
    public String getIt() { 
     logger.info("Working on " + this + " Greeting " + greeting); 
     return "Got it!"; 
    } 
} 

在以下日誌消息下面Spring的ApplicationContext

<?xml version="1.0" encoding="UTF-8"?> 
<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.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd" 
    default-init-method="init"> 

    <context:component-scan base-package="com.example.jersey.webapp"/> 
    <bean id="greeting" class="com.example.jersey.spring.Greeting"/> 
</beans> 

結果被打印在啓動

Oct 18, 2017 3:11:44 PM org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor <init> 
INFO: JSR-330 'javax.inject.Inject' annotation found and supported for autowiring 
Oct 18, 2017 3:11:44 PM com.example.jersey.webapp.Resource init 
INFO: Creating -> [email protected] injected with -> [email protected] 
Oct 18, 2017 3:11:45 PM org.springframework.web.context.ContextLoader initWebApplicationContext 
INFO: Root WebApplicationContext: initialization completed in 249 ms 

和一對夫婦的後GET調用。

Oct 18, 2017 3:11:56 PM com.example.jersey.webapp.Resource getIt 
INFO: Working on [email protected] Greeting [email protected] 
Oct 18, 2017 3:12:03 PM com.example.jersey.webapp.Resource getIt 
INFO: Working on [email protected] Greeting [email protected] 

所以它似乎春天將創建一個豆,但不是用來服務請求的一個多數民衆贊成。但是,如果使用Named而不是Component,則整個生命週期由Spring管理。

有沒有辦法使用標準@Named註釋並確保Spring完全管理資源的生命週期?

回答

0

我不認爲有辦法可以禁用HK2。

從最新Jersey-2 Userguide

資源本身必須被Spring管理,用@Component,@Service,@Controller或@Repository

春標註推薦使用的更具體註釋@Service,@Controller@Repository

Spring 4.3.12 Documentation §7.10.1

@Component和進一步典型化註解

的@Repository註解爲任何類標記物滿足 角色或鉛板的存儲庫(也被稱爲數據訪問對象 或DAO)。該標記的用途是自動翻譯 例外,如第20.2.2節「異常翻譯」中所述。

Spring提供了進一步的構造型註釋:@Component,@Service, 和@Controller。 @Component是彈簧管理組件的任何 通用刻板印象。 @Repository,@Service和@Controller分別是 @Component的特化,用於更具體的用例, 示例,分別在持久層,服務和表示層中分別爲 。因此,您可以用@ @Component註釋組件類,而是通過@Repository,@Service或@Controller對它們進行註釋,而您的類更適合用於通過工具處理或與方面關聯的 。例如,這些 原型註釋爲切入點提供了理想的目標。它也是 @Repository,@Service和@Controller可能會在未來的Spring Framework版本中攜帶 附加語義。因此, 如果您選擇使用@Component或@Service作爲 服務層,@Service顯然是更好的選擇。同樣,如上所述 ,已經支持@Repository作爲持久層中的自動異常轉換的標記。