0

how can I access to exampleService from the thread ? In new thread is null. ExampleService is an annotated @Service class. SessionFactory I set from dao and it is no null.

public class ExampleInterceptor extends EmptyInterceptor {

    private Logger logger = Logger.getLogger(getClass());

    private static final long serialVersionUID = 1L;

    @Autowired
    SessionFactory sessionFactory;

    @Autowired
    ExampleService exampleService;

    public void setExampleService(ExampleService exampleService) {
        this.exampleService = exampleService;
    }

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    public void afterTransactionCompletion(Transaction tx) {
        new Thread(new SimpleThread(exampleService)).start();
    }

    protected class SimpleThread implements Runnable {
    protected ExampleService exampleService;

        public SimpleThread() {
        };
        public SimpleThread(ExampleService exampleService) {
      this.exampleService = exampleService;
        }
        @Override
        public void run() {
                    exampleService.method(); // is null at this location                        
        }

    }

}
Jens
  • 67,715
  • 15
  • 98
  • 113
kxyz
  • 802
  • 1
  • 9
  • 32

1 Answers1

1

You need to Annotate the Service Class with @Service, And for using that object you need to Autowire it. If i am not wrong, In your "SimpleThread" class, you are using not autowiring the property "protected ExampleService exampleService", which is a not having @Autowired annotation. @Service, @Controller, @Repository & @Component

Community
  • 1
  • 1
Benoy Prakash
  • 436
  • 5
  • 17