Spring @Import注解Demo

@Import可以把第三方定义的java类加载到spring ioc容器中

配置文件

import com.gxf.importdemo.ImportBean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;


@Configuration
@ComponentScan("com.gxf.service")
@EnableAsync
@Import(ImportBean.class)
public class AsyncConfig implements AsyncConfigurer {
    @Override
    public Executor getAsyncExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(10);
        executor.setMaxPoolSize(100);
        executor.setQueueCapacity(10);
        executor.initialize();
        return executor;
    }
}

通过@Import注解把ImportBean 加入到spring ioc容器中,ImportBean没有任何spring ioc容器相关的注解

public class ImportBean {
    public void test() {
        System.out.println("test");
    }
}

可以直接通过@AutoWired注解使用

    @Autowired
    private ImportBean importBean;
Please call me JiangYouDang!
原文地址:https://www.cnblogs.com/luckygxf/p/15417719.html