1 16 17 package org.springframework.aop.target; 18 19 import java.io.NotSerializableException ; 20 import java.io.ObjectStreamException ; 21 import java.io.Serializable ; 22 23 import org.apache.commons.logging.Log; 24 import org.apache.commons.logging.LogFactory; 25 26 import org.springframework.aop.TargetSource; 27 import org.springframework.beans.BeansException; 28 import org.springframework.beans.factory.BeanFactory; 29 import org.springframework.beans.factory.BeanFactoryAware; 30 import org.springframework.util.ClassUtils; 31 import org.springframework.util.ObjectUtils; 32 33 54 public abstract class AbstractBeanFactoryBasedTargetSource 55 implements TargetSource, BeanFactoryAware, Serializable { 56 57 58 private static final long serialVersionUID = -4721607536018568393L; 59 60 61 62 protected final Log logger = LogFactory.getLog(getClass()); 63 64 65 private String targetBeanName; 66 67 68 private Class targetClass; 69 70 74 private BeanFactory beanFactory; 75 76 77 86 public void setTargetBeanName(String targetBeanName) { 87 this.targetBeanName = targetBeanName; 88 } 89 90 93 public String getTargetBeanName() { 94 return this.targetBeanName; 95 } 96 97 103 public void setTargetClass(Class targetClass) { 104 this.targetClass = targetClass; 105 } 106 107 111 public void setBeanFactory(BeanFactory beanFactory) throws BeansException { 112 if (this.targetBeanName == null) { 113 throw new IllegalStateException ("Property'targetBeanName' is required"); 114 } 115 this.beanFactory = beanFactory; 116 } 117 118 121 public BeanFactory getBeanFactory() { 122 return this.beanFactory; 123 } 124 125 126 public synchronized Class getTargetClass() { 127 if (this.targetClass == null && this.beanFactory != null) { 128 this.targetClass = this.beanFactory.getType(this.targetBeanName); 130 if (this.targetClass == null) { 131 if (logger.isTraceEnabled()) { 132 logger.trace("Getting bean with name '" + this.targetBeanName + "' in order to determine type"); 133 } 134 this.targetClass = this.beanFactory.getBean(this.targetBeanName).getClass(); 135 } 136 } 137 return this.targetClass; 138 } 139 140 public boolean isStatic() { 141 return false; 142 } 143 144 public void releaseTarget(Object target) throws Exception { 145 } 147 148 149 154 protected void copyFrom(AbstractBeanFactoryBasedTargetSource other) { 155 this.targetBeanName = other.targetBeanName; 156 this.targetClass = other.targetClass; 157 this.beanFactory = other.beanFactory; 158 } 159 160 168 protected Object writeReplace() throws ObjectStreamException { 169 if (logger.isDebugEnabled()) { 170 logger.debug("Disconnecting TargetSource [" + this + "]"); 171 } 172 try { 173 return new SingletonTargetSource(getTarget()); 175 } 176 catch (Exception ex) { 177 logger.error("Cannot get target for disconnecting TargetSource [" + this + "]", ex); 178 throw new NotSerializableException ( 179 "Cannot get target for disconnecting TargetSource [" + this + "]: " + ex); 180 } 181 } 182 183 184 public boolean equals(Object other) { 185 if (this == other) { 186 return true; 187 } 188 if (other == null || !getClass().equals(other.getClass())) { 189 return false; 190 } 191 AbstractBeanFactoryBasedTargetSource otherTargetSource = (AbstractBeanFactoryBasedTargetSource) other; 192 return (ObjectUtils.nullSafeEquals(this.beanFactory, otherTargetSource.beanFactory) && 193 ObjectUtils.nullSafeEquals(this.targetBeanName, otherTargetSource.targetBeanName)); 194 } 195 196 public int hashCode() { 197 int hashCode = getClass().hashCode(); 198 hashCode = 13 * hashCode + ObjectUtils.nullSafeHashCode(this.beanFactory); 199 hashCode = 13 * hashCode + ObjectUtils.nullSafeHashCode(this.targetBeanName); 200 return hashCode; 201 } 202 203 public String toString() { 204 StringBuffer sb = new StringBuffer (); 205 sb.append(ClassUtils.getShortName(getClass())); 206 sb.append(" for target bean '").append(this.targetBeanName).append("'"); 207 if (this.targetClass != null) { 208 sb.append(" of type [").append(this.targetClass.getName()).append("]"); 209 } 210 return sb.toString(); 211 } 212 213 } 214 | Popular Tags |