Context depedency Injection (i)

Source: Internet
Author: User

Overview CDI is a new standard and specification for Java Dependency Injection control inversion in JavaEE6 and for slicing programming. CDI is affected by popular dependency injection (DI) and aspect-oriented programming, there are no uniform specifications for various implementations, and a set of standards or specifications are required to constrain Java programming in DI and AOP, with the aim of continuing to customize unified specifications and standards on this basis in the future. Di and AOP are the foundation of many Java frameworks, and the advent of CDI makes them the basis for higher level technical standards and specifications.
This article focuses on dependency injection and control inversion, which involves many new features of CDI, such as type safety, CDIAlternativesand so on. This tutorial is divided into two parts, the first part mainly relies on the injection of the base such as @inject, @Porduces and @qualifiers annotation tags, the second part is a number of high-level discussion topics, such as how to create the class plug-in instantiation of the component and some annotation processing configuration and so on.
Second, the benefits of applying CDI CDI as the basis for Java EE 6 aspect-oriented programming, is quickly supported by various enterprise application servers, such as Caucho's RESIN,IBM WebSphere and Oracle GlassFish, Red Hat's jboss has some Other application servers. CDI is similar to the Di and AOP in the Guice framework of spring and Google. Just as JPA is an ORM specification, CDI declarations stipulate the interfaces developed by DI and AOP. If you've ever used the spring and guice frameworks, you'll find that CDI is easy to learn and use.       However, if you have just contacted the dependency injection concept, then CDI will be the cornerstone of your fast learning dependency injection. CDI can also be used alone in any framework to integrate into a variety of applications.Dependency Injection (DI) refers to the process ofSoftwareprovides an externally dependent component that can help decouple your code schema and reduce the direct dependencies between objects. This design is designed to inject dependencies in interface design and test-driven development by providing a consistent way. For example, a data Access object (DAO) dependency is injected into a database connection,Instead of locating the database connection with Jndi. One way to consider a DI framework like CDI is to use Jndi to rely on lookup injection instead of one object looking for other objects, it needs to complete the work (dependency), and the Di container injects the dependent object. This is the so-called Hollywood principle, "Don't call us?" (Find object), "We'll call You" (inject object). If you've ever developed a CRC cards application, you can think of dependencies as a partner. A co-worker is an object that another object needs to perform, just like a DAO (data Access object) requires a JDBC Connection object.

 Iii. CDI Programming steps using CDI programming requires the following steps 1. Create an empty Bean.xml file under the Meta-inf resource File 2. Add @inject annotation 3 to the appropriate setter method on the class that needs to inject the dependency. Use @ default annotation corresponding class file 4. Use @alternative annotations corresponding selectable properties 5. Use the CDI Object Management container to find the appropriate object
Iv. Example 1. Create a Web project, create an empty bean.xml CDI implementation under the Meta-inf resource file you need to create a Meta-inf file under the Bean.xml path in your jar file or classpath or the Web-inf file path of your web app. Bean.xml files can be empty files, and if you do not have bean.xml files in your War package or jar package, they will not be managed by CDI. The CDI is responsible for automatically scanning the corresponding war package or whether the Bean.xml in the jar package is empty. code example
<beans xmlns= "Http://java.sun.com/xml/ns/javaee" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance    " xsi:schemalocation= "Http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/beans_1_0.xsd" ></ Beans>

2. Create the servlet, named Cdiservlet.        Add the @inject annotation to the set method in Cdiservlet. @Inject annotations are used to mark places where dependency injection is required. @Inject tags can be used on attributes, on construction methods and on set methods. In this example, we use the Set method. code example
Package Com.zhouxiang;import Java.io.ioexception;import Javax.inject.inject;import javax.naming.context;import Javax.naming.initialcontext;import Javax.naming.namingexception;import Javax.servlet.servletexception;import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletrequest;import javax.servlet.http.httpservletresponse;/** * Servlet Implementation class Cditest */public class Cditest extends    HttpServlet {private static final long serialversionuid = 1L;        @Inject private Test1 test;        /** * @see httpservlet#httpservlet () */public cditest () {super (); TODO auto-generated Constructor stub}/** * @see Httpservlet#doget (httpservletrequest request, HttpServletResponse R esponse) */protected void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {//TODO auto-generated method Stubdopost (request,response);} /** * @see Httpservlet#dopost (httpservletrequest request, HttpServletResponseResponse) */protected void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {//TODO auto-generated method StubSystem.out.println ("DSFSDFSD"); Test.test ();}}


3. Create the Test1 class and label the default injection label @default
Package Com.zhouxiang;import Javax.enterprise.inject.default;import Javax.inject.inject;import javax.inject.Named; @Defaultpublic class Test1 {public void Test ()   {System.out.println ("Hello inject!");}  }



Context depedency Injection (i)

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.