2

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...

user899757
  • 351
  • 2
  • 4
  • 21

3 Answers3

1

I need to add the annotation config and the component scan to the applicationContext.xml so that it would properly "autowire" the bean. Once this was done, I was able to get rid of the variable in the test class (forecastElementService).

user899757
  • 351
  • 2
  • 4
  • 21
0

At least there is no propery 'entityManagerFactory' in your implementation class. Not sure if this may already cause the problem. Can you share the complete stacktrace (including nested exceptions) and some more snippets of the *context.xml file?

EDIT: Have a look at this answer: Testing against Java EE 6 API

Community
  • 1
  • 1
Wintermute
  • 1,521
  • 1
  • 13
  • 34
  • Here is the entire context.xml file: – user899757 Nov 14 '11 at 23:05
  • it won't let me post the entire xml and I cannot post an answer for the next 8 hours... I took it out. Thanks for pointing it out but I still get the same error. Actually, I now decided to just point to the context file on my c: drive and I now get a different error message - see reply to Jhakki. – user899757 Nov 14 '11 at 23:25
0

I would suggest using either @Repository or @Service - but not both - on your service impl class.

You might run into another problem related to transactions not firing - here: https://jira.springsource.org/browse/SPR-5082 - a work around is to define beans in the application context xml definition file.

Shikhar Mishra
  • 1,965
  • 2
  • 13
  • 14
  • After setting @ContextConfiguration up so that it loads the context file from the C: drive, I now get the following exception: `Absent Code attribute in method that is not native or abstract in class file javax/ejb/TransactionAttributeType` – user899757 Nov 14 '11 at 23:10
  • It won't let me post the entire pom for some reason but here are the two at play here: `commons-dbcp commons-dbcp 1.4 javax javaee-api ` – user899757 Nov 14 '11 at 23:19
  • Absent code ... means, that you have the api, but you are missing a jee implementation. Which application server do you want to use for your application? For jboss it is e.g. the dependency: org.jboss.spec jboss-javaee-6.0 1.0.0.Final pom provided – Wintermute Nov 14 '11 at 23:54