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;

 

posted on 2021-10-17 18:44  luckygxf  阅读(33)  评论(0编辑  收藏  举报

导航