zoukankan      html  css  js  c++  java
  • spring boot 启动 开启注解 加载 bean

    业务描述:创建一个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();

           

        }

    }

  • 相关阅读:
    图论
    城市吸引力指数
    bzoj3529(莫比乌斯反演+离线+树状数组)
    强制关闭tomcat
    bzoj2154(莫比乌斯反演)
    等差数列求和模板
    联想的显示屏校准(困难)
    bzoj2301(莫比乌斯反演)
    莫比乌斯反演模版
    菜鸟物流的运输网络(网络流)
  • 原文地址:https://www.cnblogs.com/jishumonkey/p/12875893.html
Copyright © 2011-2022 走看看