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);
}
}