serialVersionUID缺少时,Eclipse会发出警告.
可序列化类Foo不声明long类型的静态最终serialVersionUID字段
serialVersionUID它是什么以及为什么重要?请显示缺失serialVersionUID会导致问题的示例.
什么是JavaBean,为什么需要它?既然我可以使用类和接口结构创建所有应用程序?我为什么需要豆子?你能给我一些例子,其中bean是必不可少的而不是类和接口吗?
请在下面的上下文中解释bean的必要性:
我的DAO实现在使用Tomcat7启动服务器时抛出了一个不可序列化的异常.知道是什么原因引起的吗?我的其他DAO都没有这样做.
这是班级:
package com.project.dao;
import java.util.List;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.HibernateTemplate;
import com.project.model.User;
public class UserDAOImpl implements UserDAO {
private HibernateTemplate hibernateTemplate;
public void setSessionFactory(SessionFactory sessionFactory) {
this.hibernateTemplate = new HibernateTemplate(sessionFactory);
}
@Override
public void saveUser(User user) {
hibernateTemplate.saveOrUpdate(user);
}
@Override
@SuppressWarnings("unchecked")
public List<User> listUser() {
return hibernateTemplate.find("from User");
}
@Override
@SuppressWarnings("unchecked")
public User getUserByID(long userID) {
List<User> users= hibernateTemplate.find("from User where id = '" + userID + "'");
return users.size() > 0 ? users.get(0) : null;
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的配置:
<beans …Run Code Online (Sandbox Code Playgroud) 我有以下托管bean:
@ApplicationScoped
public class ApplicationBean {
// ...
}
Run Code Online (Sandbox Code Playgroud)
@SessionScoped
public class SessionBean implements Serializable {
@ManagedProperty("#{applicationBean}")
private ApplicationBean applicationBean;
// ...
}
Run Code Online (Sandbox Code Playgroud)
这将部署到具有多个节点的服务器群集.当HTTP会话在另一个节点上序列化时会发生什么?
ApplicationBean未序列化,因为它没有实现Serializable.它会被重新注入@ManagedProperty吗?或者它会以某种方式实际序列化?
在Tomcat 7上部署我的webApplication时,我将获得下面的控制台输出.重新启动服务器两次或三次后,它可以正常工作.
我正在使用JSF,Tomcat和businesslogic部分的RMI连接(在这里应该没关系?)
@ViewScoped托管bean中的@EJB导致java.io.NotSerializableException - 这里我读到了序列化.但是在这种情况下,客户端状态保存被激活,这在我的webApp中并非如此(就我的web.xml告诉我而言).那么我的视图编译ManagedBeans的序列化是必要的还是另一个失败?
这是我的启动控制台输出(Sry中的德语部分):
28.01.2011 20:25:05 org.apache.catalina.core.AprLifecycleListener init
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Programme\Java\jdk1.6.0_21\jre\bin;.;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:/Programme/Java/jre6/bin/client;C:/Programme/Java/jre6/bin;C:/Programme/Java/jre6/lib/i386;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Programme\Gemeinsame Dateien\Lenovo;C:\Programme\ATI Technologies\ATI.ACE\Core-Static;C:\Programme\Java\jdk1.6.0_20;C:\StudiPortal\TortoiseSVN\bin;C:\StudiPortal\apache-maven-2.0.11\bin;C:\Programme\Intel\WiFi\bin\;C:\Programme\ThinkPad\ConnectUtilities;C:\Programme\GTK2-Runtime\bin;C:\Programme\OpenVPN\bin
28.01.2011 20:25:05 org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNUNG: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:shapeyourbody' did not find a matching property.
28.01.2011 20:25:06 org.apache.coyote.http11.Http11Protocol init
INFO: Initializing Coyote HTTP/1.1 on http-8080
28.01.2011 20:25:06 org.apache.coyote.ajp.AjpProtocol init
INFO: Initializing Coyote AJP/1.3 on ajp-8009
28.01.2011 20:25:06 org.apache.catalina.startup.Catalina load
INFO: Initialization …Run Code Online (Sandbox Code Playgroud) 我一直在用@ViewScoped托管bean敲打头脑.我正在使用primeface的"schedule"组件来显示一些事件.当用户单击特定按钮时,使用ajax调用viewscoped bean中的方法,但每次我得到"java.io.NotSerializableException"时,如果我更改managed-bean范围以请求问题消失.
我究竟做错了什么?有任何想法吗?
这是我的托管bean:
@ManagedBean(name = "schedule")
@ViewScoped
public class ScheduleMBean implements Serializable {
@EJB
private CongeBean congeBean;
@ManagedProperty(value = "#{sessionBean}")
private SessionMBean sessionBean;
private DefaultScheduleModel visualiseurConges = null;
public ScheduleMBean(){
}
@PostConstruct
public void init() {
if(visualiseurConges == null){
visualiseurConges = new DefaultScheduleModel();
}
}
public void updateSchedule(){
visualiseurConges.addEvent(new DefaultScheduleEvent("test" , new Date(), new Date() ));
}
public void setVisualiseurConges(DefaultScheduleModel visualiseurConges) {
this.visualiseurConges = visualiseurConges;
}
public DefaultScheduleModel getVisualiseurConges() {
return visualiseurConges;
}
public void setSessionBean(SessionMBean sessionBean) {
this.sessionBean …Run Code Online (Sandbox Code Playgroud) 我在2个系统(笔记本电脑)中有相同的应用程序,但它在一个但不在另一个系统中工作.我在另一个系统中得到以下错误.我也发布了下面的代码.我想做的是使用调用不同托管bean方法的按钮级联下拉列表,以及在数据库中添加记录的placeOrder按钮.但是我在页面时遇到以下错误装载
WARNING: Setting non-serializable attribute value into ViewMap: (key: stockOrderBean, value class: beans.stockOrderBean)
SEVERE: Error Rendering View[/ClientTemplate/stockTrade.xhtml]
java.io.NotSerializableException: beans.stockOrderBean
WARNING: JSF1087: Unable to generate Facelets error page as the response has already been committed.
SEVERE: javax.faces.FacesException: beans.stockOrderBean
Run Code Online (Sandbox Code Playgroud)
xhtmlcode:
<h:outputText value="Exchange :"/>
<p:selectOneMenu value="#{stockOrderBean.exchange}" style="width: 200px">
<f:selectItem itemLabel="Select Exchange"/>
<f:selectItem itemLabel="NSE" itemValue="nse"/>
<f:selectItem itemLabel="BSE" itemValue="bse"/>
<p:ajax update="sym" listener="#{stockOrderBean.wow}"/>
</p:selectOneMenu>
<h:outputText value="Select ScripSymbol :"/>
<p:selectOneMenu value="#{stockOrderBean.scripID}" style="width: 200px" id="sym">
<f:selectItem itemLabel="Select scrip"/>
<f:selectItems var="scrip" value="#{stockOrderBean.sl}" itemLabel="#{scrip.scripSymbol}" itemValue="#{scrip.scripID}"/>
</p:selectOneMenu>
<p:commandButton value="Get Quote" actionListener="#{stockOrderBean.equity.setQuote}" …Run Code Online (Sandbox Code Playgroud) 我正在使用此代码从JSF页面下载文件.调用此Java时:
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head></h:head>
<h:body>
<ui:composition>
<title>example</title>
<meta charset="utf-8"></meta>
</ui:composition>
</h:body>
</html>
Run Code Online (Sandbox Code Playgroud)
豆:
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.context.FacesContext;
import javax.faces.view.ViewScoped;
import javax.inject.Named;
import javax.servlet.http.HttpServletResponse;
@Named
@ViewScoped
public class Download implements Serializable {
private String path = "/opt";
private List<directoryListObj> dataList;
private List<String> items = new ArrayList<>();
private String zone;
@PostConstruct …Run Code Online (Sandbox Code Playgroud) 我们在flex中有一个应用程序,它部署在负载均衡的Tomcat 6.0上(有一个负载均衡器将请求传递给2个tomcat服务器).
访问应用程序时出现404错误.在挖掘tomat日志时,我们发现了以下错误
记录跟踪
2013-01-17 10:42:54,148 org.apache.catalina.session.ManagerBase - IOException while loading persisted sessions: java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException: bean.Login
java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException: bean.Login
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1332)
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1946)
..
2013-01-17 10:43:04,135 org.apache.catalina.session.ManagerBase - Exception loading sessions from persistent storage
java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException: bean.Login
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1332)
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1946)
…
Caused by: java.io.NotSerializableException: bean.Login
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1164)
Run Code Online (Sandbox Code Playgroud)
原因
出现此错误的可能原因是Tomcat尝试在关闭时序列化所有活动会话的完整对象图,然后在您重新启动时尝试恢复它们.这方面的关键是Tomcat使用"普通"java对象序列化,这要求所有对象都是Serializable.
我们已经在Spring中映射了Login bean,如下所示
<bean id="currLogin" class="bean.Login" scope="session">
<aop:scoped-proxy />
</bean>
Run Code Online (Sandbox Code Playgroud)
补救
短期
在弹出服务器后,我们可以在没有错误的情况下登录应用程序.
长期
请告诉我们解决此问题的方法?