[Spring] ClassPathXmlApplicationContext类

1. 该类在package org.springframework.context.support包下. 该包在4.0.1中封装在spring-context-***.jar中.

其无参构造函数的文档注释:

* Create a new ClassPathXmlApplicationContext, loading the definitions
* from the given XML file and automatically refreshing the context.

就是为了bean风格的配置, 创建一个新的 ClassPathXmlApplicationContext 实例.

其参数是String 类型的配置路径(configLocations),对应的重载方法支持多个String类型的配置路径参数.

配置路径可以通过 AbstractRefreshableConfigApplicationContext.getConfigLocations()获取.

配置路径可以使具体的文件如: "/myfiles/context.xml"

或者 Ant-style 样式,如: "/myfiles/*-context.xml"

  (see the AntPathMatcher javadoc for pattern details).

为了避免路径重复,后面的bean定义会覆盖前面的同名bean定义.

-----------------构造函数---------------------

ClassPathXmlApplicationContext(java.lang.String configLocation)

Create a new ClassPathXmlApplicationContext, loading the definitions from the given XML file and automatically refreshing the context.
------------------方法--------------------------
Modifier and TypeMethod and Description
protected Resource[] getConfigResources()
Return an array of Resource objects, referring to the XML bean definition files that this context should be built with.

 继承自抽象类AbstractApplicationContext的方法.getBean() (重载多次)

=======================例子==============================


即使测试类没有调用userDao和UserServiceImpl.

但还是调用了set方法.

因为再创建cxt时,已经将ApplicationContext.xml中的bean注入了.

bean中的property中的name属性,对应的是bean中该类中的set方法.

如:

    <bean id="p1" class="entity.Person">
        <property name="nameX" value="zz"></property>
        <property name="content" value="I Like You"></property>
    </bean>

control+左键 点卡nameX,会跳转到setNameX上:

和其形参name和类的属性名name无关.

只需要保持这两者一致,即可.

  

宛如智障,暗藏锋芒
原文地址:https://www.cnblogs.com/zienzir/p/9111224.html