1 22 23 package org.jboss.spring.kernel; 24 25 import org.springframework.aop.TargetSource; 26 import org.springframework.aop.framework.ProxyFactory; 27 28 36 public class LazyLocator extends MicrocontainerLocatorSupport implements Locator 37 { 38 39 public Object locateBean(String beanName, Class targetType) 40 { 41 TargetSource targetSource = new LocatorTargetSource(beanName, targetType); 42 ProxyFactory proxyFactory = new ProxyFactory(); 43 proxyFactory.addInterface(targetType); 44 proxyFactory.setTargetSource(targetSource); 45 return proxyFactory.getProxy(); 46 } 47 48 private class LocatorTargetSource implements TargetSource 49 { 50 51 private String beanName; 52 private Class targetClass; 53 54 private Object cachedObject; 55 56 public LocatorTargetSource(String beanName, Class targetClass) 57 { 58 this.beanName = beanName; 59 this.targetClass = targetClass; 60 } 61 62 public Class getTargetClass() 63 { 64 return (this.cachedObject != null ? this.cachedObject.getClass() : this.targetClass); 65 } 66 67 public boolean isStatic() 68 { 69 return (this.cachedObject != null); 70 } 71 72 public Object getTarget() throws Exception 73 { 74 synchronized (this) 75 { 76 if (this.cachedObject == null) 77 { 78 this.cachedObject = locateBean(beanName, targetClass); 79 } 80 return this.cachedObject; 81 } 82 } 83 84 public void releaseTarget(Object target) 85 { 86 } 87 88 } 89 90 } 91 | Popular Tags |