📄 helloworldclient.java
字号:
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.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
public class HelloWorldClient {
protected static final Log log=LogFactory.getLog(FileHelloStr.class);
public HelloWorldClient()
{
System.out.println("1");
Resource resource=new ClassPathResource("appcontext.xml");
System.out.println("2");
BeanFactory factory=new XmlBeanFactory(resource);
System.out.println("3");
HelloWorld hw=(HelloWorld)factory.getBean("fileHelloWorld");
log.info(hw.getContent());
//以上三句为配置Spring appcontext.xml文件,开发者需要告知XmlBeanFactory构建器,即appcontext.xml文件。当然,上述客户应用
//使用了Spring框架中org.springframework.core.io提供的实用类,另外还存在两种方式。
/*
其一:基于文件找到appcontext.xml
try{
InputStream ins=new FileInputStream("src/appcontext.xml");
BeanFactory factory=new XmlBeanFactory(ints);
HelloWorld hw=(HelloWorld)factory.getBean("fileHelloWorld");
log.info(hw.getContent);
}
catch(FileNotFoundException ex)
{
log.error("文件appcontext.xml未找到",ex);
}
其二:基于ApplicationContext实现类找到appcontext.xml文件
ClassPathXmlApplicationContext appContext=new ClassPathXmlApplicationContext(new String[]{"appcontext.xml"});
BeanFactory factory=(BeanFactory)appContext;
HelloWorld hw=(HelloWorld)factory.getBean("fileHelloWorld");
log.info(hw.getContent());
*/
System.out.println("4");
System.out.println(hw.getContent());
}
public static void main(String[] args)
{
/*
FileHelloStr fhStr=new FileHelloStr("HelloWorld.properties");
HelloWorld hw=new HelloWorld(fhStr);//开发者必须在其实现的客户代码中创建FileHelloStr对象,
//并连接到HelloWorld中,因此,开发者需要借助于工厂类,以注入HelloWorld和FileHelloStr的依赖性
*/
//HelloWorld hw=HelloWorldFactory.getFileHelloWorld();
//log.info(hw.getContent());
new HelloWorldClient();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -