如何在xml定义中获得ApplicationContext ?



@Autowired
private ApplicationContext appContext;

?

注::当我试图谷歌它我得到了关于ApplicationContext如何工作的一百万结果,但不是如何在xml定义中得到它。该项目都是使用xml定义编写的,所以我需要找到一种方法,如何做到没有注释。

首先,在applicationContext.xml文件中配置spring bean例如:-

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

加载spring配置文件并从spring容器中检索bean

ClassPathXmlApplicationContext context = 
new ClassPathXmlApplicationContext("applicationContext.xml");

MyClass myBean = context.getBean("beanId",MyClass.class);

相关内容

  • 没有找到相关文章

最新更新