Browse Source

EntityManagerFactoryUtils finds default EntityManagerFactory in parent contexts as well

Also introduces consistent use of getBean(Class) for similar use cases across the framework, accepting a locally unique target bean even if further matching beans would be available in parent contexts (in contrast to BeanFactoryUtils.beanOfType's behavior).

Issue: SPR-10160
pull/218/head
Juergen Hoeller 12 years ago committed by unknown
parent
commit
1a929f22e0
  1. 9
      spring-beans/src/main/java/org/springframework/beans/factory/config/ServiceLocatorFactoryBean.java
  2. 9
      spring-orm/src/main/java/org/springframework/orm/jpa/EntityManagerFactoryUtils.java
  3. 10
      spring-tx/src/main/java/org/springframework/transaction/interceptor/TransactionAspectSupport.java
  4. 6
      spring-tx/src/main/java/org/springframework/transaction/interceptor/TransactionProxyFactoryBean.java

9
spring-beans/src/main/java/org/springframework/beans/factory/config/ServiceLocatorFactoryBean.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2012 the original author or authors. * Copyright 2002-2013 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -27,7 +27,6 @@ import org.springframework.beans.BeansException;
import org.springframework.beans.FatalBeanException; import org.springframework.beans.FatalBeanException;
import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware; import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanFactoryUtils;
import org.springframework.beans.factory.FactoryBean; import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.ListableBeanFactory; import org.springframework.beans.factory.ListableBeanFactory;
@ -364,12 +363,12 @@ public class ServiceLocatorFactoryBean implements FactoryBean<Object>, BeanFacto
try { try {
String beanName = tryGetBeanName(args); String beanName = tryGetBeanName(args);
if (StringUtils.hasLength(beanName)) { if (StringUtils.hasLength(beanName)) {
// Service locator for a specific bean name. // Service locator for a specific bean name
return beanFactory.getBean(beanName, serviceLocatorMethodReturnType); return beanFactory.getBean(beanName, serviceLocatorMethodReturnType);
} }
else { else {
// Service locator for a bean type. // Service locator for a bean type
return BeanFactoryUtils.beanOfTypeIncludingAncestors(beanFactory, serviceLocatorMethodReturnType); return beanFactory.getBean(serviceLocatorMethodReturnType);
} }
} }
catch (BeansException ex) { catch (BeansException ex) {

9
spring-orm/src/main/java/org/springframework/orm/jpa/EntityManagerFactoryUtils.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2012 the original author or authors. * Copyright 2002-2013 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -76,10 +76,12 @@ public abstract class EntityManagerFactoryUtils {
* Find an EntityManagerFactory with the given name in the given * Find an EntityManagerFactory with the given name in the given
* Spring application context (represented as ListableBeanFactory). * Spring application context (represented as ListableBeanFactory).
* <p>The specified unit name will be matched against the configured * <p>The specified unit name will be matched against the configured
* peristence unit, provided that a discovered EntityManagerFactory * persistence unit, provided that a discovered EntityManagerFactory
* implements the {@link EntityManagerFactoryInfo} interface. If not, * implements the {@link EntityManagerFactoryInfo} interface. If not,
* the persistence unit name will be matched against the Spring bean name, * the persistence unit name will be matched against the Spring bean name,
* assuming that the EntityManagerFactory bean names follow that convention. * assuming that the EntityManagerFactory bean names follow that convention.
* <p>If no unit name has been given, this method will search for a default
* EntityManagerFactory through {@link ListableBeanFactory#getBean(Class)}.
* @param beanFactory the ListableBeanFactory to search * @param beanFactory the ListableBeanFactory to search
* @param unitName the name of the persistence unit (may be {@code null} or empty, * @param unitName the name of the persistence unit (may be {@code null} or empty,
* in which case a single bean of type EntityManagerFactory will be searched for) * in which case a single bean of type EntityManagerFactory will be searched for)
@ -108,7 +110,8 @@ public abstract class EntityManagerFactoryUtils {
return beanFactory.getBean(unitName, EntityManagerFactory.class); return beanFactory.getBean(unitName, EntityManagerFactory.class);
} }
else { else {
return BeanFactoryUtils.beanOfType(beanFactory, EntityManagerFactory.class); // Find unique EntityManagerFactory bean in the context, falling back to parent contexts.
return beanFactory.getBean(EntityManagerFactory.class);
} }
} }

10
spring-tx/src/main/java/org/springframework/transaction/interceptor/TransactionAspectSupport.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2012 the original author or authors. * Copyright 2002-2013 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -24,9 +24,7 @@ import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware; import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanFactoryUtils;
import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.annotation.BeanFactoryAnnotationUtils; import org.springframework.beans.factory.annotation.BeanFactoryAnnotationUtils;
import org.springframework.core.NamedThreadLocal; import org.springframework.core.NamedThreadLocal;
import org.springframework.transaction.NoTransactionException; import org.springframework.transaction.NoTransactionException;
@ -247,12 +245,8 @@ public abstract class TransactionAspectSupport implements BeanFactoryAware, Init
else if (this.transactionManagerBeanName != null) { else if (this.transactionManagerBeanName != null) {
return this.beanFactory.getBean(this.transactionManagerBeanName, PlatformTransactionManager.class); return this.beanFactory.getBean(this.transactionManagerBeanName, PlatformTransactionManager.class);
} }
else if (this.beanFactory instanceof ListableBeanFactory) {
return BeanFactoryUtils.beanOfTypeIncludingAncestors(((ListableBeanFactory) this.beanFactory), PlatformTransactionManager.class);
}
else { else {
throw new IllegalStateException( return this.beanFactory.getBean(PlatformTransactionManager.class);
"Cannot retrieve PlatformTransactionManager beans from non-listable BeanFactory: " + this.beanFactory);
} }
} }

6
spring-tx/src/main/java/org/springframework/transaction/interceptor/TransactionProxyFactoryBean.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2012 the original author or authors. * Copyright 2002-2013 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -176,8 +176,8 @@ public class TransactionProxyFactoryBean extends AbstractSingletonProxyFactoryBe
/** /**
* This callback is optional: If running in a BeanFactory and no transaction * This callback is optional: If running in a BeanFactory and no transaction
* manager has been set explicitly, a single matching bean of type * manager has been set explicitly, a single matching bean of type
* PlatformTransactionManager will be fetched from the BeanFactory. * {@link PlatformTransactionManager} will be fetched from the BeanFactory.
* @see org.springframework.beans.factory.BeanFactoryUtils#beanOfTypeIncludingAncestors * @see org.springframework.beans.factory.BeanFactory#getBean(Class)
* @see org.springframework.transaction.PlatformTransactionManager * @see org.springframework.transaction.PlatformTransactionManager
*/ */
public void setBeanFactory(BeanFactory beanFactory) { public void setBeanFactory(BeanFactory beanFactory) {

Loading…
Cancel
Save