ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); Car myCar = (Car) context.getBean("carBean"); myCar.drive();
@Configuration public class AppConfig { @Bean public Car carBean() { return new Car(new Engine(), new Transmission()); } } public class Main { public static void main(String[] args) { ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class); Car myCar = (Car) context.getBean("carBean"); myCar.drive(); } }In this example, we are using Java configuration to define our Spring beans. We have defined a "Car" bean which requires an "Engine" and "Transmission" bean. We then retrieve an instance of the "Car" bean from the ApplicationContext and call the "drive" method on the "myCar" object. Overall, the org.springframework.context ApplicationContext package provides a powerful container for developers to manage Spring beans within their applications.