Skip to content
Advertisement

How to get ApplicationContext in xml definition?

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.

Advertisement

Answer

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);
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement