SPRINGMVC Bean injection Issue and IOC container

Source: Internet
Author: User
SPRINGMVC & Spring Ioc+bean

Spring's IOC container contains the SPRINGMVC IOC, that is, the SPRINGMVC-configured bean can invoke spring-configured beans, not vice versa.
1.1 explains why the bean is not injected automatically in the Application.xml file, but is automatically injected into the Spring-servlet.xml file

Use annotations to automatically put beans into the IOC container
2.1 @Controller (control layer); @Service (service layer); @Repository (persistent layer dao); @Component (generic component)
2.2 After using annotations, you must scan the packages in Applicationcontext.xml or spring-servlet.xml files <context:component-scan base-package= " You need to scan the package "></context:component-scan>

Get the bean from the IOC container and inject it
3.1 Manually configuring the bean, pay attention to which IOC container your configured bean is in (SPRING&SPRINGMVC)
3.2 Automatic injection, using the annotation @autowired (which can be combined with @qualifier ("Bean's name"), can also be used to @resources from the dynamic injection. The former match according to the type, and the latter according to the name.
3.3 Injected when there is one to note: Do not create a new instance, or you can not inject success, is generally null pointer exception. Injection can also be understood as an instance of new (singleton mode), so you new one, it is equivalent to take someone else's way, others have no way to go.

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.