gpt4 book ai didi

java - Spring项目中的org.springframework.beans.NotWritablePropertyException

转载 作者:行者123 更新时间:2023-11-30 12:08:30 26 4
gpt4 key购买 nike

我正在尝试创建一个带有 CRUD 操作的 Spring 项目。我是 Spring 的新手,所以我需要一些帮助-

我在使用 post 方法提交表单时遇到以下错误-

Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'userDao' of bean class [com.cjc.service.RegistrationServiceImpl]: 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? at org.springframework.beans.BeanWrapperImpl.createNotWritablePropertyException(BeanWrapperImpl.java:231) at org.springframework.beans.AbstractNestablePropertyAccessor.setPropertyValue(AbstractNestablePropertyAccessor.java:423) at org.springframework.beans.AbstractNestablePropertyAccessor.setPropertyValue(AbstractNestablePropertyAccessor.java:280) at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:95) at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:75) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1514) ... 47 more

Jan 23, 2019 9:01:09 AM org.apache.catalina.core.StandardWrapperValve invoke SEVERE: Allocate exception for servlet spring org.springframework.beans.NotWritablePropertyException: Invalid property 'userDao' of bean class [com.cjc.service.RegistrationServiceImpl]: 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? at org.springframework.beans.BeanWrapperImpl.createNotWritablePropertyException(BeanWrapperImpl.java:231) at org.springframework.beans.AbstractNestablePropertyAccessor.setPropertyValue(AbstractNestablePropertyAccessor.java:423) at org.springframework.beans.AbstractNestablePropertyAccessor.setPropertyValue(AbstractNestablePropertyAccessor.java:280) at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:95) at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:75) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1514) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1226) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:305) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:301) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:196) at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1192) at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1116) at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:543) at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:305) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:301) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:196) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)

请在下面找到相同的 java 文件-

Controller -

package com.cjc.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.cjc.model.User;
import com.cjc.service.RegistrationService;

@Controller
public class RegistrationController {
@Qualifier("regservice")
private RegistrationService registrationService;

public void setRegistrationService(RegistrationService registrationService) {
this.registrationService = registrationService;
}

@RequestMapping(value="/registration", method=RequestMethod.POST)
public String Registration(@ModelAttribute User u) {
registrationService.addPerson(u);

return "RegSuccess";
}
}

服务-

package com.cjc.service;

import com.cjc.dao.UserDAO;
import com.cjc.model.User;

public class RegistrationServiceImpl implements RegistrationService {

private UserDAO userDao ;

public void setUserDao(UserDAO userDao) {
this.userDao = userDao;
}



@Override
public int addPerson(User u) {
// TODO Auto-generated method stub

return 0;
}

}

DAO 实现-

package com.cjc.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import com.cjc.model.User;
import com.cjc.util.HibernateUtil;

public class UserDaoImpl implements UserDAO{

@Override
public void addUser(User u) {
// TODO Auto-generated method stub
SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
Session session=sessionFactory.openSession();
Transaction transaction=session.beginTransaction();

session.save(u);

transaction.commit();
}

@Override
public void deleteUser(int id) {
// TODO Auto-generated method stub

}

@Override
public List<User> getAllUser() {
// TODO Auto-generated method stub
return null;
}

@Override
public User getUserById(int id) {
// TODO Auto-generated method stub
return null;
}

@Override
public void updteUser(User u, int id) {
// TODO Auto-generated method stub

}

}

Spring-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd

http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">

<mvc:annotation-driven />
<context:component-scan base-package="com.cjc.controller" />
<bean id="userDAO" class="com.cjc.dao.UserDaoImpl">
</bean>
<bean id="regservice" class="com.cjc.service.RegistrationServiceImpl">
<property name="userDao" ref="userDAO"></property>
</bean>



<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/jsp/"></property>
<property name="suffix" value=".jsp"></property>

</bean>

<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">

<property name="basenames" value="application" />
</bean>
</beans>

请帮我解决这个问题

最佳答案

您在用户模型中缺少 setter 和 getter。添加 setter 和 getter。

关于java - Spring项目中的org.springframework.beans.NotWritablePropertyException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54319725/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com