Spring,NotReadablePropertyException和Glassfish版本

Tob*_*obb 7 java glassfish spring-mvc

我正在使用一个使用Spring MVC的Web应用程序.

它在Glassfish 3.0.1上运行良好,但是当迁移到Glassfish 3.1时,它开始表现得很奇怪.有些页面只是部分显示,或者根本没有显示任何内容,并且在日志中有很多此类消息:

    [#|2012-08-30T11:50:17.582+0200|WARNING|glassfish3.1|javax.enterprise.system.container.web.com.sun.enterprise.web|_ThreadID=69;_ThreadName=Thread-1;|StandardWrapperValve[SpringServlet]: PWC1406: Servlet.service() for servlet SpringServlet threw exception
    org.springframework.beans.NotReadablePropertyException: Invalid property 'something' of bean class [com.something.Something]: Bean property 'something' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
        at org.springframework.beans.BeanWrapperImpl.getPropertyValue(BeanWrapperImpl.java:729)
        at org.springframework.beans.BeanWrapperImpl.getNestedBeanWrapper(BeanWrapperImpl.java:576)
        at org.springframework.beans.BeanWrapperImpl.getBeanWrapperForPropertyPath(BeanWrapperImpl.java:553)
        at org.springframework.beans.BeanWrapperImpl.getPropertyValue(BeanWrapperImpl.java:719)
        at org.springframework.validation.AbstractPropertyBindingResult.getActualFieldValue(AbstractPropertyBindingResult.java:99)
        at org.springframework.validation.AbstractBindingResult.getFieldValue(AbstractBindingResult.java:226)
        at org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:120)
        at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getBindStatus(AbstractDataBoundFormElementTag.java:178)
        at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getPropertyPath(AbstractDataBoundFormElementTag.java:198)
        at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getName(AbstractDataBoundFormElementTag.java:164)
        at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.writeDefaultAttributes(AbstractDataBoundFormElementTag.java:127)
        at org.springframework.web.servlet.tags.form.AbstractHtmlElementTag.writeDefaultAttributes(AbstractHtmlElementTag.java:421)
        at org.springframework.web.servlet.tags.form.TextareaTag.writeTagContent(TextareaTag.java:95)
        at org.springframework.web.servlet.tags.form.AbstractFormTag.doStartTagInternal(AbstractFormTag.java:102)
        at org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:79)
Run Code Online (Sandbox Code Playgroud)

错误消息不正确,因为有问题的属性没有setter-method(通过构造函数获取其值).但就像我说的那样,使用Glassfish 3.0.1时,只有在使用Glassfish 3.1的新服务器上使用它时才会出现问题.

有谁知道Glassfish版本中是否有可能导致此问题的内容?或者它是新服务器上缺少的某种配置?

一些代码:

控制器:

@ModelAttribute
public SomethingContainer retriveSomethingContainer(@PathVariable final long id {
    return somethingContainerDao.retrieveSomethingContainer(id);       
}

@InitBinder("somethingContainer")
public void initBinderForSomething(final WebDataBinder binder) {
    binder.setAllowedFields(new String[] {
        "something.title",
        "something.description",
    });
}
Run Code Online (Sandbox Code Playgroud)

SomethingContainer:

@Embedded
private final Something something = new Something();

public Something getSomething() {
    return something;
}
//no setter

public String getDescription() {
    return something.getDescription();
}
Run Code Online (Sandbox Code Playgroud)

更新:

重启Glassfish实际上可以暂时解决问题.我怀疑它可能与自定义绑定器的加载有关,我们遇到了内存错误的一些问题,我认为这与它有关,但是已经修复但没有解决这个问题.

更新2:

在3.0.1服务器上,其中一个jvm参数是-client.在3.1服务器上,它是-server.我们将它更改为-client,这使得错误的频率下降了很多,它每隔一天发生一次-server,花了两周的时间才发生-client.

更新3:

有关服务器的一些信息(如果请求,可以添加更多信息..)

Server1(工作的):

Windows Server 2003
Java jdk 6 build 35
Glassfish 3.0.1 build 22
-xmx 1024m
Run Code Online (Sandbox Code Playgroud)

Server2(有问题的那个):

Windows Server 2008 64-bit
Java jdk 6 build 31
Glassfish 3.1 build 43
-xmx 1088m
-xms 1088m
Run Code Online (Sandbox Code Playgroud)

我们使用的是Spring 3.1.0版.

更新4:

我通过将jsp中的字段重命名为modelattribute中不存在的内容来重新创建错误.

但是,更重要的是,我注意到了一些事情:系统无法找到getter的字段通常是modelattribute中引用的字段的超类.继续我的例子,SomthingContainer真的是这样的:

public class SuperSomethingContainer {
    [...]
    private Something something;
    public Something getSomething() {
        return something;
    }
}

public class SomethingContainer extends SuperSomethingContainer {
    [...]
}
Run Code Online (Sandbox Code Playgroud)

控制器中的引用保持原样,因此它引用了相关对象的超类中的字段.

更新5:

发生错误后,我尝试使用调试器连接到生产服务器.我在一个控制器方法的return语句上放了一个断点,返回带有错误的对象,并试图查看当时是否可以访问该字段.我可以,所以问题必须在Spring MVC /生成的jsp-classes中.

(此外,错误的字段是"someobject.something [0] .somethingelse [0]"类型,但是当somethingelse-list为空时,没有错误!对我来说,这暗示它不知何故不能找到列表的get方法(?))

更新6:

似乎问题与jsps生成Java类有关.我们在部署时没有使用预编译jsps,因此在首次使用时会对它们进行编译.第一次访问页面时,会出现问题,并且编译了jsp.我也注意到一旦出现问题,之后编译的jsps都会出错.我保留了一些问题生成的java文件,在下次重启时我会将它们与工作文件进行比较.越来越近 :)

更新7:

比较导致错误的编译的jsp java文件与没有编译的jsp java文件,并没有区别.所以有点离开了.

所以,我现在知道离开控制器的Java对象很好(用调试器检查),并且从jsp生成的java类很好.所以它必须介于两者之间,现在我需要找出...

更新8:

另一轮调试,并将问题缩小了一些.事实证明,spring会对属于各种类的属性进行一些缓存.在org.springframework.beans.BeanWrapperImpl中,方法getPropertyValue,有以下内容:

private Object getPropertyValue(PropertyTokenHolder tokens) throws BeansException {
    String propertyName = tokens.canonicalName;
    String actualName = tokens.actualName;
    PropertyDescriptor pd = getCachedIntrospectionResults().getPropertyDescriptor(actualName);
    if (pd == null || pd.getReadMethod() == null) {
        throw new NotReadablePropertyException(getRootClass(), this.nestedPath + propertyName);
    }
Run Code Online (Sandbox Code Playgroud)

问题是cachedIntrospectionResults不包含有问题的属性,但它包含该类的所有其他属性.将需要挖掘更多,以试图找出它丢失的原因,如果它从一开始就丢失了,或者它是否会在某个地方丢失.

另外,我注意到缺少的属性是那些没有setter,只有getter的属性.并且,它似乎是上下文感知,如堆栈跟踪所示.因此,在访问一个页面时找不到属性并不意味着在访问另一个页面时它不可用.

更新9:

另一天,更多的调试.其实找到了一些好东西.前一个代码块中的getCachedIntrospectionResults()调用被调用CachedIntrospectionResults#forClass(theClassInQuestion).这返回了一个CachedIntrospectionResults对象,该对象包含远离所有预期属性(21个中的11个).进入forClass方法,我发现:

static CachedIntrospectionResults forClass(Class beanClass) throws BeansException {
    CachedIntrospectionResults results;
    Object value = classCache.get(beanClass);
    if (value instanceof Reference) {
        Reference ref = (Reference) value;
        results = (CachedIntrospectionResults) ref.get();
    }
    else {
        results = (CachedIntrospectionResults) value;
    }
    if (results == null) {
    //build the CachedIntrospectionResults, store it in classCache and return it.
Run Code Online (Sandbox Code Playgroud)

事实证明,返回的CachedIntrospectionResults是由classCache.get(beanClass)找到的.因此,存储在classCache中的内容已损坏/不包含它应该包含的所有内容.我在classCache.get(beanClass)-line上放了一个断点,并尝试通过调试器运行它:

classCache.put(beanClass,null);

当允许方法完成并重建CachedIntrospectionResults时,事情又开始起作用了.因此,如果允许重建它,那么存储在classCache中的内容与将要创建的内容不同步.这是否是由于第一次构建时出现问题,或者如果classCache在我当前不知道的线路上的某个地方被破坏了.

我开始怀疑这与类加载器有关,因为我之前遇到的问题是由于更新Glassfish时类加载器的工作方式发生了变化.

Tob*_*obb 2

我让我的一位同事调查了这个错误,他能够在单元测试中重新创建它。这是通过调用为类构建 CachedIntroSpectionResults 的方法来完成的,同时通过将字符串添加到内存(内存设置非常低)来给 jvm 施加压力。这种方法导致 20/30000 次失败。

至于其原因,我只得到了口头解释,所以我不知道所有细节,但大概是这样的:Java有自己的内省结果,并且这些结果被Spring包装起来。问题是 java 结果使用软引用,这使得它们容易被垃圾收集。因此,当 Spring 在垃圾收集器运行的同一时间围绕这些软引用构建其包装器时,它实际上清除了 Spring 正在使用的一些基础,导致属性“丢失”。

解决方案似乎是从 Spring 3.1.0.RELEASE 升级到 Spring 3.1.3.RELEASE。这里有一些变化,Spring 在确定类的属性时不再包装软引用(软引用仅在极少数特殊情况下使用,而不是一直使用)。升级Spring版本后,通过单元测试并没有重现该错误,有待实际使用中看看是否会出现这种情况。

更新:几周过去了,没有任何错误迹象。所以更新 Spring 版本有效:)