Bonjour,
Je suis entrain d'apprendre l'intégration de Spring dans un projet J2EE.
Mais je n'arrive pas à faire mon premier exercice.
Mon fichier de config MySpringConf.xml :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
 
<bean id="myDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="com.mysql.jdbc.Driver" />
		<property name="url" value="jdbc:mysql://localhost/minibankdb" />
		<property name="username" value="root" />
		<property name="password" value="admin" />
	</bean>	
 
 
	<bean id="compteDaoJdbc" class="minibank.dao.impl.JdbcCompteDAOImpl">		
		<property name="compteDao" ref="myDataSource"/>
	</bean>
La class d'implemenation de la DAO :
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
 
 
public class JdbcCompteDAOImpl extends JdbcDaoSupport implements CompteDAO {
 
 
	CompteDAO dao = null;
 
	public void setCompteDao(CompteDAO dao) {
		this.dao = dao;
	}
public Compte getCompteByNumJdbc(Long num) {
		Connection cn = null;
		Compte compte= null;
		cn = this.getConnection();		
		String req = "Select from TCompte where numero = ?";
		try {
			PreparedStatement pst = cn.prepareStatement(req);
			pst.setLong(1, num);
			ResultSet rs = pst.executeQuery();
 
			if(rs.next()){
				compte = new Compte();
				compte.setNumero(num);
				compte.setLibelle(rs.getString("libelle"));
				compte.setSolde(rs.getDouble("solde"));
			}
			rs.close();
			pst.close();
		} catch (SQLException e) {			
			e.printStackTrace();
			throw new DataRetrievalFailureException(e.getMessage());
		}finally{
			this.releaseConnection(cn);
		}		
 
		return compte;
	}
 
}
La fonction 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
 
public class Main {
 
	private static CompteDAOdao;
 
	public static void main(String[] args) {
		ApplicationContext ctx = MySpringContextUtil.getMySpringContext();
		dao = (CompteDAO) ctx.getBean("compteDaoJdbc"); 
		Compte cpt= dao.getCompteByNumJdbc(1L);
		System.out.println(cpt.getLibelle());	
 
	}
 
}
La fonction de chargement du fichier 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
 
public class MySpringContextUtil {
 
	private static ApplicationContext mySpringContext = null; // singleton
 
	public static ApplicationContext getMySpringContext() {
 
		if (mySpringContext == null) {
 
			mySpringContext = new ClassPathXmlApplicationContext(
					new String[] { "mySpringConf.xml" });
		}
		return mySpringContext;
	}
}
L'erreur est la suivante :
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
 
 
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'compteDaoJdbc' defined in class path resource [mySpringConf.xml]: Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'org.springframework.jdbc.datasource.DriverManagerDataSource' to required type 'minibank.dao.CompteDAO' for property 'compteDao'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [org.springframework.jdbc.datasource.DriverManagerDataSource] to required type [minibank.dao.CompteDAO] for property 'compteDao': no matching editors or conversion strategy found
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:521)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:450)
	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:290)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:287)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:189)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:562)
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:871)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:423)
	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:93)
	at test.MySpringContextUtil.getMySpringContext(MySpringContextUtil.java:14)
	at test.Main.main(Main.java:17)
Caused by: org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'org.springframework.jdbc.datasource.DriverManagerDataSource' to required type 'minibank.dao.CompteDAO' for property 'compteDao'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [org.springframework.jdbc.datasource.DriverManagerDataSource] to required type [minibank.dao.CompteDAO] for property 'compteDao': no matching editors or conversion strategy found
	at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:462)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.convertForProperty(AbstractAutowireCapableBeanFactory.java:1354)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1313)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1067)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:511)
	... 12 more
Caused by: java.lang.IllegalStateException: Cannot convert value of type [org.springframework.jdbc.datasource.DriverManagerDataSource] to required type [minibank.dao.CompteDAO] for property 'compteDao': no matching editors or conversion strategy found
	at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:289)
	at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:154)
	at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:452)
	... 16 more
Merci pour votre aide.