简体   繁体   中英

Autowired class from the jar file outside the classes

How can the Class in a jar file get scanned by the <context:component-scan base-package="" /> . I am getting the below error for my autowired class.

SEVERE [localhost-startStop-1] org.apache.catalina.core.ApplicationContext.log StandardWrapper.Throwable org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: org.quartz.Scheduler com.path.controller.MyController.scheduler; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.quartz.Scheduler] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

code:

package com.path.controller;
import org.quartz.Scheduler;

@Controller
public class MyController{

   @Autowired
   Scheduler scheduler;
}

This scheduler is inside the jar file quartz-oracle-2.1.6.jar and is under WEB-INF\\lib . This lib is outside the classes folder of the deployment package. In the applicationcontext.xml i have the below entry,

<context:component-scan base-package="com.path" />

When you export the jar file using the export utility in eclipse there is a option called Add directory entries. Check this option and export the jar file, this will solve the problem

I'm guessing you haven't defined your scheduler bean. You need to first define the bean in order to inject into your controller.

In your applicationcontext.xml first define a bean with type Scheduler . You might need something like below. Be sure to check out the quartz documentation to check which scheduler you need and how to instantiate it.

<bean id="schedulerFactory" 
    class="org.quartz.StdSchedulerFactory" />

<bean id="scheduler" class="org.quartz.Scheduler"  factory-bean="schedulerFactory" factory-method="getDefaultScheduler" />

As the exception complains "No qualifying bean of type [org.quartz.Scheduler]" , we will have to define the bean of type org.quartz.Scheduler, But that is not possible without any concrete implementation of the same, So , we will have to get a concrete implementation from the Factory class org.quartz.impl.StdSchedulerFactory and it's non static method getScheduler().

Hence, you will have to add following two lines in you context xml file and it will work , I have verified the same with the same version of spring which you are using :

<bean id="schedulerFactory"   class="org.quartz.impl.StdSchedulerFactory" />    

<bean id="scheduler" class="org.quartz.Scheduler"  factory-bean="schedulerFactory" factory-method="getScheduler" />

A console output after printing the initialized bean gives this :

  scheduler=org.quartz.impl.StdScheduler@536aaa8d

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM