方式1:@Import({XXX.class,XXX.class})
配置类:
package com.jarvis.config;
@Import({
com.jarvis.pojo.User.class}) //这里将User类注册到容器
@Configuration
public class JarvisConfig {
}
要import的类
注意,这个类没有加@Component,正常情况下无法被注册到容器中
package com.jarvis.pojo;
public class User {
}
测试类
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(JarvisConfig.class);
System.out.println(context.getBean(User.class));
}
======output:
com.jarvis.pojo.User