package com.openv.spring;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
//import org.springframework.beans.factory.BeanFactory;
//import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
//import org.springframework.core.io.ClassPathResource;
//import org.springframework.core.io.Resource;
/**
* HelloWorld客户应用
*
*
* 即: 对于ClassPathXmlApplicationContext(), classpath: 前缀是不需要的, 默认就是指项目的classpath路径下面;
* 如果要使用绝对路径,需要加上 file: 前缀表示这是绝对路径;
*
* 对于FileSystemXmlApplicationContext(), 默认表示的是两种:
* 1,没有盘符的是 项目工作路径, 即项目的根目录;
* 2,有盘符表示的是 文件绝对路径。
*
* 如果要使用classpath路径, 需要前缀 classpath: .
*
* @author luoshifei
*/
public class HelloClient {
protected static final Log log = LogFactory.getLog(HelloClient.class);
public static void main(String[] args) {
// Resource resource = new ClassPathResource("appcontext.xml");
// BeanFactory factory = new XmlBeanFactory(resource);
//用classpath路径也可以
// ApplicationContext factory=new ClassPathXmlApplicationContext("classpath:appcontext.xml");
// ApplicationContext factory=new ClassPathXmlApplicationContext("appcontext.xml");
// ApplicationContext factory=new ClassPathXmlApplicationContext("file:G:/1Java实用项目资源/2Spring/1精通Spring全Jar代码/workspace/workspace/example6/src/appcontext.xml");
//用文件系统的路径
// ApplicationContext factory=new FileSystemXmlApplicationContext("src/appcontext.xml");
//使用了 classpath: 前缀,作为标志, 这样,FileSystemXmlApplicationContext 也能够读入classpath下的相对路径
// ApplicationContext factory=new FileSystemXmlApplicationContext("classpath:appcontext.xml");
// ApplicationContext factory=new FileSystemXmlApplicationContext("file:G:/1Java实用项目资源/2Spring/1精通Spring全Jar代码/workspace/workspace/example6/src/appcontext.xml");
ApplicationContext factory=new FileSystemXmlApplicationContext("G:/1Java实用项目资源/2Spring/1精通Spring全Jar代码/workspace/workspace/example6/src/appcontext.xml");
IHelloWorld hw = (IHelloWorld) factory.getBean("helloworldbean");
log.info(hw.getContent("luoshifei"));
}
}
=================================================
例子:
src/beans.xml
===============
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="hello" class="com.spring.HelloAction">
<property name="username" value="张三" />
</bean>
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename">
<value>hello</value>
</property>
</bean>
</beans>
=========================================================
hello_zh.properties
==================
greeting=huan yin ni.
time = jin tian shi {0}.
hello_en.properties
===============
greeting=welcome to you !
time = Today is {0}
==========================================
HelloAction.java
======================
package com.spring;
public class HelloAction {
private String username;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String sayHello(String greeting){
return username + ", " + greeting;
}
}
========================================
HelloActionTest.java
====================
package com.spring.test;
import java.util.Date;
import java.util.Locale;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.spring.HelloAction;
import junit.framework.TestCase;
public class HelloActionTest extends TestCase {
public void test(){
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
//ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:beans.xml");
//ApplicationContext ctx = new FileSystemXmlApplicationContext("classpath:beans.xml");
//ApplicationContext ctx = new FileSystemXmlApplicationContext("src/beans.xml");
String greeting = ctx.getMessage("greeting", null, Locale.CHINESE);
String time = ctx.getMessage("time", new Object[]{new Date()}, Locale.CHINESE);
HelloAction helloAction = (HelloAction) ctx.getBean("hello");
System.out.println(helloAction.sayHello(greeting));
System.out.println(time);
}
}
分享到:
相关推荐
spring读取配置不同目录下的配置文件,使用ClassPathXmlApplicationContext与FileSystemXmlApplicationContext
二、ClassPathXmlApplicationContext[只能读放在web-info/classes目录下的配置文件]和FileSystemXmlApplicationContext的区别 classpath:前缀是不需要的,默认就是指项目的classpath路径下面; 如果要使用绝对路径,...
spring容器的触发事件 ClassPathXmlApplicationContext的start()方法的用法
NULL 博文链接:https://gaojiewyh.iteye.com/blog/409606
NULL 博文链接:https://cuiran.iteye.com/blog/487385
* ClassPathXmlApplicationContext:它可以加载类路径下的配置文件,要求配置文件必须在类路径下。不在的话,加载不了。(更常用) * FileSystemXmlApplicationContext:它可以加载磁盘任意路径下的配置文件(必须有...
NULL 博文链接:https://canfly2010.iteye.com/blog/443063
加载xml文件使用ClassPathXmlApplicationContext("applicationContext.xml") (3)BeanFactory(bean工厂)与ApplicationContext(容器)对比: 所有在xml里配置的Bean标签最后都会转换为BeanDefinition对象存放在...
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); (2)ApplicationContext context = new FileSystemXmlApplicationContext ("applicationContext.xml"); 一般用第...
FileSystemXmlApplicationContext磁盘路径 AnnotationConfigApplicationContext注解 WebApplicationContext:web环境使用的容器 注解 创建对象 Component:不分层的注解 Controller:web层 Service:...
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@c1f10e: display name [org.springframework.context.support.ClassPathXmlApplicationContext@c1f10e]; startup date [Wed ...
4.7. Application context 和Resource 路径 4.7.1. 构造application context 4.7.1.1. 创建 ClassPathXmlApplicationContext 实例 - 简介 4.7.2. Application context构造器中资源路径的通配符 4.7.2.1. Ant风格的...
手写SpringIoc的XML实现方式,仿照Spring底层Bean工厂类实现ClassPathXmlApplicationContext
ApplicationContext ac = new ClassPathXmlApplicationContext("spring/applicationContext-jms-producer.xml"); //获取生产者发送消息服务接口 MessageSender messageSender = (MessageSender)ac.getBean...
crm项目的架构 * 创建web工程 * 引入jar包 * mysql的驱动包 * hibernate需要的jar包 ... * 建立请求路径和action之间的关联 * 创建struts.xml文件,放置在src下 * 文件内容如下: <!DOCTYPE struts...
Jan 17, 2015 1:36:28 AM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@41906a77:...
{ ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); JdbcTemplate jdTemplate =(JdbcTemplate) applicationContext.getBean("jdbcTemplate"); jdTemplate....
在Java项目中,会通过ClassPathXmlApplicationContext类来实例化ApplicationContext容器。而在Web项目中,ApplicationContext容器的实例化工作会交由Web服务器来完成。 Web服务器实例化ApplicationContext容器时,...
package demo; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;