Follow the new brother. Learn Spring framework-Configuring beans with annotations (iv)

Source: Internet
Author: User

Component scanning: Spring can automatically scan from Classpath, detect and instantiate components with specific annotations.

specific components include:

1,@Component: Basic annotations, identify a spring-managed component

2.@Respository: Identify durable layer components

3.@Service: Identify business layer components

4.@Controller: Identify presentation layer components

Spring has a default naming policy : Use unqualified class name, first letter lowercase. You can also identify the name of a component in the annotation with the Value property values

When a specific annotation is used on a component class, you also need to declare <context:component-scan> in the Spring configuration file:
1. The Base-package property specifies a base class package that needs to be scanned, and the Spring container will scan all classes in the base class package and its child packages.
2. When you need to scan multiple packages, you can use commas to separate them.
3,<context:include-filter/> and <context:exclude-filter/>

1 <!--Scanning @controller Annotations -2 <Context:component-scanBase-package= "Com.hzg.controller"use-default-filters= "false">3     <Context:include-filtertype= "Annotation"expression= "Org.springframework.stereotype.Controller" />4 </Context:component-scan>5 6 <!--Configure scan annotations, do not scan @controller annotations -7 <Context:component-scanBase-package= "Com.hzg.controller">8     <Context:exclude-filtertype= "Annotation"expression= "Org.springframework.stereotype.Controller" />9 </Context:component-scan>

When using <context:include-filter/>, use-default-filters= "false" must be added to the <context:component-scan >, otherwise it will not work.

Where the property expression value is not the location of your package, do not make a mistake, it is the specific type of address you annotated.

Instance:

  Create Package Com.hzg.anotation

Create Package Com.hzg.anotation.controller

Creating the Usercontroller Class

1 @Controller2  Public classUsercontroller {3 4@Autowired (required =false)5     PrivateUserService UserService;6     //@Autowired can also be placed on the setter method, then remove the autowired annotation above7      Public voidSetuserservice (UserService userservice) {8          This. UserService =UserService;9     }Ten  One      Public voidExcute () { ASystem.out.println ("Usercontroller excute"); - Userservice.diao (); -     } the}

Create Package Com.hzg.anotation.service

Creating the UserService Class

1 @Service2  Public classUserService {3  4 @Autowired5      Privateuserrepostory userrepostory;6       Public voidDiao () {7System.out.println ("UserService Diao");8 Userrepostory.save ();9      }Ten}

Create Package Com.hzg.anotation.repostory

Creating the Userrepostory interface

1  Public Interface userrepostory {2     void save (); 3 }

Creating the USERREPOSTORYIMLP Class

1 @Repository ("Userrepostory") 2  Public class Implements userrepostory {34    @Override5      public  void  Save () {6         System.out.println ("Userrepostory save"); 7     }8 }

Create a Configautowire.xml file

1 <  base-package= "com.hzg.anotation"></Context: Component-scan>

Main method

1  Public Static void Main (string[] args) {2  new classpathxmlapplicationcontext ("Configautowire.xml"); 3 Usercontroller Usercontroller = (usercontroller) ctx.getbean ("Usercontroller"); 4 Usercontroller.excute (); 5 }

Output interface:

Usercontroller Excuteuserservice Diaouserrepostory Save

which

1, @Autowired (required = false) in required = False means: If there is no instantiation of this class, then the value will be assigned to NULL, not an error.

2. @Autowired annotations You can assign values to member variables, methods, and constructors.

3, @Repository ("Userrepostory") is equivalent to @repository (value = "Userrepostory"), value is the default value, which is represented to the bean

The value of the ID is assigned, preventing duplicate beans.

4. If you use the qualifier modifier @qualifier ("USERREPOSTORYIMLP") under the @autowired of the UserService class, then

@Repository ("Userrepostory") must be written @repository or written @repository ("USERREPOSTORYIMLP"), otherwise there is ambiguity.

--------------------------------------------------------------------------------------------------------------- ---------

Follow the new brother. Spring Frame series: Follow new brother to learn Spring framework--Create HelloWorld project (i) Follow the new Brother study Spring Framework--spring container (ii) follow the new brother. Learn Spring Framework--Configure beans with XML (iii) follow the new brother to learn Spring framework--Configuring beans by Annotations (iv) Follow the new Brother study Spring Framework--AOP (v)

Follow the new brother. Learn Spring framework-Configuring beans with annotations (iv)

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.