我正在学习计算机科学课,其中的作业样板代码是一个Java框架,其中大多数类(或它们的超类)扩展了Serializable.最终发生的事情是VSCode向我抱怨
The serializable class [insert class name here] does not declare a static final serialVersionUID field of type long"
几乎所有的起始代码.我知道在其他IDE(例如IntelliJ和Eclipse)中,可以针对所有Java项目禁止此特定警告.VSCode中的等效操作是什么?我安装了Java语言支持包.
以下是我无法声明serialVersionUID或使用@SuppressWarnings的原因:
这将迫使我修改我不允许修改的代码.教授只希望学生实施框架的某些领域.
我需要将这些更改改为大约30个不太理想的类.
假设我有一个包含许多字段的User类:
public class User {
public Integer id;
public String name;
public String username;
public Integer age;
public Address address;
public String phoneNumber;
public String email;
}
Run Code Online (Sandbox Code Playgroud)
但我并不总是需要前端的所有用户属性。每个屏幕只需要一些用户字段。为每个屏幕创建 DTO 类是一个好习惯吗,因为它们访问不同的属性?像这样:
class UserToScreenADTO implements Serializable {
public String name;
public String email;
}
class UserToScreenBDTO implements Serializable {
public String phoneNumber;
public Address address;
}
class UserToScreenCDTO implements Serializable {
public Integer id;
public String username;
public String email;
}
Run Code Online (Sandbox Code Playgroud) 我几乎没有关于连接和弹簧交易的问题.
当在事务中执行多个执行DML和DDL操作的方法时,spring是否使用相同的连接实例(传播级别为REQUIRED)?我已经读过它确实保持了相同的连接,但不知道为什么以及如何在技术上做到这一点?在解释如何提供spring源代码中的任何提示时,它会有所帮助.
如果我使用Serializable作为隔离级别,那么使用Spring声明事务会弹出以确保在该方法或从原始事务方法调用的任何其他方法中执行数据库操作时始终使用一个连接吗?
考虑到这个话题,在使用Spring Transactions时应该记住哪些要点?
任何关于这个主题的想法/帮助将不胜感激.谢谢.
更新1 - 对不起,我写了可序列化的传播级别而不是隔离级别.纠正了它.
我们知道memento模式没有违反封装,捕获和外化对象的内部状态,并且可以在不知道原始状态的情况下稍后进行回收.
我的问题在这里java.io.Serializable是如何进入这种模式的,因为当我们序列化任何私有变量并将对象状态同时写入文件时,私有变量的值对世界开放并且封装似乎在这里失败.
我正在编写.NET Remoting应用程序.我的dll,服务器和客户端都正常工作.但是,当我尝试更改我的方法调用以获取对象参数而不是像int这样的简单类型时,它会抱怨此错误.
键入System.Runtime.Remoting.ObjRef及其中的类型(例如System.Runtime.Remoting.ObjRef)不允许在此安全级别进行反序列化.
方法是这样的.
public List<Orders> GetOrders(int UserID) { //Works
public List<Orders> GetOrders(Users user) { // Doesnt Work
[Serializable]
public class Users : MarshalByRefObject {
Run Code Online (Sandbox Code Playgroud)
现在我已经创建了User类,[Serializable]并给它MarshalByRefObject继承.这可能是我的问题吗?我试过从User类中删除[Serializable]并且它抱怨因为它无法解释它.
编辑 好的,这是我的客户端方法.
IChannel channel = new TcpClientChannel();
ChannelServices.RegisterChannel(channel, false);
CustomType Server = (CustomType)Activator.GetObject(typeof(CustomType), "tcp://localhost:9934/CustomType");
Run Code Online (Sandbox Code Playgroud)
这是我的服务器.
BinaryServerFormatterSinkProvider provider = new BinaryServerFormatterSinkProvider();
provider.TypeFilterLevel = TypeFilterLevel.Full;
IDictionary props = new Hashtable();
props["port"] = 9934;
TcpChannel channel = new TcpChannel(props, null, provider);
ChannelServices.RegisterChannel(channel);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(CustomType), "CustomType", WellKnownObjectMode.Singleton);
Console.WriteLine("Server is initialized");
Console.ReadLine();
Run Code Online (Sandbox Code Playgroud) 我有一个类TouchPoint,它实现了Serializable,因为它包含Bitmap,我为该类编写了writeObject和readObject:
private void writeObject(ObjectOutputStream oos) throws IOException {
long t1 = System.currentTimeMillis();
oos.defaultWriteObject();
if(_bmp!=null){
int bytes = _bmp.getWidth()*_bmp.getHeight()*4;
ByteBuffer buffer = ByteBuffer.allocate(bytes);
_bmp.copyPixelsToBuffer(buffer);
byte[] array = buffer.array();
oos.writeObject(array);
}
Log.v("PaintFX","Elapsed Time: "+(System.currentTimeMillis()-t1));
}
private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException{
ois.defaultReadObject();
byte[] data = (byte[]) ois.readObject();
if(data != null && data.length > 0){
_bmp = BitmapFactory.decodeByteArray(data, 0, data.length);
}
}
Run Code Online (Sandbox Code Playgroud)
问题是我得到了
SkImageDecoder :: Factory返回null
那我该怎么办呢.我知道可能的解决方案是将writeObject()更改为
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
_bmp.compress(Bitmap.CompressFormat.PNG, 100, byteStream);
oos.writeObject(byteStream.toByteArray);
Run Code Online (Sandbox Code Playgroud)
但这种方法慢了近10倍.
更新 发现实际问题是在之后
buffer.array(); …Run Code Online (Sandbox Code Playgroud) ClassCastException随机发生以恢复onRestoreInstanceState()中的Vector.通常,恢复向量很好,但有时会发生异常.
我认为它发生在活动进入后台并被破坏但我不确定.
有任何想法吗?谢谢.
Stack<LocationInfo> mLocationInfoVector;
@Override
protected void onSaveInstanceState(Bundle outState) {
outState.putSerializable("locationInfos", mLocationInfoVector);
super.onSaveInstanceState(outState);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
if (savedInstanceState.getSerializable("locationInfos") != null) {
@SuppressWarnings("unchecked")
mLocationInfoVector= (Stack<LocationInfo>) savedInstanceState.getSerializable("locationInfos");
}
super.onRestoreInstanceState(savedInstanceState);
}
Run Code Online (Sandbox Code Playgroud)
添加:
我忘了附上异常日志.那是
java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.util.Stack
Run Code Online (Sandbox Code Playgroud) 我想通过bundle将Runnable传递给一个活动,这样runnable必须在onCreate触发时运行.我编写了一个实现Serializable的类,但它会导致异常:"java.lang.RuntimeException: Parcelable encountered IOException writing serializable object".我的代码在这里:
package some.company.common;
import java.io.Serializable;
public class SerializedRunnable implements Serializable, Runnable {
private static final long serialVersionUID = 6641813629033240205L;
public SerializedRunnable() {
}
private Runnable runnable;
public SerializedRunnable(Runnable runnable) {
this.runnable = runnable;
}
@Override
public void run() {
this.runnable.run();
}
}
Run Code Online (Sandbox Code Playgroud) 我需要为我的分布式系统类开发Java RMI应用程序.
在讲座期间,教授强调只让类实现Serializable,必须通过网络传递价值.
这意味着让太多的类实现Serializable会有一些缺点或惩罚.不需要通过网络发送的类.
我不知道如果你从未真正通过网络发送序列化/反序列化将永远不会发生任何缺点.
我正在尝试实现一个任务分配系统.用户可以从池中请求任务.即使设置为SERIALIZABLE,事务有时也会向多个用户提供相同的任务,即使它不应该.
CREATE TABLE tasks(
_id CHAR(24) PRIMARY KEY,
totalInstances BIGINT NOT NULL
);
CREATE TABLE assigned(
_id CHAR(24) PRIMARY KEY,
_task CHAR(24) NOT NULL
);
Run Code Online (Sandbox Code Playgroud)
任务表中填充了许多行,假设每个行都有totalInstances = 1,这意味着每个任务最多应分配一次.
assigned:WITH task_instances AS (
SELECT t._id, t.totalInstances - COUNT(assigned._id) openInstances
FROM tasks t
LEFT JOIN assigned ON t._id = assigned._task
GROUP BY t._id, t.totalInstances
),
selected_task AS (
SELECT _id
FROM task_instances
WHERE openInstances > 0
LIMIT 1
)
INSERT INTO assigned(_id, _task)
SELECT …Run Code Online (Sandbox Code Playgroud) serializable ×10
java ×5
android ×3
transactions ×2
.net ×1
bitmap ×1
bundle ×1
bytearray ×1
c# ×1
concurrency ×1
dto ×1
hibernate ×1
memento ×1
postgresql ×1
remoting ×1
rmi ×1
runnable ×1
spring ×1
sql ×1
syntax-error ×1
warnings ×1