业务描述:创建一个cache类然后交给spring 管理。

 

@Component

@Scope("singleton")

public class Cache {

 

    public Cache() {

       System.out.println("cache()");

    }

    @PostConstruct

    public void init() {

        System.out.println("init()");

    }

    @PreDestroy

    public void destory() {

        System.out.println("destory");

    }

}

 

 

 

 

 

 

 

 

修改sringboot 启动类,进行bean的获取及测试。

 

package com.cy;

@SpringBootApplication

public class CgbSpringbootApplication implements ApplicationContextAware {

    private static AnnotationConfigApplicationContext ctx;

    @Override

    public void setApplicationContext(ApplicationContext

applicationContext) throws BeansException {

      ctx=(AnnotationConfigApplicationContext)applicationContext;

    }

    public static void main(String[] args) {

        SpringApplication.run(CgbSpringbootApplication.class, args);

        System.out.println(ctx.getBean("cache"));

        System.out.println(ctx.getBean("cache"));

        ctx.close();

       

    }

}