springboot的configuration注解

1.@configuration
该注解声明一个配置类,相当于spring的一个xml配置文件中的beans,被@configuration打上注解的类中可包含多个@bean注解的内容,相当于xml配置文件中beans里边的内容。

package com.example.springboot.bean;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * Created by ZhangPei on 2019/7/18.
 */
@Configuration
public class ThreadPoolConfiguration {
    @Value("${threadpool.core-pool-size}")
    private int corePoolSize;

    @Value("${threadpool.max-pool-size}")
    private int maxPoolSize;

    @Value("${threadpool.queue-capacity}")
    private int queueCapacity;

    @Value("${threadpool.keep-alive-seconds}")
    private int keepAliveSeconds;


    @Bean
    public ThreadPoolTaskExecutor threadPoolCreater() {
        System.out.println("被bean注解打上的方法是不是会自动执行?");
        ThreadPoolTaskExecutor threadPoolExecutor = new ThreadPoolTaskExecutor();
        threadPoolExecutor.setCorePoolSize(this.corePoolSize);
        threadPoolExecutor.setMaxPoolSize(this.maxPoolSize);
        threadPoolExecutor.setQueueCapacity(queueCapacity);
        threadPoolExecutor.setKeepAliveSeconds(this.keepAliveSeconds);
        threadPoolExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardPolicy());
        return threadPoolExecutor;
    }

    //没打@bean注解的方法不会执行
    public Thread getThread() {
        System.out.println("没打bean注解会不会执行");
        return null;
    }
}

以上内容,在工程初始化的时候,被@bean注解的方法会自动执行,然后将返回值注入spring当做bean使用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值