自 Micronaut 1.1 以來,已經(jīng)包含了 JDK 的 Introspector 類的編譯時替代品。
BeanIntrospector 和 BeanIntrospection 接口允許查找 bean 自省以實例化和讀/寫 bean 屬性,而無需使用反射或緩存反射元數(shù)據(jù),這會為大型 bean 消耗過多的內(nèi)存。
使 Bean 可用于自省
與 JDK 的 Introspector 不同,每個類都不會自動進(jìn)行內(nèi)省。要使一個類可用于自省,您必須至少在構(gòu)建中啟用 Micronaut 的注釋處理器(用于 Java 和 Kotlin 的 micronaut-inject-java 和用于 Groovy 的 micronaut-inject-groovy),并確保您對 micronaut-core 具有運行時依賴性.
Gradle |
Maven |
annotationProcessor("io.micronaut:micronaut-inject-java:3.8.5")
|
<annotationProcessorPaths>
<path>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-inject-java</artifactId>
<version>3.8.5</version>
</path>
</annotationProcessorPaths>
|
對于 Kotlin,在 kapt 范圍內(nèi)添加 micronaut-inject-java 依賴項,對于 Groovy,在 compileOnly 范圍內(nèi)添加 micronaut-inject-groovy。
Gradle |
Maven |
runtimeOnly("io.micronaut:micronaut-core:3.8.5")
|
<dependency>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-core</artifactId>
<version>3.8.5</version>
<scope>runtime</scope>
</dependency>
|
配置構(gòu)建后,您可以通過多種方式生成內(nèi)省數(shù)據(jù)。
使用@Introspected 注解
@Introspected 注釋可以用在任何類上以使其可用于自省。簡單地用@Introspected 注釋類:
Java |
Groovy |
Kotlin |
import io.micronaut.core.annotation.Introspected;
@Introspected
public class Person {
private String name;
private int age = 18;
public Person(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
|
import groovy.transform.Canonical
import io.micronaut.core.annotation.Introspected
@Introspected
@Canonical
class Person {
String name
int age = 18
Person(String name) {
this.name = name
}
}
|
import io.micronaut.core.annotation.Introspected
@Introspected
data class Person(var name : String) {
var age : Int = 18
}
|
在編譯時生成內(nèi)省數(shù)據(jù)后,通過 BeanIntrospection API 檢索它:
Java |
Groovy |
Kotlin |
final BeanIntrospection<Person> introspection = BeanIntrospection.getIntrospection(Person.class); // (1)
Person person = introspection.instantiate("John"); // (2)
System.out.println("Hello " + person.getName());
final BeanProperty<Person, String> property = introspection.getRequiredProperty("name", String.class); // (3)
property.set(person, "Fred"); // (4)
String name = property.get(person); // (5)
System.out.println("Hello " + person.getName());
|
def introspection = BeanIntrospection.getIntrospection(Person) // (1)
Person person = introspection.instantiate("John") // (2)
println("Hello $person.name")
BeanProperty<Person, String> property = introspection.getRequiredProperty("name", String) // (3)
property.set(person, "Fred") // (4)
String name = property.get(person) // (5)
println("Hello $person.name")
|
val introspection = BeanIntrospection.getIntrospection(Person::class.java) // (1)
val person : Person = introspection.instantiate("John") // (2)
print("Hello ${person.name}")
val property : BeanProperty<Person, String> = introspection.getRequiredProperty("name", String::class.java) // (3)
property.set(person, "Fred") // (4)
val name = property.get(person) // (5)
print("Hello ${person.name}")
|
您可以使用靜態(tài) getIntrospection 方法檢索 BeanIntrospection
一旦有了 BeanIntrospection,就可以使用 instantiate 方法實例化一個 bean。
可以從內(nèi)省中檢索 BeanProperty
使用set方法設(shè)置屬性值
使用get方法獲取屬性值
將@Introspected 與@AccessorsStyle 一起使用
可以將@AccessorsStyle 注釋與@Introspected 一起使用:
import io.micronaut.core.annotation.AccessorsStyle;
import io.micronaut.core.annotation.Introspected;
@Introspected
@AccessorsStyle(readPrefixes = "", writePrefixes = "") (1)
public class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String name() { (2)
return name;
}
public void name(String name) { (2)
this.name = name;
}
public int age() { (2)
return age;
}
public void age(int age) { (2)
this.age = age;
}
}
使用@AccessorsStyle 注釋該類,為getter 和setter 定義空的讀寫前綴。
定義不帶前綴的 getter 和 setter。
現(xiàn)在可以使用 BeanIntrospection API 檢索編譯時生成的內(nèi)?。?/p>
BeanIntrospection<Person> introspection = BeanIntrospection.getIntrospection(Person.class);
Person person = introspection.instantiate("John", 42);
Assertions.assertEquals("John", person.name());
Assertions.assertEquals(42, person.age());
Bean Fields
默認(rèn)情況下,Java 自省僅將 JavaBean getter/setter 或 Java 16 記錄組件視為 bean 屬性。但是,您可以使用 @Introspected 注釋的 accessKind 成員在 Java 中定義具有公共或包保護(hù)字段的類:
Java |
Groovy |
import io.micronaut.core.annotation.Introspected;
@Introspected(accessKind = Introspected.AccessKind.FIELD)
public class User {
public final String name; // (1)
public int age = 18; // (2)
public User(String name) {
this.name = name;
}
}
|
import io.micronaut.core.annotation.Introspected
@Introspected(accessKind = Introspected.AccessKind.FIELD)
class User {
public final String name // (1)
public int age = 18 // (2)
User(String name) {
this.name = name
}
}
|
最終字段被視為只讀屬性
可變字段被視為讀寫屬性
accessKind 接受一個數(shù)組,因此可以允許兩種類型的訪問器,但根據(jù)它們在注釋中出現(xiàn)的順序更喜歡一種或另一種。列表中的第一個具有優(yōu)先權(quán)。
在 Kotlin 中不可能對字段進(jìn)行自省,因為無法直接聲明字段。
構(gòu)造方法
對于有多個構(gòu)造函數(shù)的類,在構(gòu)造函數(shù)上加上@Creator注解即可使用。
Java |
Groovy |
Kotlin |
import io.micronaut.core.annotation.Creator;
import io.micronaut.core.annotation.Introspected;
import javax.annotation.concurrent.Immutable;
@Introspected
@Immutable
public class Vehicle {
private final String make;
private final String model;
private final int axles;
public Vehicle(String make, String model) {
this(make, model, 2);
}
@Creator // (1)
public Vehicle(String make, String model, int axles) {
this.make = make;
this.model = model;
this.axles = axles;
}
public String getMake() {
return make;
}
public String getModel() {
return model;
}
public int getAxles() {
return axles;
}
}
|
import io.micronaut.core.annotation.Creator
import io.micronaut.core.annotation.Introspected
import javax.annotation.concurrent.Immutable
@Introspected
@Immutable
class Vehicle {
final String make
final String model
final int axles
Vehicle(String make, String model) {
this(make, model, 2)
}
@Creator // (1)
Vehicle(String make, String model, int axles) {
this.make = make
this.model = model
this.axles = axles
}
}
|
import io.micronaut.core.annotation.Creator
import io.micronaut.core.annotation.Introspected
import javax.annotation.concurrent.Immutable
@Introspected
@Immutable
class Vehicle @Creator constructor(val make: String, val model: String, val axles: Int) { // (1)
constructor(make: String, model: String) : this(make, model, 2) {}
}
|
@Creator 注釋表示要使用哪個構(gòu)造函數(shù)
此類沒有默認(rèn)構(gòu)造函數(shù),因此調(diào)用不帶參數(shù)的實例化會拋出 InstantiationException。
靜態(tài)創(chuàng)建者方法
@Creator 注釋可以應(yīng)用于創(chuàng)建類實例的靜態(tài)方法。
Java |
Groovy |
Kotlin |
import io.micronaut.core.annotation.Creator;
import io.micronaut.core.annotation.Introspected;
import javax.annotation.concurrent.Immutable;
@Introspected
@Immutable
public class Business {
private final String name;
private Business(String name) {
this.name = name;
}
@Creator // (1)
public static Business forName(String name) {
return new Business(name);
}
public String getName() {
return name;
}
}
|
import io.micronaut.core.annotation.Creator
import io.micronaut.core.annotation.Introspected
import javax.annotation.concurrent.Immutable
@Introspected
@Immutable
class Business {
final String name
private Business(String name) {
this.name = name
}
@Creator // (1)
static Business forName(String name) {
new Business(name)
}
}
|
import io.micronaut.core.annotation.Creator
import io.micronaut.core.annotation.Introspected
import javax.annotation.concurrent.Immutable
@Introspected
@Immutable
class Business private constructor(val name: String) {
companion object {
@Creator // (1)
fun forName(name: String): Business {
return Business(name)
}
}
}
|
@Creator注解應(yīng)用于實例化類的靜態(tài)方法
可以有多個注釋的“創(chuàng)建者”方法。如果有一個沒有參數(shù),它將是默認(rèn)的構(gòu)造方法。第一個帶參數(shù)的方法將用作主要構(gòu)造方法。
Enums
也可以內(nèi)省枚舉。給枚舉加上注解,可以通過標(biāo)準(zhǔn)的valueOf方法構(gòu)造。
在配置類上使用@Introspected 注解
如果要內(nèi)省的類已經(jīng)編譯并且不受您的控制,則另一種選擇是使用 @Introspected 注釋集的類成員定義配置類。
Java |
Groovy |
Kotlin |
import io.micronaut.core.annotation.Introspected;
@Introspected(classes = Person.class)
public class PersonConfiguration {
}
|
import io.micronaut.core.annotation.Introspected
@Introspected(classes = Person)
class PersonConfiguration {
}
|
import io.micronaut.core.annotation.Introspected
@Introspected(classes = [Person::class])
class PersonConfiguration
|
在上面的示例中,PersonConfiguration 類為 Person 類生成內(nèi)省。
您還可以使用 @Introspected 的 packages 成員,該包在編譯時掃描并為包中的所有類生成內(nèi)省。但是請注意,此功能目前被視為實驗性的。
編寫一個 AnnotationMapper 來檢查現(xiàn)有的注釋
如果您希望默認(rèn)內(nèi)省現(xiàn)有注釋,則可以編寫一個 AnnotationMapper。
這方面的一個例子是 EntityIntrospectedAnnotationMapper,它確保所有用 javax.persistence.Entity 注釋的 bean 在默認(rèn)情況下都是可自省的。
AnnotationMapper 必須位于注釋處理器類路徑中。
The BeanWrapper API
BeanProperty 提供讀取和寫入給定類的屬性值的原始訪問,并且不提供任何自動類型轉(zhuǎn)換。
您傳遞給 set 和 get 方法的值應(yīng)該與底層屬性類型相匹配,否則會發(fā)生異常。
為了提供額外的類型轉(zhuǎn)換智能,BeanWrapper 接口允許包裝現(xiàn)有的 bean 實例并設(shè)置和獲取 bean 的屬性,并在必要時執(zhí)行類型轉(zhuǎn)換。
Java |
Groovy |
Kotlin |
final BeanWrapper<Person> wrapper = BeanWrapper.getWrapper(new Person("Fred")); // (1)
wrapper.setProperty("age", "20"); // (2)
int newAge = wrapper.getRequiredProperty("age", int.class); // (3)
System.out.println("Person's age now " + newAge);
|
final BeanWrapper<Person> wrapper = BeanWrapper.getWrapper(new Person("Fred")) // (1)
wrapper.setProperty("age", "20") // (2)
int newAge = wrapper.getRequiredProperty("age", Integer) // (3)
println("Person's age now $newAge")
|
val wrapper = BeanWrapper.getWrapper(Person("Fred")) // (1)
wrapper.setProperty("age", "20") // (2)
val newAge = wrapper.getRequiredProperty("age", Int::class.java) // (3)
println("Person's age now $newAge")
|
使用靜態(tài) getWrapper 方法獲取 bean 實例的 BeanWrapper。
您可以設(shè)置屬性,BeanWrapper 將執(zhí)行類型轉(zhuǎn)換,或者如果無法轉(zhuǎn)換則拋出 ConversionErrorException。
您可以使用 getRequiredProperty 檢索屬性并請求適當(dāng)?shù)念愋?。如果該屬性不存在,則拋出 IntrospectionException,如果無法轉(zhuǎn)換,則拋出 ConversionErrorException。
Jackson and Bean Introspection
Jackson 配置為使用 BeanIntrospection API 來讀取和寫入屬性值并構(gòu)造對象,從而實現(xiàn)無反射序列化/反序列化。這從性能的角度來看是有益的,并且需要更少的配置來正確運行運行時,例如 GraalVM native。
默認(rèn)情況下啟用此功能;通過將 jackson.bean-introspection-module 配置設(shè)置為 false 來禁用它。
當(dāng)前僅支持 bean 屬性(具有公共 getter/setter 的私有字段),不支持使用公共字段。
此功能目前處于試驗階段,將來可能會發(fā)生變化。
更多建議: