Bonsoir,

J'essaye d'utiliser Spring et Hibernate (avec le pattern DAO) mais en vain.
J'ai un DAO :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
package com.mycompany.test;
 
import com.mycompany.test.Country;
 
public interface CountryDAO {
 
    Country getCountry(String countryId);
 
}
et son implémentation:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package com.mycompany.test;
 
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.transaction.annotation.Transactional;
 
import com.mycompany.test.CountryDAO;
import com.mycompany.test.Country;
 
 
public class CountryDAOImpl implements CountryDAO{
 
    private SessionFactory sessionFactory;
 
    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }
 
    @Override
    @Transactional(readOnly=true)
    public Country getCountry(String countryId) {
 
        Session session = this.sessionFactory.getCurrentSession();
        return (Country)session.createQuery("my query")
        .setString(0, countryId).uniqueResult();        
 
    }
 
}
Et mon application context séparé en deux parties (template généré par STS):

Fichier launch-context.xml :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:batch="http://www.springframework.org/schema/batch"
    xmlns:p="http://www.springframework.org/schema/p" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xsi:schemaLocation="
        http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch-2.1.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        ">
 
    <context:property-placeholder location="classpath:batch.properties"/>
 
    <!-- Datasource used for Spring batch meta-datas -->
 
    <bean id="dataSource"
                     class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName"
                         value="${batch.jdbc.driver}" />
        <property name="url" value="${batch.jdbc.url}" />
        <property name="username" value="${batch.jdbc.user}" />
        <property name="password" value="${batch.jdbc.password}" />
    </bean>
 
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <constructor-arg index="0" ref="dataSource" />
    </bean>
 
    <jdbc:initialize-database data-source="dataSource">
            <jdbc:script location="${batch.schema.drop_script}" />
        <jdbc:script location="${batch.schema.script}" />
    </jdbc:initialize-database>
 
    <batch:job-repository id="jobRepository" />
 
     <import resource="classpath:/META-INF/spring/module-context.xml"/>
</beans>
Fichier module-context.xml :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:batch="http://www.springframework.org/schema/batch"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch-2.1.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
 
    <batch:job id="job1">
        <batch:step id="step1">  
            <batch:tasklet start-limit="100">
                <batch:chunk reader="reader1"
                    processor="processor1" writer="writer1"
                    commit-interval="${commit_interval}" />
            </batch:tasklet>
        </batch:step>
    </batch:job>
 
    <!-- Datas used by the processor : Hibernate configuration -->
 
    <tx:annotation-driven transaction-manager="txManager"/>
 
    <bean id="cacheDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${cache.hibernate.driver}" />
    <property name="url" value="${cache.hibernate.url}" />
    <property name="username" value="${cache.hibernate.user}" />
    <property name="password" value="${cache.hibernate.password}" />
</bean>
 
    <bean id="cacheSessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="cacheDataSource" />
        <property name="mappingResources">
          <list>
            <value>hibernate-mapping/Country.hbm.xml</value>
          </list>
        </property>
        <property name="hibernateProperties">
          <value>
                hibernate.dialect=org.hibernate.dialect.MySQLDialect
          </value>
        </property>
    </bean>    
 
    <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="cacheSessionFactory" />
    </bean>
 
    <!-- DAO declaration -->
 
    <bean id="countryDAO"
class="com.mycompany.test.CountryDAOImpl">
        <property name="sessionFactory" ref="cacheSessionFactory" />
    </bean>
 
</beans>
A part ca, j'ai une classe de test :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package com.mycompany.test;
 
import static org.junit.Assert.*;
 
import org.junit.runner.RunWith;
import org.junit.Test;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.beans.factory.annotation.Autowired;
 
import com.mycompany.test.Country;
 
 
@ContextConfiguration(locations={"classpath:launch-context.xml"})
@RunWith(SpringJUnit4ClassRunner.class)
public class CountryDAOImplTests {
 
    @Autowired
    private CountryDAOImpl countryDAO;
 
    @Test
    public void testGetCountry() throws Exception
    {
 
    }
}
Quand je lance le test avec JUnit, l'instantiation des singletons (sessionFactory et DAOs) se fait bien, tout fonctionne du coté Hibernate aussi. Mais j'obtiens une exception au moment du autowiring du DAO dans la classe de test. Apparemment Spring ne trouve aucun candidat pour l'autowiring. Pourtant il est bien la.
Voici l'erreur que j'obtiens :
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.mycompany.test.CountryDAOImpl com.mycompany.test.CountryDAOImplTests.countryDAO; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.mycompany.test.CountryDAOImpl] 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)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:508)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:84)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:285)
... 26 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.mycompany.test.CountryDAOImpl] 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)}
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)
... 28 more
Merci d'avance pour votre aide.

Mickael