@Configuration //注册该类作为javaconfig public class ApplicationConfig { @Bean(name = "helloBean") //注册该对象为bean public Helloworld helloworld(){ return new HelloworldImpl(); } }
Application
public class Application { public static void main(String[] args){ ApplicationContext applicationContext = new AnnotationConfigApplicationContext(ApplicationConfig.class); Helloworld helloworld = (Helloworld) applicationContext.getBean("helloBean"); helloworld.printHelloWorld("This is Spring_JavaConfg!hhhh"); } }
2.用@Import加载多个配置文件
@Configuration @Import({ CustomerConfig.class, SchedulerConfig.class }) public class AppConfig { }