简体   繁体   中英

JSF ManagedProperty not working for class

Sorry for my English. I want to set @ManagedProperty for class TaskBO , but it is not works. TaskMB class:

@ManagedBean(name="taskMB")
@RequestScoped
public class TaskMB implements Serializable {

    @ManagedProperty(value="#{TaskBO}")
    public TaskBO taskBO;

    public TaskBO getTaskBO() {
        return this.taskBO;
    }

    public void setTaskBO(TaskBO taskBO){
        this.taskBO = taskBO;
    }
    //...
}

It prints the error:

javax.servlet.ServletException: Unable to set property taskBO for managed bean taskMB
javax.el.ELException: java.lang.IllegalArgumentException: Cannot convert com.otv.model.bo.TaskBO@6c80b8 of type class $Proxy135 to class com.otv.model.bo.TaskBO

But if I add interface ITaskBO , that it is works:

@ManagedProperty(value="#{TaskBO}")
public ITaskBO taskBO;

public ITaskBO getTaskBO() {
    return this.taskBO;
}

public void setTaskBO(ITaskBO taskBO){
    this.taskBO = taskBO;
}

Why not work @ManagedProperty with the class TaskBO ?

Is best pratice wire interface instead of concrete class to prevent the problem you encountered.

Cannot convert com.otv.model.bo.TaskBO@6c80b8 of type class $Proxy135 to class com.otv.model.bo.TaskBO

Often Spring's managed object are proxied and a java proxy can be casted ONLY to interface and not to concrete class; the error above is generated because:

  1. TaskBO object is managed by Spring and proxied to an object of type $Proxy135 (the real type of your object now is not really concrete class TaskBO but a proxy you can cast to ITaskBO, the $Proxy135)
  2. you are trying to do some like public TaskBO taskBO = (TaskBO)$Proxy135; but cast a proxy to concrete class is impossible
  3. The right way is to write public ITaskBO taskBO = (ITaskBO)$Proxy135; and this works because a proxy can be cast only to interface

Avoid - as much as possible - use of concrete class in favor of interface.

In addiction you can look here if you are mixing configuration how described in linked question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM