spring加载ClassPath使用解析

一、直接看问题,项目结构如下:

 

WrapperString类如下

package org.jext.string;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 *
 * @author mycj
 */
public class WrapperString {

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("spring_client.xml");
        WrapperString string = (WrapperString)ctx.getBean("WrapperString") ;
        System.out.print(" the name is : "+string.getName());
    }
}

spring_client.xml文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
">
    <bean id ="WrapperString" class="org.jext.string.WrapperString">
        <property name ="name">
            <value>jext</value>
        </property>
    </bean>
</beans>


在项目的classpath路径下新增spring_client.xml,org.jext.string包下的类要加载这个文件,如何加载?上代码

        ApplicationContext ctx = new ClassPathXmlApplicationContext("spring_client.xml");
        WrapperString string = (WrapperString)ctx.getBean("WrapperString") ;
        System.out.print(" the name is : "+string.getName());

这样就是默认加载classpath下的文件,当然这样写也可以,同样能加载classpath下的文件。  加载多个文件 。。。。。。。。。。。待续 

        ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:spring_client.xml");
        WrapperString string = (WrapperString)ctx.getBean("WrapperString") ;
        System.out.print(" the name is : "+string.getName());

然后我们把spring_client.xml放到org.jext.string下,那么代码就成了这样,以 /  隔开

        ApplicationContext ctx = new ClassPathXmlApplicationContext("org/jext/string/spring_client.xml");
        WrapperString string = (WrapperString)ctx.getBean("WrapperString") ;
        System.out.print(" the name is : "+string.getName());

二、关于java项目打包后的classpath下的文件覆盖问题:

我们将刚才的项目打包,再新建一个项目:


在classpath下将刚才打的包引入,同时编辑刚才新建项目的spring_client.xml文件,内容相同,不过要将属性值修改下,以检验是否将打包的classpath下的文件覆盖

spring-client.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
">
    <bean id ="WrapperString" class="org.jext.string.WrapperString">
        <property name ="name">
            <value>jext_client</value>
        </property>
    </bean>
</beans>


jext_client.java

    public static void main(String[] args) {
        // TODO code application logic here
        ApplicationContext ctx = new ClassPathXmlApplicationContext("spring_client.xml");
        WrapperString string = (WrapperString)ctx.getBean("WrapperString") ;
        System.out.print(" the name is : "+string.getName());
    }


运行后,发现显示的仍然是   the name is : jext。说明客户端项目编辑引入jar包相同的配置文件,不会覆盖jar包中文件的p


run:
2011-7-9 15:09:03 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@c1b531: display name [org.springframework.context.support.ClassPathXmlApplicationContext@c1b531]; startup date [Sat Jul 09 15:09:03 CST 2011]; root of context hierarchy
2011-7-9 15:09:03 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [spring_client.xml]
2011-7-9 15:09:03 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
信息: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@c1b531]: org.springframework.beans.factory.support.DefaultListableBeanFactory@161f10f
2011-7-9 15:09:03 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@161f10f: defining beans [WrapperString]; root of factory hierarchy
 the name is : jext成功生成(总时间:0 秒)









  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值