java - Can someone explain what com.google.inject does? -


I have declared a class with my constructor which was annotated with @Inject is. And I have not called a producer anywhere in the entire project.

Then two questions:

& lt; 1 & gt; What does @ Inject mean? (What does it do? Constructor is annotated with it?)

& lt; 2 & gt; As the producer is never called directly, does the fact that it is commented with @Inject is anything to do with?

Google is a dependency injection library, so that you can create objects by expressing the relationship between them Are there. The objects are manufactured because they are demanded to create other items. You can also implement abstract classes or interfaces with different implementations by configuring Guice, which makes it useful for running or testing your code.

@Inject Builder and methods that determine the annotates are what many things need to initiate, there are also many other annotations that determine how the guide works but only Explanation of things is not enough; You also have to configure them.

Here's a very simple example (from one of my apps) I'm a MySQLDataTracker that requires a MysqlConnectionPoolDataSource : < Pre> public class MySQLDataTracker extends ExperimentDataTracker {@Inject Public MySQLDataTracker (MysqlConnectionPoolDataSource DS) {.. ..}}

Note that MySQLDataTracker spreads ExperimentDataTracker , an abstract class can be implemented in many ways. In my Guice bindings, I declare that

  bind (ExperimentDataTracker.class) .to (MySQLDataTracker.class);   

It declares that whenever I want ExperimentDataTracker , a MySQLDataTracker will be created. I also need to make sure that the required object for this construction is available, I declare a provider:

  @Provides @Singleton MysqlConnectionPoolDataSource getMysqlCPDS () {return (some thingy i build ...); }   

It says that there should be only one connection pool data source. It also means that when I try to get an example of ExperimentDataTracker , then Guys needs to make it. If I was not above, then it would throw an error

  ExperimentDataTracker Tracker = injector.getInstance (ExperimentDataTracker.class).   

However, it does not stop here. Other things depend on ExperimentDataTracker , then it is used to inject other objects in return, in fact only one call at the top level of my code getInstance It does happen, which makes Guice very much everything, I do not have to write the new statement anywhere.

I see how it is a big fan of Guice after reducing the need to start a bunch of things for me. In order to initialize the other things, I will ask for what I want, and POF! that sounds like.

Comments

Popular posts from this blog

excel vba - How to delete Solver(SOLVER.XLAM) code -

github - Teamcity & Git - PR merge builds - anyway to get HEAD commit hash? -

ios - Replace text in UITextView run slowly -