org.springframework.beans.NotWritablePropertyException:无效的属性
Posted
技术标签:
【中文标题】org.springframework.beans.NotWritablePropertyException:无效的属性【英文标题】:org.springframework.beans.NotWritablePropertyException: Invalid property 【发布时间】:2017-05-01 06:06:35 【问题描述】:您好,我是 Spring 新手,目前我正在使用 Maven 运行我的应用程序
但是当我运行我的项目时,我遇到了如下异常
bean 类 [com.ensis.spring.MovieLister] 的“年份”:bean 属性“年份”不可写或设置方法无效。 setter 的参数类型是否与 getter 的返回类型匹配?
主要:
public class Main
public static void main(String[] args)
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
MovieLister movieLister = (MovieLister)ctx.getBean("lister");
movieLister.printMoviesByYear();
电影:
private int year;
private String name;
public Movie(int year, String name)
super();
this.year = year;
this.name = name;
public int getYear()
return year;
public void setYear(int year)
this.year = year;
public String getName()
return name;
public void setName(String name)
this.name = name;
电影查找器:
public class MovieFinder
private static List<Movie>moviesList;
static
moviesList.add(new Movie(1990, "A"));
moviesList.add(new Movie(1991, "B"));
moviesList.add(new Movie(1992, "C"));
moviesList.add(new Movie(1993, "D"));
moviesList.add(new Movie(1994, "E"));
moviesList.add(new Movie(1995, "F"));
public List<Movie>findMoviesByYear(int year)
List<Movie>findMovies=new ArrayList<Movie>();
for(Movie movie:moviesList)
if(year==movie.getYear())
findMovies.add(movie);
return findMovies;
电影列表:
public class MovieLister
private int year;
private MovieFinder movieFinder;
public void printMoviesByYear()
List<Movie>found = movieFinder.findMoviesByYear(year);
for(Movie movie:found)
System.out.println("So movies are======>"+movie.getName());
applicationContext.xml
<?xml version = "1.0" encoding = "UTF-8"?>
<beans 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-3.0.xsd">
<bean id="lister" class ="com.ensis.spring.MovieLister" autowire="byType">
<property name="year" value="1991"/>
</bean>
<bean id="finder" class="com.ensis.spring.MovieFinder"/>
</beans>
【问题讨论】:
请尝试在 MovieLister 类中添加年份的 getter 和 setter 方法并检查 嗨@avinash readdy java.lang.NullPointerException at com.ensis.spring.MovieFinder.向 MovieLister 添加一个 setter:
public void setYear(final int year)
this.year = year;
这是必需的,因为成员变量 year 是私有的。
旁注: 如果 MovieLister 实例需要一年或任何其他成员变量,我强烈建议创建一个设置这些变量的构造函数。
【讨论】:
com.ensis.spring.MovieFinder 处的 java.lang.NullPointerException。以上是关于org.springframework.beans.NotWritablePropertyException:无效的属性的主要内容,如果未能解决你的问题,请参考以下文章
REST API:org.springframework.beans.factory.UnsatisfiedDependencyException:
Spring security-org.springframework.beans.factory.BeanCreationException:创建名为“org.springframework.sec
org.springframework.beans.factory.UnsatisfiedDependencyException:
没有实现 [org.springframework.beans.factory.xml.NamespaceHandler] 接口
Spring Boot 错误 org.springframework.beans.factory.UnsatisfiedDependencyException
org.springframework.beans.factory.UnsatisfiedDependencyException:创建 bean 时出错