Spring IOC配置问题

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring IOC配置问题相关的知识,希望对你有一定的参考价值。

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<constructor-arg ref="dataSource" />
</bean>
<bean id="userDao" class="com.qywz.dao.impl.UserDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
<bean id="userService" class="com.qywz.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao" />
</bean>
这是我的配置,Service层是这样子写的:
private UserDao userDao;

public UserDao getDao()
return userDao;


public void setDao(UserDao userDao)
this.userDao = userDao;

报错:
Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'userDao' of bean class [com.qywz.service.impl.UserServiceImpl]: Bean property 'userDao' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
求大侠们解决下谢谢

参考技术A .java代码:
package ioc.first;

import ioc.first.service.StoreService;

import java.util.Map;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test

public static void main(String[] args)
// FileSystemResource("D:/a.txt");
// Resource resource=new ClassPathResource("ioc/first/applicationContext.xml");
// BeanFactory context=new XmlBeanFactory(resource);

ApplicationContext context=new ClassPathXmlApplicationContext("ioc/first/applicationContext_autowire.xml");
StoreService service=(StoreService)context.getBean("storeService") ;
service.submitOrder();

// Map env=(Map)context.getBean("env");
// System.out.println(env.get("JAVA_HOME"));
//
// System.out.println(context.getBean("env2"));




applicationContext_autowire.xml 代码:

<?xml version="1.0" encoding="UTF-8"?>
<beans default-autowire="byType"
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

<bean id="itemDao" class="ioc.first.dao.ItemDaoImpl"/>
<bean id="orderDao" class="ioc.first.dao.OrderDaoImpl"/>
<bean id="oDao1" class="ioc.first.dao.OrderDaoImpl"/>

<bean id="storeService"
class="ioc.first.service.StoreServiceImpl"
autowire="autodetect">
</bean>

</beans>

上面java代码中的//后面是注释。可以不用管 是我用来做测试的.追问

谢谢你,你这是木有回答到问题所在!不过我已经解决了,还是非常感谢你!

参考技术B getUserDao
setUserDao注意方法名称。本回答被提问者采纳

以上是关于Spring IOC配置问题的主要内容,如果未能解决你的问题,请参考以下文章

Spring Day01 Spring 框架概述以及Spring中基于XML的IOC配置

[Spring实战系列]配置Spring IOC容器的Bean

Spring学习IOC容器配置bean:定义与实例化

Spring(概念和ioc入门ioc操作xml配置文件)

Spring框架[一]——spring概念和ioc入门(ioc操作xml配置文件)

Spring框架学习使用ioc注解方式配置bean