Car 接口
package com.yzk.springboot.ann;
public interface Car {
public void print();
}
BMW
package com.yzk.springboot.ann;
import org.springframework.stereotype.Component;
@Component
public class BMW implements Car {
@Override
public void print() {
System.out.println("我是宝马汽车");
}
}
JavaConfigA
package com.yzk.springboot.ann;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
//相当于以前的Application.xml文件
//代表AOC容器
@Configuration
public class JavaConfigA {
@Bean("BMW")
public Car getBMW() {
return new BMW();
}
}
ParentConfig
package com.yzk.springboot.ann;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
@Configuration
@Import({ JavaConfigA.class, JavaConfigB.class })
public class ParentConfig {
}
测试类
ContextLoadderTest
package com.yzk.springboot.ann;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class ContextLoadderTest {
static AnnotationConfigApplicationContext context;
public static void main(String[] args) {
context = new AnnotationConfigApplicationContext(ParentConfig.class);
Car BMW = (Car) context.getBean("BMW");
BMW.print();
}
}