Home > database >  How can i get my Application Context in xml defination while working in Spring Boot
How can i get my Application Context in xml defination while working in Spring Boot

Time:10-29

What is the xml equivalent of the

@Autowired
private ApplicationContext appContext;
?

P.S.: When I try to google it I got a million results about how ApplicationContext works but not how to get it in the xml definition. The project is all writen using xml definition so I need to find a way how to do it without anotations.

CodePudding user response:

First, configure your spring beans in file applicationContext.xml For example:-

        <bean id="beanId"
         class="com.java.spring.MyClassName">
        </bean>

load the spring configuration file and retrieve bean from spring container

        ClassPathXmlApplicationContext context = 
                     new ClassPathXmlApplicationContext("applicationContext.xml");
        
        MyClass myBean = context.getBean("beanId",MyClass.class);
  • Related