Spring注解驱动开发

1. @Configuration和@Bean注解

/**
 * 配置类等同之前的applicationContext.xml配置文件
 */
@Configuration //告诉spring这是一个配置类
public class MainConfig {

    /**
     * 使用注解在容器中注册bean,默认id是方法名,返回值为bean的类型
     *
     * @Bean的vale值可以指定生成bean的id
     */
    @Bean(value = "person1")
    public Person person() {
        return new Person("ghw", 20);
    }
}

2. @ComponentScan 包扫描

排除指定的类-Controller.class, Service.class

@ComponentScan(value = "com.ghw.springannotation", excludeFilters = {
        @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = {Controller.class, Service.class})
})
public class MainConfig {
      //省略
}

运行结果如图:

image.png

只包含指定的类 (因为useDefaultFilters 默认是true会扫描所有的类,所以要先改为false)

@ComponentScan(value = "com.ghw.springannotation", includeFilters = {
        @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = {Controller.class})
}, useDefaultFilters = false)
public class MainConfig {

    /**
     * 使用注解在容器中注册bean,默认id是方法名,返回值为bean的类型
     *
     * @Bean的vale值可以指定生成bean的id
     */
    @Bean(value = "person1")
    public Person person() {
        return new Person("ghw", 20);
    }
}

运行结果如图:


image.png

FilterType有多种,ANNOTATION是按照注解选择,ASSIGNABLE_TYPE是按照类型选择,@REGEX是正则表达式,@ASPECTJ是使用ASPECTJ注解,@CUSTOM是使用自定义规则

public enum FilterType {
    ANNOTATION,
    ASSIGNABLE_TYPE,
    ASPECTJ,
    REGEX,
    CUSTOM;

    private FilterType() {
    }
}

3.@Scope @Lazy

public class MainConfig {
    /**
     * 使用注解在容器中注册bean,默认id是方法名,返回值为bean的类型
     *
     * @Bean的vale值可以指定生成bean的id
     * @Scope value可取
     * singleton,单实例,默认值,ioc容器启动即创建对象并放入容器
     * prototype,多实例,使用的时候才去创建对象放入容器
     * request,同一次请求创建一个request
     * session,同一次请求创建一个session
     */
    @Scope(value = "prototype")
    @Lazy //懒加载,单实例的时候使用,使用的时候才去创建对象
    @Bean(value = "person1")
    public Person person() {
        System.out.println("调用此方法");
        return new Person("ghw", 20);
    }
}

4. @Conditional

按照条件注册bean
创建俩个Bean

/**
 * Conditional也可以放在类上,表示所有类都生效
 * @return
 */
@Conditional({WindowsCondition.class})
@Bean(value = "windows")
public Person person01() {
    return new Person("windows", 20);
}

@Conditional({LinuxCondition.class})
@Bean(value = "linux")
public Person person02() {
    return new Person("linux", 20);
}

创建俩个Condition
LinuxCondition

package com.ghw.springannotation.condition;

import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotatedTypeMetadata;

public class LinuxCondition implements Condition {

    /**
     * ConditionContext:判断条件使用的上下文环境
     * AnnotatedTypeMetadata:注释信息
     */
    @Override
    public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
        Environment environment = conditionContext.getEnvironment();
        String osname = environment.getProperty("os.name");
        if (osname.contains("Linux")) {
            return true;
        }
        return false;
    }
}

WindowsCondition

package com.ghw.springannotation.condition;

import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotatedTypeMetadata;


/**
 * 判断是否windows系统
 */
public class WindowsCondition implements Condition {


    /**
     * ConditionContext:判断条件使用的上下文环境
     * AnnotatedTypeMetadata:注释信息
     */
    @Override
    public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
        Environment environment = conditionContext.getEnvironment();
        String osname = environment.getProperty("os.name");
        if (osname.contains("Windows")) {
            return true;
        }
        return false;
    }
}

5. @Import

总结:
给容器中注册组件:

  1. @Controller @Service @Component @Repository 自己创建的类
  2. @Bean 第三方jar包内组件
  3. @Import 快速给容器中导入一个组件

在类名上面加@Import

@Import({Red.class, Green.class})
public class MainConfig {

结果如下,默认bean是全类名

信息: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@763d9750: startup date [Sat Aug 25 23:11:04 CST 2018]; root of context hierarchy
bean name:org.springframework.context.annotation.internalConfigurationAnnotationProcessor
bean name:org.springframework.context.annotation.internalAutowiredAnnotationProcessor
bean name:org.springframework.context.annotation.internalRequiredAnnotationProcessor
bean name:org.springframework.context.annotation.internalCommonAnnotationProcessor
bean name:org.springframework.context.event.internalEventListenerProcessor
bean name:org.springframework.context.event.internalEventListenerFactory
bean name:mainConfig
bean name:bookController
bean name:com.ghw.springannotation.importt.Red
bean name:com.ghw.springannotation.importt.Green
bean name:person1
bean name:linux
os name:Linux

Process finished with exit code 0

@ImportSelector

自定义要导入的bean类型,实现ImportSelector接口,实现方法selectImports,返回一个字符串数组,内容是要导入bean的全路径,annotationMetadata参数可以获取容器注解全部信息

package com.ghw.springannotation.importt;

import org.springframework.context.annotation.ImportSelector;
import org.springframework.core.type.AnnotationMetadata;

public class MyImportSelect implements ImportSelector {
    @Override
    public String[] selectImports(AnnotationMetadata annotationMetadata) {

        return new String[]{"com.ghw.springannotation.importt.Blue"};
    }
}
annotationMetadata中的内容

@ImportBeanDefinitionRegistrar

实现ImportBeanDefinitionRegistrar 接口,通过参数beanDefinitionRegistry的containsBeanDefinition方法判断当前容器中是否包含某个bean,然后通过registerBeanDefinition方法注册新的bean

package com.ghw.springannotation.importt;

import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;

public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
    @Override
    public void registerBeanDefinitions(AnnotationMetadata annotationMetadata, BeanDefinitionRegistry beanDefinitionRegistry) {
        Boolean red = beanDefinitionRegistry.containsBeanDefinition("com.ghw.springannotation.importt.Red");
        Boolean green = beanDefinitionRegistry.containsBeanDefinition("com.ghw.springannotation.importt.Green");
        if (red && green) {
            RootBeanDefinition beanDefinition = new RootBeanDefinition(RainBow.class);
            beanDefinitionRegistry.registerBeanDefinition("rainbow", beanDefinition);
        }

    }
}
@Import({Red.class, Green.class, MyImportSelect.class, MyImportBeanDefinitionRegistrar.class})
public class MainConfig {

6. @FactoryBean

通过工厂Bean创建对象

package com.ghw.springannotation.factory;

import com.ghw.springannotation.importt.Color;
import org.springframework.beans.factory.FactoryBean;

/**
 * Color类的工厂Bean,工厂Bean创建Color类,返回color对象,加入容器
 */
public class ColorFactoryBean implements FactoryBean<Color> {
    @Override
    public Color getObject() throws Exception {
        System.out.println("调用getObject方法返回Color对象");
        return new Color();
    }

    @Override
    public Class<?> getObjectType() {
        return Color.class;
    }

    @Override
    /**
     * true 单例
     * false 多例
     */
    public boolean isSingleton() {
        return false;
    }
}

在Config类中加入ColorFactoryBean的注解

@Bean
public ColorFactoryBean ColorFactoryBean() {
    return new ColorFactoryBean();
}

测试方法

@Test
public void test() throws Exception {

    printBeans(context);

    Object bean = context.getBean("ColorFactoryBean");
    Object bean2 = context.getBean("ColorFactoryBean");
    Color color = (Color) bean;
    System.out.println(bean == bean2);
    color.print();


    //取得当前环境
    ConfigurableEnvironment environment = context.getEnvironment();
    System.out.println("os name:" + environment.getProperty("os.name"));

}

public void printBeans(AnnotationConfigApplicationContext context) {
    String[] nameForType = context.getBeanDefinitionNames();
    //遍历输出注册的bean
    for (String s : nameForType) {
        System.out.println("bean name:" + s);
    }
}

总结:

image.png

7. initMethod和destroyMethod

首先定义Car类,并编写构造方法init方法和destory方法

package com.ghw.springannotation.bean;

/**
 * Bean的init和destory方法
 */
public class Car {
    public Car() {
        System.out.println("Car构造方法执行");
    }

    public void init() {
        System.out.println("Car初始化方法");
    }

    public void destory() {
        System.out.println("Car销毁方法");
    }
}

在MainConfig类中添加注解,在注解上添加initMethod,destroyMethod

@Bean(initMethod = "init",destroyMethod = "destory")
public Car car() {
    return new Car();
}
public void test02() {
    Car car = (Car) context.getBean("car");
    System.out.println(car);
    context.close();
}

输出结果如下

信息: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@6f79caec: startup date [Sun Aug 26 23:19:52 CST 2018]; root of context hierarchy
Car构造方法执行
Car初始化方法

八月 26, 2018 11:19:52 下午 org.springframework.context.support.AbstractApplicationContext doClose
信息: Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@6f79caec: startup date [Sun Aug 26 23:19:52 CST 2018]; root of context hierarchy
enter test02
com.ghw.springannotation.bean.Car@49d904ec
Car销毁方法

从结果可以看出容器创建的时候就执行构造方法然后执行init方法,容器关闭之后才执行test02,destory方法。此处有疑惑,正常不应该是先执行test再关闭容器。


以上是单实例的情况,如果是多实例,则会在使用bean的时候执行构造方法和初始化方法,容器关闭也不会执行销毁方法
如下图:

八月 26, 2018 11:22:27 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@6f79caec: startup date [Sun Aug 26 23:22:27 CST 2018]; root of context hierarchy
八月 26, 2018 11:22:28 下午 org.springframework.context.support.AbstractApplicationContext doClose
信息: Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@6f79caec: startup date [Sun Aug 26 23:22:27 CST 2018]; root of context hierarchy
enter test02
Car构造方法执行
Car初始化方法
com.ghw.springannotation.bean.Car@4b5d6a01

7. InitializingBean, DisposableBean接口

功能和#6相似

package com.ghw.springannotation.bean;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

/**
 * 测试InitializingBean和DisposableBean接口
 */
@Component
public class Cat implements InitializingBean, DisposableBean {

    public Cat() {
        System.out.println("Cat执行构造方法");
    }

    @Override
    public void destroy() throws Exception {
        System.out.println("Cat销毁方法");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("Cat初始化方法");
    }
}

test方法

package com.ghw.springannotation.bean;

import com.ghw.springannotation.MainConfig;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class CatTest {
    //获取context
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MainConfig.class);

    @Test
    public void test() {
        context.getBean("cat");
        context.close();
    }

}

结果如下:

八月 26, 2018 11:36:14 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@67117f44: startup date [Sun Aug 26 23:36:14 CST 2018]; root of context hierarchy
Cat执行构造方法
Cat初始化方法
八月 26, 2018 11:36:14 下午 org.springframework.context.support.AbstractApplicationContext doClose
信息: Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@67117f44: startup date [Sun Aug 26 23:36:14 CST 2018]; root of context hierarchy
Cat销毁方法

8. @PostConstruct和@PreDestory

这俩个注解是JSR250中提出的
新建Dog类做测试

package com.ghw.springannotation.bean;

import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

/**
 * 测试@PostConstruct和@PreDestory
 */
@Component
public class Dog {
    public Dog() {
        System.out.println("Dog构造方法执行");
    }

    /**
     * PostConstruct注解的方法在Bean创建完成并且属性赋值之后执行
     */
    @PostConstruct
    public void init() {
        System.out.println("Dog初始化方法");
    }

    /**
     * PreDestroy注解的方法在容器销毁Bean之前执行
     */
    @PreDestroy
    public void destory() {
        System.out.println("Dog销毁方法");
    }
}

DogTest.java

package com.ghw.springannotation.bean;

import com.ghw.springannotation.MainConfig;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import static org.junit.Assert.*;

public class DogTest {

    //获取context
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MainConfig.class);

    @Test
    public void test() {
        context.getBean("dog");
        context.close();
    }
}

运行结果如下:


image.png

9. BeanPostProcessor接口,Bean的后置处理器

postProcessBeforeInitialization在初始化之前执行
postProcessAfterInitialization在初始化之后执行

package com.ghw.springannotation.bean;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;

/**
 * @author gaoho
 */
@Component
public class MyBeanPostProcessor implements BeanPostProcessor {
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("beanName:" + beanName + "bean:" + bean + "Before");
        return bean;
    }

    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("beanName:" + beanName + "bean:" + bean + "After");
        return bean;
    }
}

执行结果如下:

beanName:dogbean:com.ghw.springannotation.bean.Dog@4e91d63f---Before
beanName:dogbean:com.ghw.springannotation.bean.Dog@4e91d63f---After

BeanPostProcessor原理:

image.png
image.png
image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,736评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,167评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,442评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,902评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,302评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,573评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,847评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,562评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,260评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,531评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,021评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,367评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,016评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,068评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,827评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,610评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,514评论 2 269

推荐阅读更多精彩内容

  • 1.1 Spring IoC容器和bean简介 本章介绍了Spring Framework实现的控制反转(IoC)...
    起名真是难阅读 2,535评论 0 8
  • 1.1 spring IoC容器和beans的简介 Spring 框架的最核心基础的功能是IoC(控制反转)容器,...
    simoscode阅读 6,633评论 2 22
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,099评论 18 139
  • 容器 使用@Configuration来创建一个配置类 使用@Bean为容器中注册一个组件 使用@Import快速...
    随手点灯阅读 913评论 0 0
  • 云和海分居两地,人和人相聚别离。一个在夏天停步,另一个在秋天打探消息。一个就是喜欢,另一个觉得麻烦。总会经历心甘情...
    木卯丁阅读 186评论 0 0