Java lightweight IOC framework Guice
Guice is an absolutely lightweight java IoC container developed by Google Daniel Bob lee. Its advantage is: the speed is 100 times faster than spring. There is no external configuration (you can use the Guice extension package if you need to use external resources). It is completely based on the annotation Feature and supports refactoring and static code check. Simple, fast, and basically no learning cost. Guice and spring have their own strengths. Guice is more suitable for embedded or high-performance but simple project solutions, such as OSGI containers, and spring is more suitable for large project organizations. When talking about the IOC framework, the injection method will first focus on the construction, attributes, and function injection methods. The implementation of Guice only needs to be in the constructor, field, or add @ Inject to the injection function, for example, construct the public class OrderServiceImpl implements OrderService {private ItemService itemService; private extends PriceService; @ Inject public OrderServiceImpl (ItemService itemService, priceService PriceService. itemService = itemService; this. priceService = priceService ;}...} attribute injection public class OrderServiceImpl Implements OrderService {private ItemService itemService; private PriceService priceService; @ Inject public void init (ItemService itemService, PriceService priceService) {this. itemService = itemService; this. priceService = priceService ;}...} function (setter) injection public class OrderServiceImpl implements OrderService {private ItemService itemService; private PriceService priceService; @ Inject public voi D setItemService (ItemService itemService) {this. itemService = itemService;} @ Inject public void setPriceService (PriceService priceService) {this. priceService = priceService ;}...} module dependency register Guice provides the dependency configuration class, which must be inherited to AbstractModule to implement the configure method. In the configure method, we can use Binder to configure dependencies. Binder uses the chain to form a set of unique-semantic DSL, such as: basic configuration: binder. bind (serviceClass ). to (implClass ). in (Scopes. [SINGLETON | NO_SCOPE]); no base class, interface configuration: binder. bind (implClass ). in (Scopes. [SINGLETON | NO_SCOPE]); service instance configuration: binder. bind (serviceClass ). toInstance (servieInstance ). in (Scopes. [SINGLETON | NO_SCOPE]); multiple instances are injected by name: binder. bind (serviceClass ). annotatedWith (Names. named ("name ")). to (implClass ). in (Scopes. [SINGLETON | NO_SCOPE]); runtime injection: Use @ Provides annotation injection method, which is equivalent to spring @ Bean. @ ImplementedBy: Or Mark @ ImplementedBy on the implementation interface to specify its implementation class. This method is somewhat counter-OO design, so the abstract should not know its implementation class. Only @ Inject annotation is required for the above configuration in the injection method, but @ Named annotation must be added before the parameter for name-based injection, such as: public void configure () {final Binder binder = binder (); // TODO: bind named instance; binder. bind (NamedService. class ). annotatedWith (Names. named ("impl1 ")). to (NamedServiceImpl1.class); binder. bind (NamedService. class ). annotatedWith (Names. named ("impl2 ")). to (NamedServiceImpl2.class);} @ Injectpublic List <NamedService> getAllItemServices (@ Named ("impl 1 ") NamedService nameService1, @ Named (" impl2 ") NamedService nameService2) {} Guice can also use the @ Provides annotation injection method for runtime injection: for example, @ Provides public List <NamedService> getAllItemServices (@ Named ("impl1") NamedService nameService1, @ Named ("impl2") NamedService nameService2) {final ArrayList <NamedService> list = new ArrayList <NamedService> (); list. add (nameService1); list. add (nameService2); return list;} under a Guice instance is Module instance code: contains most of the commonly used dependency configuration methods. For more code, see github. package com. github. greengerong. app; /*************************************** * ***** Auth: green gerong ** Date: 2014 ** blog: http://greengerong.github.io/** github: *************************************** * *****/public class AppModule extends AbstractModule {private static final Logger LOGGER = LoggerFactory. getLogger (AppModule. class); pri Vate final BundleContext bundleContext; public AppModule (BundleContext bundleContext) {this. bundleContext = bundleContext; LOGGER.info (String. format ("enter app module with: % s", bundleContext) ;}@ Override public void configure () {final Binder binder = binder (); // TODO: bind interface binder. bind (ItemService. class ). to (ItemServiceImpl. class ). in (SINGLETON); binder. bind (OrderService. class ). to (Orde RServiceImpl. class ). in (SINGLETON); // TODO: bind self class (without interface or base class) binder. bind (PriceService. class ). in (Scopes. SINGLETON); // TODO: bind instance not class. binder. bind (RuntimeService. class ). toInstance (new RuntimeService (); // TODO: bind named instance; binder. bind (NamedService. class ). annotatedWith (Names. named ("impl1 ")). to (NamedServiceImpl1.class); binder. bind (NamedService. cl Ass ). annotatedWith (Names. named ("impl2 ")). to (NamedServiceImpl2.class);} @ Provides public List <NamedService> getAllItemServices (@ Named ("impl1") NamedService nameService1, @ Named ("impl2") NamedService nameService2) {final ArrayList <NamedService> list = new ArrayList <NamedService> (); list. add (nameService1); list. add (nameService2); return list;} Use of Guice is relatively simple for Guice. Use the Guice module to initialize Guice and create its inj Ector, for example: Injector injector = Guice. createInjector (new AppModule (bundleContext); multiple modules can be imported here, and we can use modules to separate domain dependencies. Guice api method: public static Injector createInjector (Module... modules) public static Injector createInjector (Iterable <? Extends Module> modules) public static Injector createInjector (Stage stage, Module... modules) public static Injector createInjector (Stage stage, Iterable <? Extends Module> modules) Guice also supports different Region configurations. The State overload above supports TOOL, DEVELOPMENT, and PRODUCTION options. The default value is the DEVELOPMENT environment.