AnnotationConfigApplicationContext是在独立应用程序中加载Spring bean的最重要的类。
独立程序
public class ApplicationX {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class); Service someService = context.getBean(Service.class);
appx.start();
}
private void start() {
// do stuff
}
}配置类
@ComponentScan(basePackages = "YOUR_COMPONENTS_PACKAGE")
@Configuration
public class SpringConfig {
@Component
public Service getService() {
return new SomeService();
}
}