新建一个interface文件:IHello 并放在first文件中
- package first;
-
- public interface IHello {
- public String sayHello(String user);
- }
再在同一个文件夹中建一个class文件Hello
- package first;
-
- public class Hello implements IHello {
-
- @Override
- public String sayHello(String user) {
- return "Hello: " + user;
- }
-
- }
建一个测试文件java
- import org.springframework.beans.factory.xml.XmlBeanFactory;
- import org.springframework.core.io.ClassPathResource;
-
- import first.IHello;
-
- public class test {
- public static void main(String arg[]) {
- // 指定配置文件路径
- ClassPathResource resource = new ClassPathResource(
- "applicationContext.xml");
- XmlBeanFactory factory = new XmlBeanFactory(resource);
- //得到配置文件中的Bean
- IHello hello = (IHello)factory.getBean("hello");
- String result = hello.sayHello("kavian");
- System.out.println(result);
- }
- }
原整版本:
http://www.kavian.com.cn/?p=25