passage de paramètres username et password
en utilisant la synthaxe suivante dans config.xml
Code:
1 2 3 4 5 6
| <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"><value>${jdbc.driver}</value></property>
<property name="url"><value>${jdbc.url}</value></property>
<property name="username"><value>${jdbc.username}</value></property>
<property name="password"><value>${jdbc.password}</value></property>
</bean> |
comment passer les paramètres ${jdbc.driver},${jdbc.url},${jdbc.username} et
${jdbc.password} à la connection d'un utilisateur
Error creating bean with name 'connexion'
voici la nouvelle config
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
<bean id="connexion" class="mis.cinq.connexion.ConnectionDialog">
<property name="username" value="Alef [setter]"/>
<property name="password" value="Alef [setter1]"/>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/gestcar" />
<property name="username">
<bean id="connexion.username" class="org.springframework.beans.factory.config.PropertyPathFactoryBean"/>
</property>
<property name="password">
<bean id="connexion.password" class="org.springframework.beans.factory.config.PropertyPathFactoryBean"/>
</property>
</bean> |
et voici le code java
Code:
1 2 3 4 5 6 7 8 9 10
|
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring_config_jdbc.xml");
BeanFactory factory = applicationContext;
logger.info("LittleLeagueServices Initializing");
//BeanFactory tb = (BeanFactory)factory.getBean("setter");
ConnectionDialog tb = (ConnectionDialog)factory.getBean("connexion");
Assert.assertEquals("Alef [setter]", tb.getuserName());
//tb = (ConnectionDialog)factory.getBean("pwd");
Assert.assertEquals("Alef [setter1]", tb.getpassword()); |
Malheureusement je reçois l'erreur suivante
Code:
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
|
log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlApplicationContext).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'connexion' defined in class path resource [spring_config_jdbc.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'username' of bean class [mis.cinq.connexion.ConnectionDialog]: Bean property 'username' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1279)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1010)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:472)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
at java.security.AccessController.doPrivileged(Native Method)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:429)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:728)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:380)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at mis.cinq.view.TestSpringJdbc.main(TestSpringJdbc.java:30)
Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'username' of bean class [mis.cinq.connexion.ConnectionDialog]: Bean property 'username' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:801)
at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:651)
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:78)
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:59)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1276)
... 16 more
Java Result: 1
BUILD SUCCESSFUL (total time: 39 seconds) |