方法1:
String resource = "org/mybatis/example/mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory =
new SqlSessionFactoryBuilder().build(inputStream);
如:
方法2:
/**
* 初始方法的执行:
* 1) SomeService service = new SomeService();
* 2) service.setUp();
*
* 单例对象调用销毁方法
* service.tearDown();
*/
@Test
public void test01(){
String configLocation="com/bjpowernode/ba03/applicationContext.xml";
ApplicationContext ctx = new ClassPathXmlApplicationContext(configLocation);
SomeService service = (SomeService) ctx.getBean("someService");
service.doSome();
/**
* 销毁方法的执行:
* 1.关闭容器,关闭容器时会通知容器中的单例对象,调用对象自己的销毁方法
* 2.对象必须是单例的
*/
((ClassPathXmlApplicationContext)ctx).close();
}
方法3: 加载资源文件到项目代码中,然后再读取文件
package com.bjpowernode.test;
import java.io.File;
import java.io.IOException;
import com.bjpowernode.Container;
import com.bjpowernode.service.UserService;
public class MyTest {
public static void main(String[] args) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, IOException {
//创建Service对象
// UserService service = new UserServiceImpl();
// service.addUser();
//使用配置文件表示对象的信息, 使用Contianer完成对象创建,属性赋值
//E:\JAVA课程内容\07Spring\本阶段代码\第一天代码\01-miniSpring\bin
String path = MyTest.class.getResource("/").getPath();
System.out.println("path:"+path);
File file = new File(path,"myobject.txt");
Container cc = new Container(file);
//从Contianer中获取对象
UserService service = (UserService) cc.getBean("service");
service.addUser();
}
}
package com.bjpowernode;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
/**
* 读取myobject.txt文件
* 1.使用反射创建对象
* 2.给对象的属性(依赖对象)赋值
*
*/
public class Container {
//使用集合保存创建好的对象
private Map<String,Object> map = new HashMap<>();
public Container(File file) throws IOException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException{
init(file);
}
//从Map中获取程序中使用对象
public Object getBean(String name){
Object object = null;
if( map.containsKey(name)){
object = map.get(name);
}
return object;
}
/**
* 读取文件myobject
* @param file
* @throws IOException
* @throws SecurityException
* @throws NoSuchFieldException
* @throws IllegalAccessException
* @throws IllegalArgumentException
*/
public void init(File file) throws IOException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException{
FileReader fr = new FileReader(file);
//缓冲流读取文件。
BufferedReader br = new BufferedReader(fr);
String line = null;
while( (line = br.readLine()) != null ){
/*
* service=com.bjpowernode.service.UserServiceImpl
myUserDao=com.bjpowernode.dao.UserDaoMySQLImpl
service#userDao=myUserDao
*/
String [] data = line.split("=");
String left = data[0];//service#userDao
String right = data[1];//myUserDao
//区分是第三行数据
if( left.indexOf("#") > 0 ){
//第三行数据
String ref []= left.split("#");
String refLeft = ref[0];//service
String refRight = ref[1];//userDao 属性名
//从Map中取对象
Object bean = map.get(refLeft);
//反射机制获取Class
Class clazz = bean.getClass();
//获取要修改的属性 Field
Field field = clazz.getDeclaredField(refRight); //userDao属性
//允许属性赋值
field.setAccessible(true);
//给属性赋值
field.set(bean, map.get(right));
} else {
//使用反射创建对象
Object obj = makeObject(right);
//创建好的对象放入Map
map.put(left, obj);
}
}
}
//创建对象
private Object makeObject(String className){
Object object = null;
try {
//调用类的无参数构造方法创建对象
object = Class.forName(className).newInstance();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return object;
}
}