装配Bean——通过XML装配bean

来源:互联网 发布:cats vs dogs数据下载 编辑:程序博客网 时间:2024/05/22 11:36

   在Spring刚出现的时候,XML是描述配置的主要方式。在Spring的名义下,我们创建了无数行的XML代码。但是现在Spring有了强大的自动化配置和基于java的配置,XML已经不是第一选择了。

   本节内容只是用来帮助你维护已有的XML配置,在完成新的Spring工作时,希望使用自动化配置和JavaConfig

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       xmlns:context="http://www.springframework.org/schema/context"       xsi:schemaLocation="http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">  ...<bean id=”...”  class=”...”><property name=”...”  value=”...”/></bean>...</beans>


上面就是基本的格式,如果想得到相应的bean,在java代码中使用如下代码:

ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");相应的对象 = application.getBean(“bean的ID”);

可以加我微信一起学习

                                                  


原创粉丝点击