按照 @Ryan Emerson 的建议更新了我的代码,但我仍然没有看到任何自动生成的 Impl 文件和 proto 文件。
@AutoProtoSchemaBuilder(
includeClasses = { Book.class, Author.class },
schemaFileName = "library.proto",
schemaFilePath = "proto/")
interface DummyInitializer extends SerializationContextInitializer {
}
笔者班
public class Author {
private final String name;
private final String surname;
@ProtoFactory
public Author(String name, String surname) {
this.name = (String)Objects.requireNonNull(name);
this.surname = (String)Objects.requireNonNull(surname);
}
@ProtoField(
number = 1
)
public String getName() {
return this.name;
}
@ProtoField(
number = 2
)
public String getSurname() {
return this.surname;
}
public boolean equals(Object o) {
if (this == o) {
return true;
} else if (o != null && this.getClass() == o.getClass()) {
Author author = (Author)o;
return this.name.equals(author.name) && this.surname.equals(author.surname);
} else {
return false;
}
}
public int hashCode() {
return Objects.hash(new Object[]{this.name, this.surname});
}
}
书籍类
public class Book {
private final String title;
private final String description;
private final int publicationYear;
private final Set<Author> authors;
@ProtoFactory
public Book(String title, String description, int publicationYear, Set<Author> authors) {
this.title = (String)Objects.requireNonNull(title);
this.description = (String)Objects.requireNonNull(description);
this.publicationYear = publicationYear;
this.authors = (Set)Objects.requireNonNull(authors);
}
@ProtoField(
number = 1
)
public String getTitle() {
return this.title;
}
@ProtoField(
number = 2
)
public String getDescription() {
return this.description;
}
@ProtoField(
number = 3,
defaultValue = "-1"
)
public int getPublicationYear() {
return this.publicationYear;
}
@ProtoField(
number = 4
)
public Set<Author> getAuthors() {
return this.authors;
}
public boolean equals(Object o) {
if (this == o) {
return true;
} else if (o != null && this.getClass() == o.getClass()) {
Book book = (Book)o;
return this.publicationYear == book.publicationYear && this.title.equals(book.title) && this.description.equals(book.description) && this.authors.equals(book.authors);
} else {
return false;
}
}
public int hashCode() {
return Objects.hash(new Object[]{this.title, this.description, this.publicationYear, this.authors});
}
}
带有覆盖方法的context-initialzer类。
import org.infinispan.protostream.SerializationContext;
import java.io.UncheckedIOException;
public class contextInitializer implements DummyInitializer {
@Override
public String getProtoFileName() {
return null;
}
@Override
public String getProtoFile() throws UncheckedIOException {
return null;
}
@Override
public void registerSchema(SerializationContext serCtx) {
}
@Override
public void registerMarshallers(SerializationContext serCtx) {
}
}
那么实例化上下文初始化器的ClassA
public class classA {
DummyInitializer myInterface= new contextInitializer();
//Create a new cache instance
public void startCache() {
{
try {
manager = new DefaultCacheManager("src/main/resources/infinispan.xml");
GlobalConfigurationBuilder builder= new GlobalConfigurationBuilder();
builder.serialization().addContextInitializers(myInterface);
System.out.println("------------------>"+ builder.serialization().addContextInitializers(myInterface));
cache = manager.getCache();
System.out.println(cache.getName()+" is initialized ");
} catch (IOException e) {
throw new IllegalArgumentException("Failed to initialize cache due to IO error",e);
}
}
}
Maven
<dependency>
<groupId>org.infinispan</groupId>
<artifactId>infinispan-bom</artifactId>
<version>${infinispan.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.infinispan.protostream</groupId>
<artifactId>protostream-processor</artifactId>
<scope>provided</scope>
</dependency>
我仍然没有看到任何自动生成的proto文件。谁能告诉我,我到底做错了什么?
你还需要添加 org.infinispan.protostream:protostream-processor
artifact作为依赖,以便生成代码。
<dependency>
<groupId>org.infinispan.protostream</groupId>
<artifactId>protostream-processor</artifactId>
<version>4.3.2.Final</version>
</dependency>
一旦有了这个依赖,一个DummyInitializerImpl.java类就会被生成,它将自动注册proto文件和marshallers的 Book
和 Author
类。请记住,这些类也必须有原流注解,以便生成模式和marshallers。请看 文件 的代码示例。
你目前的代码有两个问题。
DummyInitializerImpl
类,但那是应该由 @AutoProtoSchemaBuilder
.DummyInitializerImpl
您正在尝试注册Infinispan的。UUIDMarshaller
对于 Book
和 Author
类型。这样做是行不通的,因为那个marshaller是为java的 UUID
类。我怀疑这两个问题是由于对代码生成工作原理的误解。如果你只是要求一个 SerializationContextInitializer
对于 Author
和 Book
类,它不需要创建 DummyInitializerImpl
你绝对不需要使用UUIDMarshaller。
你没有说使用的是哪个构建系统,也许是maven?你是否添加了protostream注释处理器作为依赖?对这些问题有一个明确的答案将有助于解决代码生成的问题。而在这之后,我们仍然需要找出谁应该初始化那个dummyInitializer字段。