Spring Boot学习笔记(三)

Bean标签

Spring中Bean标签在Spring Boot中的使用:

图片介绍:

Spring Boot学习笔记(三)_第1张图片

javaConfig:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 
 *     
 *     
 * 
 */
@Configuration
public class Config {

    @Bean(name={"sb"},initMethod = "init",destroyMethod = "destroy")
    public SomeBean someBean(){
        return new SomeBean();
    }
}
测试代码:
    @Test
    public void testDemo() throws IOException {
        AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(Config.class);
        SomeBean sb = ctx.getBean("sb",SomeBean.class);
        System.out.println(sb);
        ctx.close();
    }


测试结果:
一月 10, 2018 7:57:26 下午 org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@7cef4e59: startup date [Wed Jan 10 19:57:26 CST 2018]; root of context hierarchy
===init===
一月 10, 2018 7:57:26 下午 org.springframework.context.annotation.AnnotationConfigApplicationContext doClose
信息: Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@7cef4e59: startup date [Wed Jan 10 19:57:26 CST 2018]; root of context hierarchy
com.fan.springboot.SomeBean@400cff1a
===destroy===
以上介绍了id,name,init-method,destroy-method在Spring Boot中的配置,其中init-method和destroy-method另外两种配置方式如下:

public class SomeBean {
    @PostConstruct
    public void init(){
        System.out.println("===init===");
    }
    @PreDestroy
    public void destroy(){
        System.out.println("===destroy===");
    }
}


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

public class SomeBean implements InitializingBean,DisposableBean {

    public void destroy(){
        System.out.println("===destroy===");
    }
    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("===init===");
    }
}
bean标签的scope属性不在bean标签的参数上而在类上(未测试):

/**
 * 作用域:
 * 基本作用域(singleton、prototype)
 * singleton(单例,默认)
 * prototype原型,多例
 * Web 作用域(reqeust、session、globalsession)
 * 自定义作用域
 */
@Scope("prototype")
public class SomeBean {

    public void init(){
        System.out.println("===init===");
    }
    public void destroy(){
        System.out.println("===destroy===");
    }
}
spring中的FactoryBean在Spring Boot中的应用代码示例:
public class SomeBeanFactory implements FactoryBean {
    @Override
    public SomeBean getObject() throws Exception {
        return new SomeBean();
    }

    @Override
    public Class getObjectType() {
        return SomeBean.class;
    }

    @Override
    public boolean isSingleton() {
        return true;
    }
}
@Configuration
public class Config {

    @Bean
    public SomeBeanFactory someBean(){
        return new SomeBeanFactory();
    }
}
测试代码:

    @Test
    public void testDemo() throws IOException {
        AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(Config.class);
        SomeBean sb = ctx.getBean("someBean",SomeBean.class);
        System.out.println(sb);
        SomeBeanFactory someBeanFactory = ctx.getBean("&someBean",SomeBeanFactory.class);
        System.out.println(someBeanFactory);
    }
测试结果:
com.fan.springboot.SomeBean@52aa2946
com.fan.springboot.SomeBeanFactory@4de5031f





你可能感兴趣的:(Spring,Boot)