1. Spring Annotation Development, Day 1

Keywords: Programming Spring Java xml Lombok

Day 1: Spring annotation development

Directory: 1, @Configuration and @Bean to Register Components for Containers 2, @ConponentScan Auto Scan Annotations

I. @Configuration and @Bean Register Components for Containers

1. Creating configuration files and beans in older versions

/ / entity class

package com.lee.bean;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data//GET SET Generation Method
@NoArgsConstructor//non-parameter constructor
@AllArgsConstructor//Parametric constructor
public class Person {

    private Integer id;

    private String username;

    private String gender;

}

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="person" class="com.lee.bean.Person">
        <property name="id" value="1"></property>
        <property name="username" value="lee"></property>
        <property name="gender" value="male"></property>
    </bean>

</beans>

test

package com.lee;

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

public class MainTest {

    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
        Object person = context.getBean("person");
        System.out.println(person);
    }
}

Result:

Person(id=1, username=lee, gender=male)

2. Configuration files and beans in Annotation

Configuration class

package com.lee.config;

import com.lee.bean.Person;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration//Tell Spring that this is a configuration class
public class MainConfig {

    //Bean registers a bean to the container with the type of the return value, and id defaults to the method name as id
    //@ Bean("name") can change the name of the bean
    @Bean
    public Person person(){
        return new Person(2,"lee2","male2");
    }
}

Test class

package com.lee;

import com.lee.bean.Person;
import com.lee.config.MainConfig;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class MainTest {

    public static void main(String[] args) {
//        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
//        Object person = context.getBean("person");
//        System.out.println(person);

        ApplicationContext context = new AnnotationConfigApplicationContext(MainConfig.class);
        Person bean = context.getBean(Person.class);
        System.out.println(bean);
    }
}

Test results:

Person(id=2, username=lee2, gender=male2)

summary

@ Configuration is equivalent to the previous configuration file -- tell spring that this is a configuration class

@Bean Register a bean for the container with the type of the return value, and the id defaults to the method name as id

2. @ConponentScan Automatic Scan Annotation

1. Original xml

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

    <context:component-scan base-package="com.lee"></context:component-scan>

</beans>

2. Automatic Scanning in Annotation

Preparation Class

package com.lee.controller;

import org.springframework.stereotype.Controller;

@Controller
public class BookController {

}
package com.lee.service;

import org.springframework.stereotype.Service;

@Service
public class BookService {

}
package com.lee.dao;

import org.springframework.stereotype.Repository;

@Repository
public class BookDao {

}

Configuration file automatic scanning

package com.lee.config;

import com.lee.bean.Person;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@ComponentScan("com.lee")//Autoscan value specifies the package to be scanned
@Configuration//Tell Spring that this is a configuration class
public class MainConfig {

    //Bean registers a bean to the container with the type of the return value, and id defaults to the method name as id
    //@ Bean("name") can change the name of the bean
    @Bean
    public Person person(){
        return new Person(2,"lee2","male2");
    }


}

Test class

package com.lee;

import com.lee.bean.Person;
import com.lee.config.MainConfig;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class MainTest {

    public static void main(String[] args) {

        ApplicationContext context = new AnnotationConfigApplicationContext(MainConfig.class);
        String[] names = context.getBeanDefinitionNames();
        for(String name : names){
            System.out.println(name);
        }
    }
}

test result

org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalRequiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
mainConfig
bookController
bookDao
bookService
person

3. @ComponentScan filtering function

Source code, component has include filters and exclude filters, which can filter the package to be filtered

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package org.springframework.context.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.beans.factory.support.BeanNameGenerator;
import org.springframework.core.annotation.AliasFor;

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
@Documented
@Repeatable(ComponentScans.class)
public @interface ComponentScan {
    
    @AliasFor("basePackages")
    String[] value() default {};

    boolean useDefaultFilters() default true;

    ComponentScan.Filter[] includeFilters() default {};

    ComponentScan.Filter[] excludeFilters() default {};

    @Retention(RetentionPolicy.RUNTIME)
    @Target({})
    public @interface Filter {
        FilterType type() default FilterType.ANNOTATION;

        @AliasFor("classes")
        Class<?>[] value() default {};

        @AliasFor("value")
        Class<?>[] classes() default {};

        String[] pattern() default {};
    }
}

Configuration class 1

package com.lee.config;

import com.lee.bean.Person;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Controller;

//@ ComponentScan("com.lee")// Automatic Scan value specifies the package to be scanned
@Configuration//Tell Spring that this is a configuration class
@ComponentScan(value="com.lee",excludeFilters = {
        @ComponentScan.Filter(type=FilterType.ANNOTATION,value = Controller.class)
})
public class MainConfig {

    //Bean registers a bean to the container with the type of the return value, and id defaults to the method name as id
    //@ Bean("name") can change the name of the bean
    @Bean
    public Person person(){
        return new Person(2,"lee2","male2");
    }


}

Test 1

mainConfig
bookDao
bookService
person

Configuration class 2

package com.lee.config;

import com.lee.bean.Person;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Controller;

//@ ComponentScan("com.lee")// Automatic Scan value specifies the package to be scanned
@Configuration//Tell Spring that this is a configuration class
//@ComponentScan(value="com.lee",excludeFilters = {
//        @ComponentScan.Filter(type=FilterType.ANNOTATION,value = Controller.class)
//})
//includeFilters must be used with useDefaultFilters=false to work
//useDefaultFilters defaults to true, which means that all packages are scanned by default.
@ComponentScan(value = "com.lee",useDefaultFilters=false,includeFilters = {
        @ComponentScan.Filter(type = FilterType.ANNOTATION,value = Controller.class)
})
public class MainConfig {

    //Bean registers a bean to the container with the type of the return value, and id defaults to the method name as id
    //@ Bean("name") can change the name of the bean
    @Bean
    public Person person(){
        return new Person(2,"lee2","male2");
    }


}

Test 2

mainConfig
bookController
person

Posted by GaryAC on Tue, 01 Oct 2019 13:18:03 -0700