zoukankan      html  css  js  c++  java
  • springboot-@Configuration

    @Configuration

    1、通过配置文件注入类

    @Configuration

    @PropertySource("classpath:config/redis.properties")

    2、注册bean

    1)@Configuration+@Bean方式注册

    public class TestBean {
    
        private String username;
        private String url;
        private String password;
    
        public void sayHello() {
            System.out.println("TestBean sayHello...");
        }
    
        public String toString() {
            return "username:" + this.username + ",url:" + this.url + ",password:" + this.password;
        }
    
        public void start() {
            System.out.println("TestBean 初始化。。。");
        }
    
        public void cleanUp() {
            System.out.println("TestBean 销毁。。。");
        }
    }

    @Configuration
    public class TestConfiguration {
        public TestConfiguration() {
            System.out.println("TestConfiguration容器启动初始化。。。");
        }
    
        // @Bean注解注册bean,同时可以指定初始化和销毁方法
        // @Bean(name="testBean",initMethod="start",destroyMethod="cleanUp")
        @Bean
        @Scope("prototype")
        public TestBean testBean() {
            return new TestBean();
        }
    }

    public class TestMain {
        public static void main(String[] args) {
    
            // @Configuration注解的spring容器加载方式,用AnnotationConfigApplicationContext替换ClassPathXmlApplicationContext
            ApplicationContext context = new AnnotationConfigApplicationContext(TestConfiguration.class);
             //获取bean
            TestBean tb = (TestBean) context.getBean("testBean");
            tb.sayHello();
        }
    }

    2)@Configuration启动容器+@ComponentScan+@Component注册Bean
    //添加注册bean的注解
    @Component
    public class TestBean {
    
        private String username;
        private String url;
        private String password;
    
        public void sayHello() {
            System.out.println("TestBean sayHello...");
        }
    
        public String toString() {
            return "username:" + this.username + ",url:" + this.url + ",password:" + this.password;
        }
    
        public void start() {
            System.out.println("TestBean 初始化。。。");
        }
    
        public void cleanUp() {
            System.out.println("TestBean 销毁。。。");
        }
    }

    @Configuration
    //添加自动扫描注解,basePackages为TestBean包路径
    @ComponentScan(basePackages = "com.dxz.demo.configuration")
    public class TestConfiguration {
        public TestConfiguration() {
            System.out.println("TestConfiguration容器启动初始化。。。");
        }
    
        /*// @Bean注解注册bean,同时可以指定初始化和销毁方法
        // @Bean(name="testNean",initMethod="start",destroyMethod="cleanUp")
        @Bean
        @Scope("prototype")
        public TestBean testBean() {
            return new TestBean();
        }*/
    }

    public class TestMain {
        public static void main(String[] args) {
    
            // @Configuration注解的spring容器加载方式,用AnnotationConfigApplicationContext替换ClassPathXmlApplicationContext
            ApplicationContext context = new AnnotationConfigApplicationContext(TestConfiguration.class);
             //获取bean
            TestBean tb = (TestBean) context.getBean("testBean");
            tb.sayHello();
        }
    }

    备注1:可以使用基于 Java 的配置来管理 bean 的生命周期。@Bean 支持两种属性,即 initMethod 和destroyMethod,这些属性可用于定义生命周期方法。在实例化 bean 或即将销毁它时,容器便可调用生命周期方法。生命周期方法也称为回调方法,因为它将由容器调用。使用 @Bean 注释注册的 bean 也支持 JSR-250 规定的标准 @PostConstruct 和 @PreDestroy 注释。如果您正在使用 XML 方法来定义 bean,那么就应该使用 bean 元素来定义生命周期回调方法。

    备注2:可以用两种方式获取注解上下文

    1)ApplicationContext context = new AnnotationConfigApplicationContext(TestConfiguration.class);

    2)ApplicationContext ctx = new AnnotationConfigApplicationContext();

      ctx.register(AppContext.class)

    备注3:@Configuration+@Bean注册类时,如果有多个,则可以调用,相当于在application.xml中一个bean定义时引用另一个bean(注册bean的先后顺序)

    备注4:如果是在controller这类受容器管理的类中调用,则直接如下定义即可使用

    @Autowired

    TestBean testBean;

  • 相关阅读:
    Java学习笔记-Java中的常用类
    Java学习笔记-Java中的常用类
    Java学习笔记-泛型
    GeoIP简介与资源,定位经纬度,获取用户IP
    【插代码】手机号码所在地查询,引用代码
    最后一个对象属性后边不要加豆号的bug,血淋淋的教训啊,模块化开发IE7下的严重错误,养成好习惯
    CSS自定义滚动条样式
    css实现input文本框的双边框美化
    在函数内部定义的变量加与不加var的区别,匿名函数和有名函数内声明变量的区别
    js定时器关闭,js定时器停止,一次关闭所有正在运行的定时器,自定义函数clearIntervals()一次关闭所有正在运行的定时器
  • 原文地址:https://www.cnblogs.com/lichangyunnianxue/p/9675101.html
Copyright © 2011-2022 走看看