1. <bean id="userDao_msi" class="com.msi.dao.impl.UserDaoImpl">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
2. <bean id="gradeDao_msi" class="com.msi.dao.impl.GradeDaoImpl">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
3. <bean id="UserService_msi" class="com.msi.service.impl.UserServiceImpl">
<property name="userDao_msi">
<ref local="userDao_msi" />
</property>
</bean>
未添加3. 之前程序測試無異常出現,
添加3. 這個配置之後,出現如下異常,請教下如何修改下3. 的配置,從而避免異常的出現,請教如何修改,謝謝
異常描述
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'UserService_msi' defined in class path resource [applicationContext.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'userDao_msi' of bean class [com.msi.service.impl.UserServiceImpl]: Bean property 'userDao_msi' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
----------------------------------------------------------------
以下的配置方式是可行的。為什麽上面的1. 2. 3. 卻不可以呢??
<bean id="dao_msi" class="com.msi.dao.impl.DAOImpl">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<bean id="service_msi" class="com.msi.service.impl.ServiceImpl">
<property name="dao">
ref local="dao_msi" />
</property>
</bean>
別人的項目,這樣配置就可以,爲什麽我的跟這個差不多就不行呢
是不是在你的service中没有声明dao
######service 中的interface 與 dao中的interface 是一模一樣的######UserServiceImpl里userDao_msi属性不存在,或者这个属性的set方法不存在,或者set方法名称有问题######<bean id="userDao_msi" class="com.msi.dao.impl.UserDaoImpl"> 這樣的話,這個不就是表示存在???######Invalid property 'userDao_msi' of bean class [com.msi.service.impl.UserServiceImpl]: Bean property 'userDao_msi' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
Service类里dao对象有正确的get sert方法吗?
######<bean id="UserService_msi" class="com.msi.service.impl.UserServiceImpl">
<property name="userDao_msi">
<ref local="userDao_msi" />
</property>
</bean>
注入property name="userDao_msi" 时 ,需要com.msi.service.impl.UserServiceImpl有一个setter方法,方法名为setUserDao_msi,才能成功注入
缺少set/get方法
###### 哥們;按照你說的配置之後,異常消失
不過,如果我這樣測試就出現Exception in thread "main" java.lang.ClassCastException: $Proxy11 cannot be cast to com.msi.service.impl.UserServiceImpl
at com.msi.service.impl.UserServiceImpl.main(UserServiceImpl.java:33)
--------------------------------------------------------
配置部份
<bean id="userDao_msi" class="com.msi.dao.impl.UserDaoImpl">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<bean id="userService" class="com.msi.service.impl.UserServiceImpl">
<property name="userService_msi">
<ref local="userDao_msi" />
</property>
</bean>
代碼--------------------------------------------
import com.msi.dao.UserDao;
import com.msi.dao.impl.UserDaoImpl;
import com.msi.model.User;
import com.msi.service.UserService;
public class UserServiceImpl implements UserService {
public UserDao userService_msi;
public UserDao getuserService_msi() {
return userService_msi;
}
public void setuserService_msi(UserDao userService_msi) {
this.userService_msi = userService_msi;
}
@Override
public User login(User user) throws Exception {
// TODO Auto-generated method stub
return userService_msi.login(user);
}
public static void main(String args[]) throws Exception {
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"applicationContext.xml");
UserServiceImpl dao = (UserServiceImpl) ctx.getBean("userService");
User user = new User("admin", "admin");
User u = dao.login(user);
System.out.println(u);
}
}
這樣就出現最上面的異常,這是爲什麽呢
UserServiceImpl dao = (UserServiceImpl) ctx.getBean("userService");
改成UserService dao = (UserService) ctx.getBean("userService");
Spring在实例化的是UserServiceImpl的一个代理对象、实现了UserService接口。所以要有UserService声明dao变量(父类引用指向子类实例)
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。