spring注入有点问题,求在线解答,拜托了

J2EE 码拜 8年前 (2016-05-21) 801次浏览
 报错: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>

<!– setter注入方式 –>
<bean id=”userDao” class=”com.train.dao.UserDao.jdbcTemplate”>
<property name=”jdbcTemplate” ref=”jdbcTemplate”/>
</bean>

解决方案

30

<property name=”dataSource” ref=“dataSource” />

10

引用:
Quote: 引用:

<property name=”dataSource” ref=“dataSource” />

跟这个没啥关系,后面那个注入ref引用的是上面那个jdbcTemplate。 val=dataSource 是对的。谢谢了。

应该就是一楼说的问题,你的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

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明spring注入有点问题,求在线解答,拜托了
喜欢 (0)
[1034331897@qq.com]
分享 (0)