# 4.5 Inject dependency injection

Using the @Inject annotation, you can inject dependent objects into the Controller and Interceptor. To use the injection function, you need the following configuration:

public void configConstant(Constants me) {
    // Enable the injection for jfinal web project components: Controller, Interceptor, and Validator.
    me.setInjectDependency(true);
    
    // Enable injection for superclasses. If not enabled, injection can be done in the superclass using Aop.get(...).
    me.setInjectSuperClass(true);
}
1
2
3
4
5
6
7

The above me.setInjectDependency(true) is a configuration pertaining to jfinal web components. However, Aop.get(...) and Aop.inject(...) can support injection without any additional configuration.

Once configured, it can be used in controllers, for example:

public class AccountController {
   
   @Inject
   AccountService service;    // Here, the dependent object will be injected
   
   public void index() {
       service.justDoIt();    // Call the method of the injected object
   }
}
1
2
3
4
5
6
7
8
9

@Inject can also be used for property injection in interceptors, for example:

public class MyInterceptor implements Interceptor {
    
    @Inject
    Service service;    // Here, the dependent object will be injected
    
    public void intercept(Invocation inv) {
        service.justDoIt();    // Call the method of the injected object
        inv.invoke();
    }
}
1
2
3
4
5
6
7
8
9
10

Special Note: The prerequisite for using Inject is that the object creation of the class annotated with @Inject is managed by jfinal. This allows jfinal the opportunity to perform injection. For example, the creation of Controller, Interceptor, and Validator is managed by jfinal, so these three components can use @Inject for injection.

Additionally, the injection action can be passed downward. For instance, if a Controller uses @Inject to inject an AaaService, then within AaaService, you can use @Inject to inject a BbbService, and so on.

If the object you need to create isn't managed by jfinal, you can use the Aop.get(...) method for the creation and injection of dependent objects, for example:

public class MyKit {
   
   static Service service = Aop.get(Service.class);
   
   public void doIt() {
      service.justDoIt();
   }
}
1
2
3
4
5
6
7
8

Since the creation of MyKit isn't managed by jfinal, you cannot use @Inject for dependency injection. However, the creation and assembly of Controller and Interceptor are managed by jfinal, so they can use @Inject to inject dependencies.

With Aop.get(...), you can create and inject objects anywhere. Moreover, you can use Aop.inject(...) to only inject dependencies into objects without creating them.

The @Inject annotation also supports specifying the injected implementation class. For example, in the following code, a MyService object will be injected for Service:

@Inject(MyService.class)
Service service;
1
2

If the @Inject(...) annotation doesn't specify the type of injection, you can still specify the type of injection using AopManager.me().addMapping(...), for instance:

AopManager.me().addMapping(Service.class, MyService.class);
1

With the above mapping, the following code will inject MyService for Service:

public class IndexController {
    
    @Inject
    Service service;
    
    public void index() {
        service.justDoIt();
    }
}
1
2
3
4
5
6
7
8
9
Last Updated: 9/17/2023, 6:05:19 AM