0

I'm using annotation-based configuration - no XML at all.

I've configured everything, but can't figure out why OrderService is not being autowired and continues to be null. The class below at the very bottom is the one that shows the actual problem. The other classes are all my configuration.

I do have log4j on this application but am inexperienced with it. Is there a way I can log what packages/classes are being scanned to help determine why this isn't working?

OrderService

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

Services config

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

Main Config

@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();
}

Where the problem is...

@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();
Ben
  • 60,438
  • 111
  • 314
  • 488
  • Are both `OrderService` and `NewJobRequestHandler` in the packages in your `@ComponentScan`? Also, you don't need a `@Bean` for the `@OrderService`. The `@Service` already identifies it as a bean. – Sotirios Delimanolis Feb 28 '13 at 15:31
  • Yes, they are both in packages that are in `@ComponentScan`. Are you saying I don't need the services config file because it is flagged as a `@Service` and it's being scanned? – Ben Feb 28 '13 at 15:33
  • If that is the only bean in there, yes, that is what I am saying. – Sotirios Delimanolis Feb 28 '13 at 15:34
  • @SotiriosDelimanolis - Thanks, I'll look into that once I get the service working. – Ben Feb 28 '13 at 15:39

1 Answers1

0

The problem here is that your Spring configuration and context is separated from your Jersey/Grizzly stack. You are expecting Jersey to be able to get beans from Spring, but it has no knowledge that Spring exists, its annotations don't mean anything to it.

You need to replace your Jersey Servlet with Spring's Jersey Servlet and add a ContextLoaderListener. Take a look at this example on how to wire Jersey and Spring. I'm not sure how Grizzly works, but if it's like any other servlet container, this should work.

For log4j, you can set your root logger level to INFO or DEBUG and you will get all sorts of log statements from Spring.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • I added that... It did expose an error in the Autowiring so it looks like that pat is working correctly. However, now I get the error `Exception in thread "main" java.lang.IllegalStateException: GenericApplicationContext does not support multiple refresh attempts: just call 'refresh' once` when deploying despite having 1 refresh. – Ben Feb 28 '13 at 15:38
  • Are you passing the context to any method? – Sotirios Delimanolis Feb 28 '13 at 15:39
  • No, but I do have a service that is manually wired `VendorService vendorService = (VendorService) ApplicationContextProvider.getApplicationContext().getBean("vendorService");` in another portion of the application. But commenting it out has no effect – Ben Feb 28 '13 at 15:43
  • Is this running within some container? Or is your entry point the `Main`? – Sotirios Delimanolis Feb 28 '13 at 15:45
  • `Main` is the entry point. I'm running on Grizzly, so it sets up the http container and everything – Ben Feb 28 '13 at 15:47
  • I don't know how Grizzly works, but if you're going to use Spring's DI, you're going to have to get all of your beans from its context, not generate them yourself. As for your exception, check where it happens and follow the trail. – Sotirios Delimanolis Feb 28 '13 at 15:52
  • It's coming from `ctx.refresh()` you suggested I add. – Ben Feb 28 '13 at 15:53
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/25299/discussion-between-sotirios-delimanolis-and-webnet) – Sotirios Delimanolis Feb 28 '13 at 15:56
  • Please take a look at this thread [1], there is an example of Jersey <-> Spring <-> Grizzly integration. [1] http://grizzly.1045725.n5.nabble.com/Integration-Grizzly2-2-X-with-Jersey-and-Spring-td5710024.html – alexey Feb 28 '13 at 23:58