13Spring Configuring Beans with annotations (1)

Source: Internet
Author: User

Configuration Bean Form: XML file-based approach, annotation-based configuration bean, annotation- based assembly of Bean properties

The following describes the annotation-based approach to configuring beans.

--component scanning (component scanning): Spring is able to automatically scan, detect and instantiate components with specific annotations from the classpath.

-specific components include:

[Email protected]: basic annotations that identify a component that is managed by spring

[Email protected]: Identify the persistence layer component

[Email protected]: Identify service layer (business layer) components

[Email protected]: Identify presentation layer components

--for scanned components,Spring has a default naming policy : Use unqualified class name, first letter lowercase, or you can identify the component's name through the Value property in the annotation

After a specific annotation is used on the component class, it is also necessary to declare <context:component-scan> in the Spring configuration file (the context namespace needs to be imported):

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.

--When you need to scan multiple packages, you can use commas to separate

--If you want to scan only specific classes, not all classes under the base package, you can apply the Resource-pattern property to filter specific classes, for instance:

<context:component-scan base-package= "Com.spring.beans" resource-pattern= "Autowire/*.class"/>

The--<context:include-filter> child node represents the target class to be included

--<context:exclude-filter> child nodes indicate the target class to exclude

--<context:component-scan> can have a number of <context:include-filter> and <context: exclude-filter> child nodes

 Package annotation; Import org.springframework.stereotype.Component; @Component  Public class Testobject {}
 package   annotation.repository;  public  interface   Userrepository {  save ();}  
 package   annotation.repository;  import   Org.springframework.stereotype.Repository; @Repository (Value  =" Userrepository ")  public  class  userrepositoryimpl  userrepository {@Override  public  void
     Save () {System.out.println (" Userrepositoryimpl save "); }}
 package   Annotation.controller;  import   Org.springframework.stereotype.Controller; @Controller  public  class   Usercontroller { public  void   Execute () {System.out.println (" Usercontroller Execute "); }}
 Package Annotation.service; Import Org.springframework.stereotype.Service; @Service  Public class UserService {    publicvoid  Add () {        System.out.println ("UserService Add ");}    }
<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:context= "Http://www.springframework.org/schema/context"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd " >    <!--Specify packages scanned by the spring IOC container -    <!--<context:component-scan base-package= "annotation" > </context:component-scan> -    <!--the scanned resource can be specified by Resource-pattern -    <!--<context:component-scan base-package= "annotation" resource-pattern= "Repository/*.class" &    Gt </context:component-scan> -    <!--Context:exclude-filter Sub-nodes specify which components to exclude from the specified expression -    <!--The context:include-filter child node Specifies the component that contains the specified expression, which needs to be use-default-filters with a -    <Context:component-scanBase-package= "Annotation">        <!--annotation Indicates whether to include or not include according to the specified annotations -        <!--<context:exclude-filter type= "annotation" expression= "Org.springframework.stereotype.Repository" ></ Context:exclude-filter> -        <!--<context:include-filter type= "annotation" expression= "Org.springframework.stereotype.Repository" ></ Context:include-filter> -        <!--assignable contains or does not contain the specified class name according to the -       <!--<context:exclude-filter type= "assignable" expression= "Annotation.repository.UserRepository" ></ Context:exclude-filter> -        <!--<context:include-filter type= "assignable" expression= "Annotation.repository.UserRepository" ></ Context:include-filter> -    </Context:component-scan></Beans>
 Packageannotation;ImportAnnotation.controller.UserController;Importannotation.repository.UserRepository;ImportAnnotation.service.UserService;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext; Public classMain { Public Static voidMain (string[] args) {ApplicationContext ctx=NewClasspathxmlapplicationcontext ("13-1.xml"); Testobject to= (testobject) ctx.getbean ("Testobject");        System.out.println (to); Usercontroller Usercontroller= (Usercontroller) ctx.getbean ("Usercontroller");        System.out.println (Usercontroller); UserService UserService= (UserService) ctx.getbean ("UserService");        System.out.println (UserService); Userrepository userrepository= (userrepository) ctx.getbean ("Userrepository");    System.out.println (userrepository); }}

13Spring Configuring Beans with annotations (1)

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.