1、Spring注解开发,第一天

第一天:Spring annotation开发

目录:1、@Configuration与@Bean给容器注册组件 2、@ConponentScan自动扫描注解

一、@Configuration与@Bean给容器注册组件

1、旧版本中创建配置文件和Bean

//实体类

package com.lee.bean;

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

@Data//生成GET SET方法
@NoArgsConstructor//无参构造函数
@AllArgsConstructor//有参构造函数
public class Person {

    private Integer id;

    private String username;

    private String gender;

}

配置文件

<?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>

测试

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);
    }
}

结果:

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

2、Annotation中的配置文件和Bean

配置类

package com.lee.config;

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

@Configuration//告诉Spring这是一个配置类
public class MainConfig {

    //Bean 给容器注册一个bean,类型为返回值的类型,id默认使用方法名作为id
    //@Bean("名字")可以给bean修改名称
    @Bean
    public Person person(){
        return new Person(2,"lee2","male2");
    }
}

测试类

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);
    }
}

测试结果:

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

总结

@Configuration等同于以前的配置文件—告诉spring这是一个配置类

@Bean 给容器注册一个bean,类型为返回值的类型,id默认使用方法名作为id

二、@ConponentScan自动扫描注解

1、原来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、Annotation中的自动扫描

准备类

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 {

}

配置文件自动扫描

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")//自动扫描 value指定要扫描的包
@Configuration//告诉Spring这是一个配置类
public class MainConfig {

    //Bean 给容器注册一个bean,类型为返回值的类型,id默认使用方法名作为id
    //@Bean("名字")可以给bean修改名称
    @Bean
    public Person person(){
        return new Person(2,"lee2","male2");
    }


}

测试类

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);
        }
    }
}

测试结果

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过滤功能

源码,component中有includeFilters和excludeFilters功能,能够过滤要筛选的包

//
// 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 {};
    }
}

配置类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")//自动扫描 value指定要扫描的包
@Configuration//告诉Spring这是一个配置类
@ComponentScan(value="com.lee",excludeFilters = {
        @ComponentScan.Filter(type=FilterType.ANNOTATION,value = Controller.class)
})
public class MainConfig {

    //Bean 给容器注册一个bean,类型为返回值的类型,id默认使用方法名作为id
    //@Bean("名字")可以给bean修改名称
    @Bean
    public Person person(){
        return new Person(2,"lee2","male2");
    }


}

测试1

mainConfig
bookDao
bookService
person

配置类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")//自动扫描 value指定要扫描的包
@Configuration//告诉Spring这是一个配置类
//@ComponentScan(value="com.lee",excludeFilters = {
//        @ComponentScan.Filter(type=FilterType.ANNOTATION,value = Controller.class)
//})
//includeFilters必须和useDefaultFilters=false一起使用才能起到作用
//useDefaultFilters默认是true,即默认扫描全部的包
@ComponentScan(value = "com.lee",useDefaultFilters=false,includeFilters = {
        @ComponentScan.Filter(type = FilterType.ANNOTATION,value = Controller.class)
})
public class MainConfig {

    //Bean 给容器注册一个bean,类型为返回值的类型,id默认使用方法名作为id
    //@Bean("名字")可以给bean修改名称
    @Bean
    public Person person(){
        return new Person(2,"lee2","male2");
    }


}

测试2

mainConfig
bookController
person
上一篇:javacv开发详解之1:调用本机摄像头视频(建议使用javaCV最新版本)


下一篇:2、Spring注解开发,第二天