Spring's Dependency Injection (DI), inversion of Control (IOC), and facet-oriented (AOP)

Source: Internet
Author: User

The configuration file in spring is incremented

<context:component-scan base-package = "Com.jmu.ccjoin.service"/>
<context:annotation-config/>
1  Public InterfaceUserService {2 3     /**4 * Get user information by ID5      * 6      * @paramID7      * @return8      */9     voidget (Long ID);Ten}
1  Public Interface Buyerservice {23     void get (); 4 }

1 @Service 2  Public class Implements Buyerservice {34    @Override5public     void  Get () {6         System.out.println ("Buyer landing ..."); 7     }89 }

1 @Service2  Public classUserserviceimplImplementsUserService {3 4     //no Userdao type injected into the spring container into a bean5 //@Autowired6 //private Userdao Userdao;7     8 @Autowired9     PrivateBuyerservice Buyerservice;Ten  One      Public voidget (Long ID) { A buyerservice.get (); -System.out.println ("Userserviceimpl Run sucess!!"); -     } the}

1  Public Static voidMain (string[] args) {2 3ApplicationContext app =NewClasspathxmlapplicationcontext ("Classpath:applicationContext.xml");4Usercontroller Usercontroller = (usercontroller) app.getbean ("Usercontroller");5 Usercontroller.save ();6         7System.out.println ("----------------------------------");8         9Userserviceimpl Userserviceimpl = (Userserviceimpl) app.getbean ("Userserviceimpl");TenUserserviceimpl.get (1L); One          ASystem.out.println ("----------------------------------"); -         //userserviceimpl is injected into the spring container, and userservice is not injected into the spring container -UserService UserService = (userservice) app.getbean ("Userserviceimpl"); theUserservice.get (1L); -}

Note: The types of Userserviceimpl and UserService are UserService, and UserService is not registered as a bean in spring, so the UserService type can only be taken to Userserviceimpl

When using @autowired, first query the container for the corresponding type of bean

If the query results are exactly one, assemble the bean to the data specified by the @autowired

If the query results in more than one result, the @autowired is looked up by name.

If the result of the query is empty, an exception is thrown. Workaround, use Required=false

Spring will automatically scan the base-package corresponding path or the path of the sub-package below the Java file, if scanned into a file with @service, @Component, @Repository, @Controller and other such annotated class, Register these classes as beans


Di Dependency injection: only need to rely on the Spring-context package, the dependency injection is the interface, the interface must be implemented.
IOC control reversal: The original to the caller new, which is now given to spring to create, and injected into the caller, the control has been reversed. In fact, control inversion and dependency injection is about one thing.
AOP faces facets: its applications, such as dynamic proxies, are given to the spring agent for execution, and other logic is added before and after the method is called.

Other than that:

IOC can be decomposed into two seed types: Dependency Injection and Dependency lookup.

(1) Dependency lookup

For example, using Jndi to register a database connection pool, the code obtains a jndi lookup of dependencies from the registry (Jndi lookups):

CODE:
Initcontext = new InitialContext ();
Get Data source
DataSource ds = (DataSource) initcontext.lookup ("Java:comp/env/jdbc/mysql");

(2) Dependency Injection

Three implementation types of dependency Injection: interface injection, Setter injection, and constructor injection.

Spring's Dependency Injection (DI), inversion of Control (IOC), and facet-oriented (AOP)

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.