spring加载ClassPath使用解析
2011-07-09 15:13
369 查看
一、直接看问题,项目结构如下:
![](http://hi.csdn.net/attachment/201107/9/0_1310194414apc3.gif)
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下的文件覆盖问题:
我们将刚才的项目打包,再新建一个项目:
![](http://hi.csdn.net/attachment/201107/9/0_1310195242uNcE.gif)
在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 秒)
![](http://hi.csdn.net/attachment/201107/9/0_1310194414apc3.gif)
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下的文件覆盖问题:
我们将刚才的项目打包,再新建一个项目:
![](http://hi.csdn.net/attachment/201107/9/0_1310195242uNcE.gif)
在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 秒)
相关文章推荐
- Spring中使用classpath加载配置文件浅析
- Spring中使用classpath加载配置文件浅析
- Spring中使用classpath加载配置文件浅析
- Java天地 学习探讨Java Spring中使用classpath加载配置文件浅析
- spring 使用classpath方式加载hibernate映射文件
- Spring中使用classpath加载配置文件浅析[转]
- 文件映射spring 使用classpath方式加载hibernate映射文件
- Spring中使用classpath加载配置文件浅析
- 使用import简化spring的配置 spring import 标签的解析 使用import或加载spring配置时,报错误There is no ID/IDREF 多个Spring配置文件import resource路径配置
- Spring中使用classpath加载配置文件浅析 1
- Spring中使用classpath加载配置文件浅析
- Spring中使用classpath加载配置文件浅析
- Spring中使用classpath加载配置文件浅析 2
- Spring中使用classpath加载配置文件浅析
- Spring中使用classpath加载配置文件浅析
- spring 使用classpath方式加载hibernate映射文件
- Spring整合Hibernate使用懒加载报错Could not initialize proxy - no Session
- (4)Spring Boot完美使用FastJson解析JSON数据【从零开始学Spring Boot】
- Android Volley完全解析(二),使用Volley加载网络图片
- 使用spring 实现真正多数据源的动态加载及动态切换