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:
- Using the @configuration annotation requires the class to be configured, indicating that the class will define the bean's metadata
- Using the @bean annotation method, the method name defaults to the name of the bean, and the method return value is the Bean object.
- 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