开发者社区> 问答> 正文

Spring配置問題-java报错

"

<span style=""background-color:#e53333;"">1.   <bean id="userDao_msi" class="com.msi.dao.impl.UserDaoImpl">
  <property name="sessionFactory">
   <ref local="sessionFactory" />
  </property>
 </bean>
<span style=""background-color:#e53333;"">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>
別人的項目,這樣配置就可以,爲什麽我的跟這個差不多就不行呢


 

 

"

展开
收起
montos 2020-05-31 11:24:33 709 0
1 条回答
写回答
取消 提交回答
  • "

    是不是在你的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变量(父类引用指向子类实例)

    "
    2020-05-31 11:24:42
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Spring Cloud Alibaba - 重新定义 Java Cloud-Native 立即下载
The Reactive Cloud Native Arch 立即下载
JAVA开发手册1.5.0 立即下载