您的位置:首页 > 编程语言 > Java开发

spring加载ClassPath使用解析

2011-07-09 15:13 369 查看
一、直接看问题,项目结构如下:
 


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 秒)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息