We use dependency injection a lot when we write code. You often see annotations like @beans and @Service in Spring to convert classes into beans for efficient management.
Beans managed by the beanFactory can be injected into other implementation classes via annotations. From there, we don’t care where the Bean comes from, where it’s going, how it’s created, how it’s destroyed, how the context is retrieved, and so on.
Bean
Most mainstream frameworks have this concept.
For example, our business implementation classes, interceptors, configuration classes, and filters are all based on the concept of beans. Quarkus’s beans, for example, have multiple annotations to distinguish between different scopes, but none of them require us to care about the details of Bean creation, injection, etc. Quarkus simplifies Bean discovery, but Quarkus encourages users not to use private members in beans.
Bean dependency injection
With beans effectively managed, implementing dependency injection is a very simple matter.
@ApplicationScoped
public class BrandInfoRepository extends ResourceRepository<BrandInfo> {}Copy the code
The most common annotation, @applicationScoped, puts the Bean into container management.
There are many ways to inject, but Quarkus does not recommend using private when injecting. The package-private modifier can be used instead.
@ApplicationScoped
public class BrandInfoResource extends BaseResource {
@Inject
BrandInfoRepository brandInfoRepository;
}
Copy the code
This is also the recommended method, Inject via @inject.
It is also possible to Inject via constructors, in which case the @inject annotation is unnecessary, but in which case the decorator can be private.
@ApplicationScoped
public class BrandInfoResource extends BaseResource {
private BrandInfoRepository brandInfoRepository;
public BrandInfoResource(BrandInfoRepository brandInfoRepository) {
this.brandInfoRepository = brandInfoRepository; }}Copy the code
Quarkus dependency injection method is actually more, but generally commonly used or @inject annotation way.
annotations
@ApplicationScoped
Javax.mail. Enterprise. The context ApplicationScoped is we most often use annotations. Annotated beans are singletons in the application system and are lazily created.
@Target({ TYPE, METHOD, FIELD })
@Retention(RUNTIME)
@Documented
@NormalScope
@Inherited
public @interface ApplicationScoped {
/**
* Supports inline instantiation of the {@link ApplicationScoped} annotation.
*
* @author Martin Kouba
* @since2.0 * /
public final static class Literal extends AnnotationLiteral<ApplicationScoped> implements ApplicationScoped {
public static final Literal INSTANCE = new Literal();
private static final long serialVersionUID = 1L; }}Copy the code
@Singleton
Javax.inject.Singleton is also a common annotation that differs from ApplicationScoped in that the instance is created immediately.
@Scope
@Documented
@Retention(RUNTIME)
public @interface Singleton {}
Copy the code
@RequestScoped
Javax.mail. Enterprise. Context. RequestScoped is limited to the scope of the current request.
@Target({ TYPE, METHOD, FIELD })
@Retention(RUNTIME)
@Documented
@NormalScope
@Inherited
public @interface RequestScoped {
/**
* Supports inline instantiation of the {@link RequestScoped} annotation.
*
* @author Martin Kouba
* @since2.0 * /
public final static class Literal extends AnnotationLiteral<RequestScoped> implements RequestScoped {
public static final Literal INSTANCE = new Literal();
private static final long serialVersionUID = 1L; }}Copy the code
@Dependent
. Javax.mail. Enterprise. The context Dependent the instance is not Shared, and each injection point will generate a new dependence on the Bean instance.
The bean’s life cycle is bound to the bean that injects it, and it is created and destroyed along with the bean that injects it.
@Target({ METHOD, TYPE, FIELD })
@Retention(RUNTIME)
@Documented
@Scope
@Inherited
public @interface Dependent {
/**
* Supports inline instantiation of the {@link Dependent} annotation.
*
* @author Martin Kouba
* @since2.0 * /
public final static class Literal extends AnnotationLiteral<Dependent> implements Dependent {
public static final Literal INSTANCE = new Literal();
private static final long serialVersionUID = 1L; }}Copy the code
@SessionScoped
Javax.mail. Enterprise. Context. SessionScoped cooperate quarkus – undertow use, the scope is the current Session.
Use appropriate notes
This is mainly for ApplicationScoped, which is created lazily, and Singleton, which is created immediately.
Singleton typically has better performance because it is invoked directly, eliminating the need for the Proxy to delegate context to the current instance’s Proxy.
However, Singleton cannot simulate beans through QuarkusMock.
In general, unless there is a good reason to use ApplicationScoped, we recommend sticking with Singleton by default for better performance.