本帖最后由 张玉建 于 2013-9-17 23:17 编辑
在BeanFactory内出现NullPointerException
求解释
package com.itheima.beanfactory;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import com.itheima.Advice;
public class BeanFactory {
//创建配置文件,io流的字节流,properties
Properties pro=new Properties();//util包中的io流用于配置文件。
//构造函数中
public BeanFactory(InputStream ips)
{
try {
pro.load(ips);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public Object getBean(String name) throws InstantiationException, IllegalAccessException, ClassNotFoundException
{
String className = pro.getProperty(name);//拿到对应的类名名字。
Object bean=null;
try {
Class clazz=Class.forName(className);
//javaBean必须有一个不带参数的构造方法,newInstance()创建实例对象。
bean= clazz.newInstance();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//测试是那个类的实例吗/
if (bean instanceof ProxyFactoryBean )
{
Object proxy=null;
//创建代理对象,
ProxyFactoryBean proxyFactoryBean=(ProxyFactoryBean)bean;
try {
Advice advice= (Advice)Class.forName(pro.getProperty(name+".advice")).newInstance();
Object trgert=Class.forName(pro.getProperty(name+".trgert")).newInstance();
proxyFactoryBean.setAdvice(advice);//
proxyFactoryBean.setTrgert(trgert);//目标
proxy= proxyFactoryBean.getProxy();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//Object proxy =((ProxyFactoryBean)bean).getProxy();//beanfactory创建实例对象。
return proxy;
}
return bean;
}
}
at com.itheima.beanfactory.BeanFactory.getBean(BeanFactory.java:43)
at com.itheima.beanfactory.AopFremWorkTest.main(AopFremWorkTest.java:14)
Exception in thread "main" java.lang.NullPointerException
at com.itheima.beanfactory.AopFremWorkTest.main(AopFremWorkTest.java:15)
Advice advice= (Advice)Class.forName(pro.getProperty(name+".advice")).newInstance();
也就是这里!求解释!
|