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

           

        }

    }

  • 相关阅读:
    Linux基础命令(一)
    You've made choice
    protege推理
    字符编码
    第二次作业
    数据类型-集合set
    数据类型-元组&字典
    数据类型-列表
    数据类型-数值&字符串
    流程控制之for循环
  • 原文地址:https://www.cnblogs.com/jishumonkey/p/12875893.html
Copyright © 2011-2022 走看看