1.Spring provides annotations for creating objects in Bean management

  • @Component(Generic annotations. You can use Component generic annotations when you don’t know which annotation to use)
  • @service (annotating services at the Service layer)
  • @Controller(annotated Web request Controller)
  • @repository (Annotating DAO layer data access)

The above four annotations are the same in that they can be used to create bean instances, but they are as specific as possible

1.1 Importing Dependencies

1.2 Configuring the bean.xml file

Import XMLNS: Context, change xSI :schemaLocation, and enable component scanning. Filter,include, and exclude can be set


      
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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">
<! Enable component scanning -->
<! -- 1. If multiple packets are scanned, separate multiple packets with commas -->
<! -- 2. Scan the upper directory of the package -->
<! -- <context:component-scan base-package="com.atguigu.dao,com.atguigu.service"></context:component-scan>-->
    <context:component-scan base-package="com.atguigu"></context:component-scan>

<! -- Only scan Controller annotations for com.atguigu packages -->
    <context:component-scan base-package="com.atguigu" use-default-filters="false">
         <context:include-filter type="annotation"
                                 expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

    <! -- Scan com.atguigu for comments other than Controller -->
    <context:component-scan base-package="com.atguigu" use-default-filters="false">
        <context:exclude-filter type="annotation"
                                expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

</beans>
Copy the code

1.3 Creating the UserService class

package com.atguigu.service;

import com.atguigu.dao.UserDao;
import org.springframework.stereotype.Service;

/ * * *@author Zhang
 * @Description
 * @createIn the 2021-04-06 s when * /

// In the annotation, the value of the attribute can be omitted
    // The default is UserService --> UserService
// @service is equivalent to the following
//@Service(value = "userService")
@service (value = "haha")
@Service(value = "haha")
public class UserService {

    private UserDao userDao;
    public void add(a){
        System.out.println("service add......"); userDao.add(); }}Copy the code

1.4 Test: Tests whether objects can be obtained

public void test01(a){
    // Read the XML configuration file
    ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
    // Get the object
    UserService userService = context.getBean("haha", UserService.class);
    System.out.println(userService);
    userService.add();
}
Copy the code

The test results


2. Implement attribute injection based on annotation

  • @autowired (automatic assembly by attribute type)
  • @qualifier (auto-assemble by attribute name, note that this needs to be used in conjunction with @autowired)
  • @resource (can be injected by type, can be injected by name)
  • @value (inject normal attributes)

3. Complete annotation development and configure the SpringConfig class

package com.atguigu.configure;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

/ * * *@author ZhangZhengtao
 * @Description
 * @create2021-04-06 21:33 * /
@Configuration
@ComponentScan(basePackages = {"com.atguigu"})  
// The 
       tag is used in bean.xml
public class SpringConfig {}Copy the code

test

@Test
public void test02(a){
    // Load the configuration class
    ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
    UserService userService = context.getBean("userService", UserService.class);
    System.out.println(userService);
    userService.add();
}
Copy the code