Three ways to define Spring beans

Source: Internet
Author: User

I. XML-based configuration

Applicable scenarios:

    • The Bean implementation class comes from a third-party class library, such as: DataSource, etc.
    • Requires namespace configuration, such as: CONTEXT,AOP,MVC, etc.
<Beans><ImportResource= "Resource1.xml"/>//Import definitions for other profile beans<ImportResource= "Resource2.xml"/>
<BeanID= "UserService"class= "cn.lovepi.***." UserService "Init-method= "Init"Destory-method= "Destory"> </Bean><BeanID= "message"class= "Java.lang.String">    <Constructor-argIndex= "0"value= "Test"></Constructor-arg></Bean></Beans>

Second, annotation-based configuration

Applicable scenarios:

    • The classes that are used in the project, such as Controller, service, DAO, etc.

The steps are as follows:

1. Configure the scan package path in Applicationcontext.xml

<Context:component-scanBase-package= "Com.lovepi.spring">     <Context:include-filtertype= "Regex"expression= "com.lovepi.spring.*"/>Included Target class<Context:exclude-filtertype= "AspectJ"expression= "Cn.lovepi". *controller+ "/>Excluded target Classes</Context:component-scan>

Note:<context:component-scan/> has actually included the function of <context:annotation-config/>

2. Declaring beans with annotations

Spring provides four annotations that work in accordance with the XML definition bean effect above, in which the component is given to the spring container management. The name of the component defaults to the class name (lowercase) and can be modified by itself:

    • @Component: Use this annotation when the hierarchy of components is difficult to locate
    • @Controller: Component representing the control layer
    • @Service: Components representing the business logic layer
    • @Repository: Component representing the data access layer
@Servicepublic class Sysuserservice {    @Resource    private sysusermapper sysusermapper;    public int insertselective (Sysuser record) {        return sysusermapper.insertselective (record);}    }
Third, Java class-based configuration

Applicable scenarios:

    • scenarios where you need to create logic through code control objects
    • Implement zero Configuration, eliminate XML configuration files

The steps are as follows:

    1. Using the @configuration annotation requires the class to be configured, indicating that the class will define the bean's metadata
    2. Using the @bean annotation method, the method name defaults to the name of the bean, and the method return value is the Bean object.
    3. Annotationconfigapplicationcontext or subclasses load Java class-based configuration
@Configuration public  class Beansconfiguration {      @Bean public      Student Student () {          Student student=new Student ();          Student.setname ("Zhang San");          Student.setteacher (Teacher ());          return student;      }      @Bean public      Teacher Teacher () {          Teacher teacher=new Teacher ();          Teacher.setname ("John Doe");          return teacher;      }  }  

public class Main {public      static void Main (String args[]) {          Annotationconfigapplicationcontext context = new Anno Tationconfigapplicationcontext (beansconfiguration.class);          Student Student = (Student) context.getbean ("Student");        Teacher Teacher = (Teacher) context.getbean ("Teacher");        SYSTEM.OUT.PRINTLN ("Student's name:" + student.getname () + ". The teacher is "+ student.getteacher (). GetName ());          System.out.println ("Teacher's name:" + teacher.getname ());      }  

Ref: 78524489

Three ways to define Spring beans

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.