Dependency--Dependency injection in Java EE

Source: Internet
Author: User

when a resource annotation is prevented in a field or Setter method, there are two things that will happen. First, the resource reference is declared as if it were placed above the Bean class (similar to the code example above), and the resource name is bound to the environment naming context when the component is created. Second, the server automatically makes dependency lookups for you and sets the results to the instantiated class.

the process of automatically looking over a resource and setting it to a class is called Dependency injection (Dependency Injectionbecause the server is said to inject the parsed dependency into the class. This technique, one of which is commonly referred to as control inversion (Inversion of Control), eliminating the manualJNDIthe burden of finding resources in the context of the environment. Dependency injection is considered a best practice for application development, not only because it reducesJNDIfind the need (and the associated service locator mode), and also because it simplifies testing. The class does not have anyJNDI APIThe code relies on the application Server runtime environment, soBeanclasses can be instantiated directly in a unit test. Developers can manually provide the required dependencies and test the functionality of the class to be examined without worrying about how to surroundJNDI APIto work.

Field Injection

the first form of dependency injection is called field injection (Field Injection). Injecting a dependency into a field means that the server assigns the result directly to the annotation field of the class after the environment naming context looks for dependencies. The following code re-examines the code samples from the previous article and demonstrates@EJBNote, this time to inject the resultsAuditfield. In all of the Directory interface code we demonstrated earlier,Beanbusiness methods can assume thatAuditfield is retained in theAuditservice Beanthe reference.

Code

field injection is, of course, the easiest to implement, and the example used in this book is purely to save space. Field injection The only thing to consider is if you are planning unit tests, then you need to add a setter method or make this field accessible to your unit tests to manually satisfy the dependencies. Although free fields are legal, if there are no accessible methods, you will need an unpleasant hacker to set their values. If you want the unit test not to add a setter method, considerthe scope of the field-injected package.

as mentioned in the previous section, when a resource annotation is placed in a field orSettermethod, a name that is automatically generated is referenced, and for completeness, the format of the name is described, but you are unlikely to have many opportunities to use it. The generated name is fully qualified as the class name, followed by a forward slash, and then the name of the field or property. This means that ifAuditservice Beanis locatedpersistence.sessionpackage, then the injection referenced in the above programEJBin the "Persistence.session.auditservice/auditThe environment naming context under the name will be accessible. Specifying the resource annotationsnameelement overrides this default value.

Setter injected

setter Injection ( setterinjection setter setter using the results of the query. Span style= "font-size:13.0pt;line-height:200%;font-family: Song Body" > method. The following code examines the above code for the last time to demonstrate the setter injection.

Code

This style of injection allows the use of private fields, but it also applies to unit tests. Each test can simply instantiate the Bean class and perform the dependency injection manually by invoking the setter method, usually for the setter Method provides an implementation that conforms to the resources required for testing.

Dependency--Dependency injection in Java EE

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.