I have spent hours trying to figure out why my code will thow the following exception. At this point, I am hoping that someone can be more clever than I am as I am losing hope... ;)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.ls.forecast.jpa.ForecastElementService] 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), @org.springframework.beans.factory.annotation.Quali fier(value=main)} at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:924) at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:793) at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:707) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:480)
I have looked at many threads/tutorials without luck. They all seem to have the same set up as mine. I added @Service on the implementation of the service, I added a Qualifier, checked that ForecastElementServiceImpl actually implemented the interface ForecastElementService.
Service interface:
public interface ForecastElementService {
Collection<ForecastElement> retrieve(String date);
Collection<ForecastElement> retrieve();
}
interface implementation:
@Stateless
@TransactionAttribute(TransactionAttributeType.REQUIRED)
@Repository("forecastElementService")
@Service
public class ForecastElementServiceImpl implements ForecastElementService {
@PersistenceContext
protected EntityManager em;
@Override
@Cacheable("forecastElements")
public Collection<ForecastElement> retrieve(String date) {
String sql = null;
if(date != null) {
sql = " SELECT fe FROM ForecastElement fe JOIN FETCH fe.forecastType WHERE ?1 between fe.startDate and fe.endDate";
} else {
sql = " SELECT fe FROM ForecastElement fe JOIN FETCH fe.forecastType";
}
final TypedQuery<ForecastElement> query = em.createQuery(sql, ForecastElement.class);
return query.getResultList();
}
@Override
@Cacheable("forecastElements")
public Collection<ForecastElement> retrieve() {
return retrieve(null);
}
}
context.xml:
<bean id="forecastElementService" class="com.ls.forecast.jpa.ForecastElementServiceImpl">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
AND finally the trouble test class - the forecastElementService variable is the one throwing the exception.
@Repository
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"*-context.xml"})
public class ModelJpaTest extends AbstractTransactionalJUnit4SpringContextTests {
//final Logger logger = LoggerFactory.getLogger(ModelJpaTest.class);
@Autowired
protected ForecastElementService forecastElementService;
@Autowired
@Qualifier("basicDataSource")
@Override
public void setDataSource(DataSource dataSource) {
super.setDataSource(dataSource);
}
@Test
public void LoadModelTest() {
assertNotNull("forecastElementService is null", forecastElementService);
Collection<ForecastElement> elements = forecastElementService.retrieve();
assertTrue(elements.size() > 0);
}
}
Any insight or help would be immensely appreciated!
See @ContextConfiguration... If I place the ModelJpaTest-context.xml in the resource (test) folder of my maven project and edit to:
@ContextConfiguration({"/ModelJpaTest-context.xml"})
Really confused by now...