DATAJPA-956 - Fixed accidental pick up of non-EntityManagerFactor JNDI objects.
We now exclude all candidates beans that could theoretically become an EntityManagerFactory unless we definitely know so. Previously bean definitions for JndiObjectFactoryBean that didn't expose an expected type (e.g. ones defined via JavaConfig) were propagated to the creation of a DefaultJpaContext.
This commit is contained in:
@@ -127,6 +127,8 @@ public class BeanDefinitionUtils {
|
|||||||
if (!EntityManagerFactory.class.getName().equals(definition.getPropertyValues().get("expectedType"))) {
|
if (!EntityManagerFactory.class.getName().equals(definition.getPropertyValues().get("expectedType"))) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
} else if (!EntityManagerFactory.class.equals(beanFactory.getType(name))) {
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
definitions.add(new EntityManagerFactoryBeanDefinition(name, beanFactory));
|
definitions.add(new EntityManagerFactoryBeanDefinition(name, beanFactory));
|
||||||
|
|||||||
@@ -17,12 +17,15 @@ package org.springframework.data.jpa.repository.support;
|
|||||||
|
|
||||||
import static org.hamcrest.CoreMatchers.*;
|
import static org.hamcrest.CoreMatchers.*;
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
|
import static org.mockito.Mockito.*;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
|
|
||||||
|
import javax.naming.NamingException;
|
||||||
import javax.persistence.EntityManager;
|
import javax.persistence.EntityManager;
|
||||||
import javax.persistence.EntityManagerFactory;
|
import javax.persistence.EntityManagerFactory;
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.BeforeClass;
|
import org.junit.BeforeClass;
|
||||||
@@ -44,6 +47,8 @@ import org.springframework.data.jpa.repository.JpaContext;
|
|||||||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
||||||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
|
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
|
||||||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
|
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
|
||||||
|
import org.springframework.jndi.JndiObjectFactoryBean;
|
||||||
|
import org.springframework.mock.jndi.ExpectedLookupTemplate;
|
||||||
import org.springframework.mock.jndi.SimpleNamingContextBuilder;
|
import org.springframework.mock.jndi.SimpleNamingContextBuilder;
|
||||||
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
|
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
@@ -113,6 +118,7 @@ public class DefaultJpaContextIntegrationTests {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @see DATAJPA-813
|
* @see DATAJPA-813
|
||||||
|
* @see DATAJPA-956
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void bootstrapsDefaultJpaContextInSpringContainer() {
|
public void bootstrapsDefaultJpaContextInSpringContainer() {
|
||||||
@@ -163,7 +169,7 @@ public class DefaultJpaContextIntegrationTests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@EnableJpaRepositories
|
@EnableJpaRepositories
|
||||||
@ComponentScan(includeFilters = @Filter(type = FilterType.ASSIGNABLE_TYPE, value = ApplicationComponent.class) ,
|
@ComponentScan(includeFilters = @Filter(type = FilterType.ASSIGNABLE_TYPE, value = ApplicationComponent.class),
|
||||||
useDefaultFilters = false)
|
useDefaultFilters = false)
|
||||||
static class Config {
|
static class Config {
|
||||||
|
|
||||||
@@ -171,6 +177,21 @@ public class DefaultJpaContextIntegrationTests {
|
|||||||
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
|
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
|
||||||
return createEntityManagerFactoryBean("spring-data-jpa");
|
return createEntityManagerFactoryBean("spring-data-jpa");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// A non-EntityManagerFactory JNDI object to make sure the detection doesn't include it
|
||||||
|
// @see DATAJPA-956
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public JndiObjectFactoryBean jndiObject() throws NamingException {
|
||||||
|
|
||||||
|
JndiObjectFactoryBean bean = new JndiObjectFactoryBean();
|
||||||
|
|
||||||
|
bean.setJndiName("some/DataSource");
|
||||||
|
bean.setJndiTemplate(new ExpectedLookupTemplate("some/DataSource", mock(DataSource.class)));
|
||||||
|
bean.setExpectedType(DataSource.class);
|
||||||
|
|
||||||
|
return bean;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
|
|||||||
Reference in New Issue
Block a user