Day2021-10-04 (getting started with spring IOC)

3. Spring

3.1 general

  • IoC: control inversion, which reverses the right to create objects from new to spring.
  • IoC core: manage the life cycle of objects and the relationship between objects (injection)

3.2 introduction case: basic case

  • Objective: to write an introduction to spring IoC

  • Steps:

    • Write the function implementation class and add it to the spring container: UserDao
    • Write configuration classes and scan the classes that need to be loaded
    • Test class
  • Steps:

    • Function implementation class and add it to the spring container: UserDao

      package com.czxy.demo01.dao;
      
      import org.springframework.stereotype.Component;
      
      /**
       * @author Uncle Tong
       * @email liangtong@itcast.cn
       */
      @Component("userDao1")
      public class UserDao {
          public void selectAll() {
              System.out.println("userdao selectAll");
          }
      }
      
      
    • Write configuration classes and scan the classes that need to be loaded

      package com.czxy.demo01.config;
      
      import org.springframework.context.annotation.ComponentScan;
      import org.springframework.context.annotation.Configuration;
      
      /**
       * @author Uncle Tong
       * @email liangtong@itcast.cn
       */
      @Configuration      //Configuration class
      @ComponentScan(basePackages = "com.czxy.demo01.dao")  //Scan the package of the class to be loaded
      public class SpringConfiguration {
      }
      
      
    • Test class

      package com.czxy.demo01;
      
      import com.czxy.demo01.config.SpringConfiguration;
      import com.czxy.demo01.dao.UserDao;
      import org.springframework.context.ApplicationContext;
      import org.springframework.context.annotation.AnnotationConfigApplicationContext;
      
      /**
       * @author Uncle Tong
       * @email liangtong@itcast.cn
       */
      public class TestDemo01 {
          public static void main(String[] args) {
              // 1 obtain the factory by processing the notes
              ApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringConfiguration.class);
              // 2. Obtain mapper through the factory
              UserDao userDao =(UserDao) applicationContext.getBean("userDao1");
              // 3 call
              userDao.selectAll();
          }
      }
      
      

3.3 entry case: dependency injection

[the external chain picture transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-zbsipl8c-163347949587) (assets / image-20211004111630293. PNG)]

  • Steps:

    1. Write UserDao and use @ Component
    2. Write UserService and use @ Component
    3. Modify UserService, provide UserDao member variable, and use @ Resource to complete injection
    4. Write test classes and use @ componentscan (service, dao)
    5. Test class
  • Steps:

  1. Write UserDao and use @ Component

    package com.czxy.demo02.dao;
    
    import org.springframework.stereotype.Component;
    
    /**
     * @author Uncle Tong
     * @email liangtong@itcast.cn
     */
    @Component("userDao2")
    public class UserDao {
    
        public void selectAll() {
            System.out.println("dao : selectAll ");
        }
    }
    
    
  2. Write UserService and use @ Component

  3. Modify UserService, provide UserDao member variable, and use @ Resource to complete injection

    package com.czxy.demo02.service;
    
    import com.czxy.demo02.dao.UserDao;
    import org.springframework.stereotype.Component;
    
    import javax.annotation.Resource;
    
    /**
     * @author Uncle Tong
     * @email liangtong@itcast.cn
     */
    @Component("userService2")
    public class UserService {
    
        // Inject userDao1 into UserDao
        @Resource(name="userDao2")
        private UserDao userDao;
    
        public void selectAll() {
            // Call dao
            userDao.selectAll();
    
            System.out.println("service : selectAll ");
        }
    }
    
    
  4. Write test classes and use @ componentscan (service, dao)

    package com.czxy.demo02.config;
    
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    
    /**
     * @author Uncle Tong
     * @email liangtong@itcast.cn
     */
    @Configuration
    @ComponentScan(basePackages = {"com.czxy.demo02.dao","com.czxy.demo02.service"})
    public class Demo02Configuration {
    }
    
    
  5. Test class

    package com.czxy.demo02;
    
    
    import com.czxy.demo02.config.Demo02Configuration;
    import com.czxy.demo02.dao.UserDao;
    import com.czxy.demo02.service.UserService;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.annotation.AnnotationConfigApplicationContext;
    
    /**
     * @author Uncle Tong
     * @email liangtong@itcast.cn
     */
    public class TestDemo02 {
        public static void main(String[] args) {
            // 1 obtain the factory by processing the notes
            ApplicationContext applicationContext = new AnnotationConfigApplicationContext(Demo02Configuration.class);
            // 2. Obtain mapper through the factory
            UserService userService = applicationContext.getBean("userService2", UserService.class);
            // 3 call
            userService.selectAll();
        }
    }
    
    

3.4 entry case: dependency injection interface

  • step

    1. Write UserDao interface
    2. Write UserDao implementation class: UserDaoImpl
    3. Write UserService interface
    4. Write the UserServiceImpl implementation class and inject UserDao: UserServiceImpl
    5. Write configuration class
    6. Writing test classes
  • step

    1. Write UserDao interface

      package com.czxy.demo03.dao;
      
      /**
       * @author Uncle Tong
       * @email liangtong@itcast.cn
       */
      public interface UserDao {
          public void selectAll() ;
      }
      
      
    2. Write UserDao implementation class: UserDaoImpl

      package com.czxy.demo03.dao.impl;
      
      import com.czxy.demo03.dao.UserDao;
      import org.springframework.stereotype.Component;
      
      /**
       * @author Uncle Tong
       * @email liangtong@itcast.cn
       */
      @Component("userDao3")
      public class UserDaoImpl implements UserDao {
          @Override
          public void selectAll() {
              System.out.println("userdao Interface: selectAll " );
          }
      }
      
      
    3. Write UserService interface

      package com.czxy.demo03.service;
      
      /**
       * @author Uncle Tong
       * @email liangtong@itcast.cn
       */
      public interface UserService {
          public void selectAll();
      }
      
      
    4. Write the UserServiceImpl implementation class and inject UserDao: UserServiceImpl

      package com.czxy.demo03.service.impl;
      
      import com.czxy.demo03.dao.UserDao;
      import com.czxy.demo03.service.UserService;
      import org.springframework.stereotype.Component;
      
      import javax.annotation.Resource;
      
      /**
       * @author Uncle Tong
       * @email liangtong@itcast.cn
       */
      @Component("userService3")
      public class UserServiceImpl implements UserService {
      
          @Resource(name="userDao3")
          private UserDao userDao;
          @Override
          public void selectAll() {
              userDao.selectAll();;
              System.out.println("userService3 : selectAll ");
          }
      }
      
      
    5. Write configuration class

      package com.czxy.demo03.config;
      
      import org.springframework.context.annotation.ComponentScan;
      import org.springframework.context.annotation.Configuration;
      
      /**
       * @author Uncle Tong
       * @email liangtong@itcast.cn
       */
      @Configuration
      @ComponentScan(basePackages = {"com.czxy.demo03.dao","com.czxy.demo03.service"})
      public class Demo03Configuration {
      }
      
      
    6. Write test classes and test with integrated JUnit

      package com.czxy.demo03;
      
      import com.czxy.demo03.config.Demo03Configuration;
      import com.czxy.demo03.service.UserService;
      import org.junit.Test;
      import org.junit.runner.RunWith;
      import org.springframework.test.context.ContextConfiguration;
      import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
      
      import javax.annotation.Resource;
      
      /**
       * @author Uncle Tong
       * @email liangtong@itcast.cn
       */
      @RunWith(SpringJUnit4ClassRunner.class)     //spring integrates Junit
      @ContextConfiguration(classes = Demo03Configuration.class)  //Load configuration class
      public class TestDemo03 {
      
          @Resource(name="userService3")
          private UserService userService;
      
          @Test
          public void testDemo03() {
              userService.selectAll();
          }
      }
      
      

      Summary: after Mybatis finishes learning, it immediately learns the most important framework. The spring framework is one of the most used frameworks in enterprises. Spring is a one-stop framework. The reason why it is called a one-stop framework is that spring can integrate other frameworks. So learning and mastering is very important. After typing the cases in the course, I tried to turn dao into Mybtis to query data. It was successful, but I felt that Mybatis was a little rusty before. I still need to strengthen it. A good memory is not as good as a bad pen.
      Another day closer to work

Keywords: Java Spring

Added by CurrierSites on Tue, 05 Oct 2021 04:51:19 +0300