[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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值