报错:org.springframework.beans.factory.BeanCreationException: Error creating bean with name “jdbcTemplate” defined in class path resource [spring-config.xml]: Initialization of bean failed;
代码:
public class UserText {
static ApplicationContext acx = new ClassPathXmlApplicationContext(“spring-config.xml”);
static UserDao userDao = (UserDao) acx.getBean(“jdbcTemplate”);
static User user = new User();
public static void main(String[] args) {
xinZeng();
}
public static void xinZeng(){
user.setId(“1”);
user.setName(“ronghao”);
user.setEmail(“ronghao@qq.com”);
user.setAge(12);
user.setSex(“0”);
user.setPhone(“15029001645″);
userDao.create(user);
}
spring配置:
<bean id=”jdbcTemplate” class=”org.springframework.jdbc.core.JdbcTemplate”>
<property name=”dataSource” value=”dataSource”/>
</bean>
代码:
public class UserText {
static ApplicationContext acx = new ClassPathXmlApplicationContext(“spring-config.xml”);
static UserDao userDao = (UserDao) acx.getBean(“jdbcTemplate”);
static User user = new User();
public static void main(String[] args) {
xinZeng();
}
public static void xinZeng(){
user.setId(“1”);
user.setName(“ronghao”);
user.setEmail(“ronghao@qq.com”);
user.setAge(12);
user.setSex(“0”);
user.setPhone(“15029001645″);
userDao.create(user);
}
spring配置:
<bean id=”jdbcTemplate” class=”org.springframework.jdbc.core.JdbcTemplate”>
<property name=”dataSource” value=”dataSource”/>
</bean>
<!– setter注入方式 –>
<bean id=”userDao” class=”com.train.dao.UserDao.jdbcTemplate”>
<property name=”jdbcTemplate” ref=”jdbcTemplate”/>
</bean>
解决方案
30
<property name=”dataSource” ref=“dataSource” />
10
应该就是一楼说的问题,你的datasource怎么定义的?
10
两个问题:
static ApplicationContext acx = new ClassPathXmlApplicationContext(“spring-config.xml”);
static UserDao userDao = (UserDao) acx.getBean(“jdbcTemplate”);
这里获取的是jdbctemplate对象吧,应该直接getbean(userdao)吧
<bean id=”jdbcTemplate” class=”org.springframework.jdbc.core.JdbcTemplate”>
<property name=”dataSource” value=”dataSource”/>
</bean>
你这样注入的是data属性注入了一个字符串datasource
static ApplicationContext acx = new ClassPathXmlApplicationContext(“spring-config.xml”);
static UserDao userDao = (UserDao) acx.getBean(“jdbcTemplate”);
这里获取的是jdbctemplate对象吧,应该直接getbean(userdao)吧
<bean id=”jdbcTemplate” class=”org.springframework.jdbc.core.JdbcTemplate”>
<property name=”dataSource” value=”dataSource”/>
</bean>
你这样注入的是data属性注入了一个字符串datasource