Spring framework review

1. Review of spring core concepts

Spring has two cores: IOC (DI) based on factory pattern and AOP based on dynamic agent.

IOC (DI) refers to controller inversion (dependency injection). In the past, a class object instance must be created by itself. After using spring, it does not need to be created by itself. It is created by spring. If necessary, it is directly obtained from spring, and if there are dependencies, spring will automatically inject through reflection.

AOP is to add additional logic before and after it does not affect the normal execution process. For example, permission, log, etc., the business logic of the execution is normal, and the execution knowledge can judge the permission and record the log.

2.springXML programming - IOC

Let's review the ioc code writing: classpathxml * ApplicationContext

Dependencies required to import the project

<dependencies>

    <!--unit testing -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>

    <!--Import Spring Dependence of-->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.3.7.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>4.3.7.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>4.3.7.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
        <version>4.3.7.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-expression</artifactId>
        <version>4.3.7.RELEASE</version>
    </dependency>

    <!--Framework logging tool-->
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.14</version>
    </dependency>

    <!--Lombok-->
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.8</version>
        <scope>provided</scope>
    </dependency>

</dependencies>

Spring configuration file

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       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">

    <bean id="myDate" class="java.util.Date"></bean>

</beans>

Spring testing

package com.bruceliu.test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.Date;


public class TestSpringIoc {

    /**
     * xml based configuration file
     */
    @Test
    public void test() throws Exception {
        //Initializing the spring container through xml
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        System.out.println(context.getBean(Date.class));

        //Get the type IDs of all bean s in spring from
        String[] beanDefinitionNames = context.getBeanDefinitionNames();
        for (String beanDefinitionName : beanDefinitionNames) {
            System.out.println(beanDefinitionName);
        }
    }

}

3.spring annotation programming - IOC

3.1. introduce

The above is to register beans through configuration files. In fact, spring now advocates annotation driven bean registration

3.2. Component registration

3.2.1. Configuration class & bean annotation

@Configuration: the class with this annotation is equivalent to a traditional ApplicationContext XXX xml
@Bean: marking @ bean on the way in the class marked with @ Configuration is equivalent to applying context XXX A < bean id = "userDao" class = "com. XML" configured by bruceliu. dao. userDao”>
Dao's name is the method name by default. If you want to change the method name, use @ Bean("beanName")

//Annotation class = = configuration file
@Configuration //Tell spring that this is an annotation class
public class MainConfig {

    //It is equivalent to configuring < bean id = "" class = "com. Bruceliu. Dao. Userdao" > < bean / > in xml
    @Bean("userDao") //Specifies the name of the bean
    public UserDao userDao01(){
        return new UserDaoImpl();
    }
}
public class MainConfigTest {

    @Test
    public void testIoc(){
        //1 traditional xml mode
        //ApplicationContext context = new ClassPathXmlApplicationContext(
        //          "applicationContext.xml");
        //UserDao userDao = context.getBean(UserDao.class);
        //System.out.println(userDao.loadUser(1L));


        //2. The annotation method does not require configuration files. How to configure bean s? When obtaining the container, the configuration file was originally formulated, but now the configuration class is formulated
        ApplicationContext context = new AnnotationConfigApplicationContext(MainConfig.class);
        UserDao userDao = context.getBean(UserDao.class);
        System.out.println(userDao.loadUser(1L));

        String[] beanNamesForType = context.getBeanNamesForType(UserDao.class);
        for (String beanName : beanNamesForType) {

            System.out.println(beanName);
        }

    }
}

3.2.3.@ComponentScan scan bean

We used to use spring instead of configuring bean s one by one in xml. We added @ Repository, @ Service,@Controller @Component to the class, and @ AutoWired annotation can be used for injection. All these functions require us to configure package scanning < context: component scan base package = "com. Bruceliu" / >
However, now annotation driven development has no configuration file and cannot be configured. However, @ ComponentScan is provided. We can add this annotation to the configuration class. The same is true. We can also scan the relevant annotations of the configuration package items and complete automatic injection.

Next, let's look at the scanning component first, and then the injection

package com.bruceliu.service;

import com.bruceliu.bean.User;
import org.springframework.stereotype.Service;

@Service
public class UserService {
    public User getUser(Long id){
        System.out.println("userservice...");
        return null;
    }
}
package com.bruceliu.controller;

import com.bruceliu.bean.User;
import org.springframework.stereotype.Controller;


@Controller
public class UserController {

    //Print directly without copying the page
    public User getUser(Long id){
        System.out.println("usercontroller...");
        return null;
    }
}
//Annotation class = = configuration file
@Configuration //Tell spring that this is an annotation class
@ComponentScan("com.bruceliu")
public class MainConfig {

    //It is equivalent to configuring < bean id = "" class = "com. Bruceliu. Dao. Userdao" > < bean / > in xml
    @Bean("userDao") //Specifies the name of the bean
    public UserDao userDao01(){
        return new UserDaoImpl();
    }
}
public class MainConfigTest {

    @Test
    public void testIoc(){
        ApplicationContext context = new AnnotationConfigApplicationContext(MainConfig.class);
        for (String beanName : context.getBeanDefinitionNames()) {
            System.out.println(beanName);
        }
    }

}

3.2.4. Advanced Grammar

package com.bruceliu.spring;

import com.bruceliu.dao.UserDao;
import com.bruceliu.dao.impl.UserDaoImpl;
import org.springframework.context.annotation.*;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;


//Annotation class = = configuration file
//Annotation class = = configuration file
@Configuration //Tell spring that this is an annotation class
//@ComponentScan(value = "com.bruceliu")
//includeFilters = Filter [] contains filters
//excludeFilters = Filter [] exclude filters
//useDefaultFilters = use default filters
//Multiple scan configuration methods: use ComponentScans
@ComponentScans(value = {
        @ComponentScan(value = "com.bruceliu",
        //excludeFilters = @ComponentScan.Filter(type = FilterType.ANNOTATION,classes = {Controller.class, Service.class})
                //Turn off the default all scan includeFilters to take effect
        includeFilters = @ComponentScan.Filter(type = FilterType.ANNOTATION,classes = {Service.class}),useDefaultFilters = false
      )
})
public class MainConfig {

    //It is equivalent to configuring < bean id = "" class = "com. Bruceliu. Dao. Userdao" > < bean / > in xml
    @Bean("userDao") //Specifies the name of the bean
    public UserDao userDao01() {
        return new UserDaoImpl();
    }
}
public class MainConfigTest {

    @Test
    public void testIoc(){
        ApplicationContext context = new AnnotationConfigApplicationContext(MainConfig.class);
        for (String beanName : context.getBeanDefinitionNames()) {
            System.out.println(beanName);
        }
    }

}

Note 1 separate test exclusion and inclusion
Note 2: impact of other configuration classes - the scanning itself also includes configuration classes

Posted by bluegreen1 on Fri, 13 May 2022 06:41:48 +0300