2013-02-28 44 views
0

我正在使用基於註釋的配置 - 根本沒有XML。服務未正確自動佈線 - 基於註釋的配置

我已經配置了所有內容,但無法弄清楚爲什麼OrderService未被自動裝配並且仍然是null。最底層的類是顯示實際問題的類。其他類都是我的配置。

我確實有log4j在這個應用程序,但我沒有經驗。有沒有一種方法可以記錄正在掃描的軟件包/類,以幫助確定這不起作用的原因?

OrderService

@Service 
public class OrderService extends GenericService<OrderDAO, Order> { 
    @Autowired 
    OrderDAO dao; 
} 

服務配置

@Configuration 
public class Config { 
    @Bean 
    public OrderService orderService() { 
     return new OrderService(); 
    } 
} 

主要配置

@Configuration 
@ComponentScan(basePackages = { 
     "com.production.api", 

     //todo: may not need the rest of these 
     "com.production.api.dao", 
     "com.production.api.models", 
     "com.production.api.requests", 
     "com.production.api.requests.job", 
     "com.production.api.resources", 
     "com.production.api.resources.job", 
     "com.production.api.services" 
}) 
@Import({ 
     com.production.api.services.Config.class, 
     com.production.api.dao.Config.class 
}) 
@PropertySource(value= "classpath:/META-INF/application.properties") 
@EnableTransactionManagement 
public class Config { 

Main.java

public static void main(String[] args) throws IOException { 
    //process annotation configuration 
    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(Config.class); 

    HttpServer httpServer = startServer(); 
    System.out.println(String.format("Jersey app started with WADL available at " + "%sapplication.wadl\nTry out %shelloworld\nHit enter to stop it...", BASE_URI, BASE_URI)); 
    System.in.read(); 
    httpServer.stop(); 
} 

問題出在哪裏...

@Component 
public class NewJobRequestHandler { 

    public static Logger logger = Logger.getLogger(Logger.class.getName()); 

    //@todo Why isn't this autowiring? 
    @Autowired 
    OrderService orderService; 

    public void instantiateOrderService() { 
     //@todo remove this manual wiring 
     orderService = (OrderService) ApplicationContextProvider.getApplicationContext().getBean("orderService"); 
    } 

    public AuthenticatedApiResponse getResponse(AuthenticatedRequest<NewJobRequest> request) { 
     instantiateOrderService(); 
+0

您的'@ ComponentScan'包中是否都有'OrderService'和'NewJobRequestHandler'?另外,您不需要'@ OrderService'的'@ Bean'。 '@ Service'已經將它識別爲一個bean。 – 2013-02-28 15:31:15

+0

是的,它們都在'@ ComponentScan'中。你是說我不需要服務配置文件,因爲它被標記爲'@ Service'並且正在被掃描? – Webnet 2013-02-28 15:33:30

+0

如果那是那裏唯一的豆子,是的,那就是我所說的。 – 2013-02-28 15:34:18

回答

0

的這裏的問題是,你的Spring配置和上下文從新澤西/灰熊堆棧分離。你期望Jersey能夠從Spring獲得beans,但它並不知道Spring存在,它的註釋並不意味着什麼。

您需要用Spring的Jersey Servlet替換Jersey Servlet並添加一個ContextLoaderListener。請看this example關於如何連接澤西島和春季。我不確定Grizzly是如何工作的,但如果它像其他任何servlet容器一樣,這應該起作用。

對於log4j,您可以將根記錄器級別設置爲INFODEBUG,您將獲得Spring的各種日誌語句。

+0

我補充說......它確實暴露了Autowiring中的一個錯誤,所以看起來pat的工作正常。但是,現在我得到了錯誤'線程中的異常「main」java.lang.IllegalStateException:GenericApplicationContext不支持多次刷新嘗試:部署時儘管刷新1次,只調用'刷新'一次'。 – Webnet 2013-02-28 15:38:25

+0

您是否將上下文傳遞給任何方法? – 2013-02-28 15:39:08

+0

不,但我確實有一個手動連線的服務:VendorService vendorService =(VendorService)ApplicationContextProvider.getApplicationContext()。getBean(「vendorService」);'在應用程序的另一部分。但評論它沒有影響 – Webnet 2013-02-28 15:43:21