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

           

        }

    }

  • 相关阅读:
    AC自动机
    【洛谷P1972】HH的项链
    【洛谷P4341】外星联络
    【洛谷P4576】棋盘游戏
    【JZOJ3800】败屩妖
    【JZOJ3798】临洮巨人
    【洛谷P3830】随机树
    【JZOJ3799】青蛙神
    牛客练习赛56 题解
    【洛谷P5300】与或和
  • 原文地址:https://www.cnblogs.com/jishumonkey/p/12875893.html
Copyright © 2011-2022 走看看