public interface iuserservice {
public void dosome();
}
public class userserviceimpl implements iuserservice {
public userserviceimpl(){
system.out.println("--被实例化了--");
}
@override
public void dosome() {
system.out.println("userserviceimpl 。。。 被执行了");
}
}
自定义factorybean
public class myfactorybean implements factorybean<iuserservice>{
@override
public iuserservice getobject() throws exception {
system.out.println("--iuserservice实例化之前---");
iuserservice service = new userserviceimpl();
system.out.println("--iuserservice实例化后---");
return service;
}
@override
public class<?> getobjecttype() {
return iuserservice.class;
}
@override
public boolean issingleton() {
return true;
}
}
public interface iuserservice {
public void dosome();
}
public class userserviceimpl implements iuserservice {
public userserviceimpl(){
system.out.println("--被实例化了--");
}
@override
public void dosome() {
system.out.println("userserviceimpl 。。。 被执行了");
}
}
自定义factorybean
public class myfactorybean implements factorybean,initializingbean,disposablebean{
private object proxyobject;
private object target;
private string interfacename;
@override
public object getobject() throws exception {
return proxyobject;
}
@override
public class<?> getobjecttype() {
return proxyobject.getclass()==null?object.class:proxyobject.getclass();
}
@override
public boolean issingleton() {
return true;
}
/**
* myfactorybean 对象销毁的回调方法
* @throws exception
*/
@override
public void destroy() throws exception {
system.out.println("destroy ....");
}
/**
* myfactorybean 对象实例化的方法
*/
@override
public void afterpropertiesset() throws exception {
system.out.println("---afterpropertiesset---");
proxyobject = proxy.newproxyinstance(
this.getclass().getclassloader()
, new class[]{class.forname(interfacename)}
, new invocationhandler() {
@override
public object invoke(object proxy, method method, object[] args) throws throwable {
system.out.println("----代理方法执行开始----");
object obj = method.invoke(target, args);
system.out.println("----代理方法执行结束----");
return obj;
}
});
}
public string getinterfacename() {
return interfacename;
}
public void setinterfacename(string interfacename) {
this.interfacename = interfacename;
}
public object gettarget() {
return target;
}
public void settarget(object target) {
this.target = target;
}
}