org.springframework.beans.factory.xml.xmlbeandefinitionreader Java Examples

The following examples show how to use org.springframework.beans.factory.xml.xmlbeandefinitionreader. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TunedDocumentLoader.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Override
public Document loadDocument(InputSource inputSource, EntityResolver entityResolver,
                             ErrorHandler errorHandler, int validationMode, boolean namespaceAware)
    throws Exception {
    if (validationMode == XmlBeanDefinitionReader.VALIDATION_NONE) {
        SAXParserFactory parserFactory =
            namespaceAware ? nsasaxParserFactory : saxParserFactory;
        SAXParser parser = parserFactory.newSAXParser();
        XMLReader reader = parser.getXMLReader();
        reader.setEntityResolver(entityResolver);
        reader.setErrorHandler(errorHandler);
        SAXSource saxSource = new SAXSource(reader, inputSource);
        W3CDOMStreamWriter writer = new W3CDOMStreamWriter();
        StaxUtils.copy(saxSource, writer);
        return writer.getDocument();
    }
    return super.loadDocument(inputSource, entityResolver, errorHandler, validationMode,
                              namespaceAware);
}
 
Example #2
Source File: ProxyFactoryBeanTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testGetObjectTypeWithDirectTarget() {
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	new XmlBeanDefinitionReader(bf).loadBeanDefinitions(new ClassPathResource(TARGETSOURCE_CONTEXT, CLASS));

	// We have a counting before advice here
	CountingBeforeAdvice cba = (CountingBeforeAdvice) bf.getBean("countingBeforeAdvice");
	assertEquals(0, cba.getCalls());

	ITestBean tb = (ITestBean) bf.getBean("directTarget");
	assertTrue(tb.getName().equals("Adam"));
	assertEquals(1, cba.getCalls());

	ProxyFactoryBean pfb = (ProxyFactoryBean) bf.getBean("&directTarget");
	assertTrue("Has correct object type", TestBean.class.isAssignableFrom(pfb.getObjectType()));
}
 
Example #3
Source File: SpringContextService.java    From lutece-core with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Loads plugins contexts.
 * 
 * @param filesContext       context files names
 * @param strConfPluginsPath full path
 * @param xmlReader          the xml reader
 */
private static void loadContexts( String[] filesContext, String strConfPluginsPath,
        XmlBeanDefinitionReader xmlReader )
{
    if ( filesContext != null )
    {
        for ( String fileContext : filesContext )
        {
            String[] file =
            { PROTOCOL_FILE + strConfPluginsPath + fileContext };

            // Safe loading of plugin context file
            try
            {
                xmlReader.loadBeanDefinitions( file );
                AppLogService.info( "Context file loaded : " + fileContext );
            }
            catch ( Exception e )
            {
                AppLogService.error(
                        "Unable to load Spring context file : " + fileContext + " - cause : " + e.getMessage( ), e );
            }
        }
    }
}
 
Example #4
Source File: MergedBeanDefinitionDemo.java    From geekbang-lessons with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
    DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
    // 基于 XML 资源 BeanDefinitionReader 实现
    XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);
    String location = "META-INF/dependency-lookup-context.xml";
    // 基于 ClassPath 加载 XML 资源
    Resource resource = new ClassPathResource(location);
    // 指定字符编码 UTF-8
    EncodedResource encodedResource = new EncodedResource(resource, "UTF-8");
    int beanNumbers = beanDefinitionReader.loadBeanDefinitions(encodedResource);
    System.out.println("已加载 BeanDefinition 数量:" + beanNumbers);
    // 通过 Bean Id 和类型进行依赖查找
    User user = beanFactory.getBean("user", User.class);
    System.out.println(user);

    User superUser = beanFactory.getBean("superUser", User.class);
    System.out.println(superUser);
}
 
Example #5
Source File: QueryLogicFactoryBeanTest.java    From datawave with Apache License 2.0 6 votes vote down vote up
@Before
public void setup() throws IllegalArgumentException, IllegalAccessException {
    System.setProperty(NpeUtils.NPE_OU_PROPERTY, "iamnotaperson");
    System.setProperty("dw.metadatahelper.all.auths", "A,B,C,D");
    Logger.getLogger(ClassPathXmlApplicationContext.class).setLevel(Level.OFF);
    Logger.getLogger(XmlBeanDefinitionReader.class).setLevel(Level.OFF);
    Logger.getLogger(DefaultListableBeanFactory.class).setLevel(Level.OFF);
    ClassPathXmlApplicationContext queryFactory = new ClassPathXmlApplicationContext();
    queryFactory.setConfigLocation("TestQueryLogicFactory.xml");
    queryFactory.refresh();
    factoryConfig = queryFactory.getBean(QueryLogicFactoryConfiguration.class.getSimpleName(), QueryLogicFactoryConfiguration.class);
    
    Whitebox.setInternalState(bean, QueryLogicFactoryConfiguration.class, factoryConfig);
    Whitebox.setInternalState(bean, ClassPathXmlApplicationContext.class, queryFactory);
    
    ctx = createMock(EJBContext.class);
    logic = createMockBuilder(BaseQueryLogic.class).addMockedMethods("setLogicName", "getMaxPageSize", "getPageByteTrigger").createMock();
    DatawaveUser user = new DatawaveUser(SubjectIssuerDNPair.of("CN=Poe Edgar Allan eapoe, OU=acme", "<CN=ca, OU=acme>"), UserType.USER, null, null, null,
                    0L);
    principal = new DatawavePrincipal(Collections.singletonList(user));
}
 
Example #6
Source File: BeanFactoryUtilsTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() {
	// Interesting hierarchical factory to test counts.
	// Slow to read so we cache it.

	DefaultListableBeanFactory grandParent = new DefaultListableBeanFactory();
	new XmlBeanDefinitionReader(grandParent).loadBeanDefinitions(ROOT_CONTEXT);
	DefaultListableBeanFactory parent = new DefaultListableBeanFactory(grandParent);
	new XmlBeanDefinitionReader(parent).loadBeanDefinitions(MIDDLE_CONTEXT);
	DefaultListableBeanFactory child = new DefaultListableBeanFactory(parent);
	new XmlBeanDefinitionReader(child).loadBeanDefinitions(LEAF_CONTEXT);

	this.dependentBeansFactory = new DefaultListableBeanFactory();
	new XmlBeanDefinitionReader(this.dependentBeansFactory).loadBeanDefinitions(DEPENDENT_BEANS_CONTEXT);
	dependentBeansFactory.preInstantiateSingletons();
	this.listableBeanFactory = child;
}
 
Example #7
Source File: ProxyFactoryBeanTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testGetObjectTypeWithNoTargetOrTargetSource() {
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	new XmlBeanDefinitionReader(bf).loadBeanDefinitions(new ClassPathResource(TARGETSOURCE_CONTEXT, CLASS));

	ITestBean tb = (ITestBean) bf.getBean("noTarget");
	try {
		tb.getName();
		fail();
	}
	catch (UnsupportedOperationException ex) {
		assertEquals("getName", ex.getMessage());
	}
	FactoryBean<?> pfb = (ProxyFactoryBean) bf.getBean("&noTarget");
	assertTrue("Has correct object type", ITestBean.class.isAssignableFrom(pfb.getObjectType()));
}
 
Example #8
Source File: HapporSpringContext.java    From happor with MIT License 6 votes vote down vote up
public HapporSpringContext(final ClassLoader classLoader, String filename) {
	this.classLoader = classLoader;
	
	ctx = new FileSystemXmlApplicationContext(filename) {
		@Override
		protected void initBeanDefinitionReader(XmlBeanDefinitionReader reader) {
            super.initBeanDefinitionReader(reader);
            reader.setBeanClassLoader(classLoader);
            setClassLoader(classLoader);
		}
	};
	
	registerAllBeans();

	setServer(ctx.getBean(HapporWebserver.class));

	try {
		setWebserverHandler(ctx.getBean(WebserverHandler.class));
	} catch (NoSuchBeanDefinitionException e) {
		logger.warn("has no WebserverHandler");
	}
	

}
 
Example #9
Source File: SpringServiceBuilderFactory.java    From cxf with Apache License 2.0 6 votes vote down vote up
/**
 * This is factored out to permit use in a unit test.
 *
 * @param additionalFilePathnames
 * @return
 */
public static ApplicationContext getApplicationContext(List<String> additionalFilePathnames) {
    BusApplicationContext busApplicationContext = BusFactory.getDefaultBus()
        .getExtension(BusApplicationContext.class);
    GenericApplicationContext appContext = new GenericApplicationContext(busApplicationContext);
    XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(appContext);
    List<URL> urls = ClassLoaderUtils.getResources("META-INF/cxf/java2wsbeans.xml",
                                                   SpringServiceBuilderFactory.class);
    for (URL url : urls) {
        reader.loadBeanDefinitions(new UrlResource(url));
    }

    for (String pathname : additionalFilePathnames) {
        try {
            reader.loadBeanDefinitions(new FileSystemResource(pathname));
        } catch (BeanDefinitionStoreException bdse) {
            throw new ToolException("Unable to open bean definition file " + pathname, bdse.getCause());
        }
    }
    appContext.refresh();
    return appContext;
}
 
Example #10
Source File: GenericSqlQueryTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {
	this.beanFactory = new DefaultListableBeanFactory();
	new XmlBeanDefinitionReader((BeanDefinitionRegistry) this.beanFactory).loadBeanDefinitions(
			new ClassPathResource("org/springframework/jdbc/object/GenericSqlQueryTests-context.xml"));
	DataSource dataSource = mock(DataSource.class);
	this.connection = mock(Connection.class);
	this.preparedStatement = mock(PreparedStatement.class);
	this.resultSet = mock(ResultSet.class);
	given(dataSource.getConnection()).willReturn(connection);
	TestDataSourceWrapper testDataSource = (TestDataSourceWrapper) beanFactory.getBean("dataSource");
	testDataSource.setTarget(dataSource);
}
 
Example #11
Source File: AopNamespaceHandlerPointcutErrorTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testDuplicatePointcutConfig() {
	try {
		DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
		new XmlBeanDefinitionReader(bf).loadBeanDefinitions(
				qualifiedResource(getClass(), "pointcutDuplication.xml"));
		fail("parsing should have caused a BeanDefinitionStoreException");
	}
	catch (BeanDefinitionStoreException ex) {
		assertTrue(ex.contains(BeanDefinitionParsingException.class));
	}
}
 
Example #12
Source File: JeeNamespaceHandlerTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {
	GenericApplicationContext ctx = new GenericApplicationContext();
	new XmlBeanDefinitionReader(ctx).loadBeanDefinitions(
			new ClassPathResource("jeeNamespaceHandlerTests.xml", getClass()));
	ctx.refresh();
	this.beanFactory = ctx.getBeanFactory();
	this.beanFactory.getBeanNamesForType(ITestBean.class);
}
 
Example #13
Source File: PropertyPathFactoryBeanTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testPropertyPathFactoryBeanWithNullResult() {
	DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
	new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(CONTEXT);
	assertNull(xbf.getType("tb.spouse.spouse"));
	assertNull(xbf.getBean("tb.spouse.spouse"));
}
 
Example #14
Source File: MessageBrokerBeanDefinitionParserTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private void loadBeanDefinitions(String fileName) {
	XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(this.appContext);
	ClassPathResource resource = new ClassPathResource(fileName, MessageBrokerBeanDefinitionParserTests.class);
	reader.loadBeanDefinitions(resource);
	this.appContext.setServletContext(new MockServletContext());
	this.appContext.refresh();
}
 
Example #15
Source File: AutowiredConfigurationTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testAutowiredAnnotatedConstructorSupported() {
	DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
	new XmlBeanDefinitionReader(factory).loadBeanDefinitions(
			new ClassPathResource("annotation-config.xml", MultipleConstructorConfig.class));
	GenericApplicationContext ctx = new GenericApplicationContext(factory);
	ctx.registerBeanDefinition("config1", new RootBeanDefinition(MultipleConstructorConfig.class));
	ctx.registerBeanDefinition("config2", new RootBeanDefinition(ColorConfig.class));
	ctx.refresh();
	assertSame(ctx.getBean(MultipleConstructorConfig.class).colour, ctx.getBean(Colour.class));
}
 
Example #16
Source File: AnnotationDependencyInjectionResolutionDemo.java    From geekbang-lessons with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {

        // 创建 BeanFactory 容器
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
        // 注册 Configuration Class(配置类) -> Spring Bean
        applicationContext.register(AnnotationDependencyInjectionResolutionDemo.class);

        XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(applicationContext);

        String xmlResourcePath = "classpath:/META-INF/dependency-lookup-context.xml";
        // 加载 XML 资源,解析并且生成 BeanDefinition
        beanDefinitionReader.loadBeanDefinitions(xmlResourcePath);

        // 启动 Spring 应用上下文
        applicationContext.refresh();

        // 依赖查找 QualifierAnnotationDependencyInjectionDemo Bean
        AnnotationDependencyInjectionResolutionDemo demo = applicationContext.getBean(AnnotationDependencyInjectionResolutionDemo.class);

        // 期待输出 superUser Bean
        System.out.println("demo.user = " + demo.user);
        System.out.println("demo.injectedUser = " + demo.injectedUser);

        // 期待输出 user superUser
        System.out.println("demo.users = " + demo.users);
        // 期待输出 superUser Bean
        System.out.println("demo.userOptional = " + demo.userOptional);
        // 期待输出 superUser Bean
        System.out.println("demo.myInjectedUser = " + demo.myInjectedUser);


        // 显示地关闭 Spring 应用上下文
        applicationContext.close();
    }
 
Example #17
Source File: ComponentScanParserBeanDefinitionDefaultsTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testDependencyCheckObjectsWithAutowireByName() {
	GenericApplicationContext context = new GenericApplicationContext();
	XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(context);
	reader.loadBeanDefinitions(LOCATION_PREFIX + "defaultDependencyCheckObjectsWithAutowireByNameTests.xml");
	context.refresh();
	DefaultsTestBean bean = (DefaultsTestBean) context.getBean(TEST_BEAN_NAME);
	assertNull("constructor dependency should not have been autowired", bean.getConstructorDependency());
	assertNotNull("property dependencies should have been autowired", bean.getPropertyDependency1());
	assertNotNull("property dependencies should have been autowired", bean.getPropertyDependency2());
}
 
Example #18
Source File: BeanFactoryGenericsTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testGenericSetBean() throws Exception {
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	new XmlBeanDefinitionReader(bf).loadBeanDefinitions(
			new ClassPathResource("genericBeanTests.xml", getClass()));
	Set<?> set = (Set<?>) bf.getBean("set");
	assertEquals(1, set.size());
	assertEquals(new URL("http://localhost:8080"), set.iterator().next());
}
 
Example #19
Source File: ProxyFactoryBeanTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testProxyNotSerializableBecauseOfAdvice() throws Exception {
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	new XmlBeanDefinitionReader(bf).loadBeanDefinitions(new ClassPathResource(SERIALIZATION_CONTEXT, CLASS));
	Person p = (Person) bf.getBean("interceptorNotSerializableSingleton");
	assertFalse("Not serializable because an interceptor isn't serializable", SerializationTestUtils.isSerializable(p));
}
 
Example #20
Source File: ProxyFactoryBeanTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testSerializablePrototypeProxy() throws Exception {
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	new XmlBeanDefinitionReader(bf).loadBeanDefinitions(new ClassPathResource(SERIALIZATION_CONTEXT, CLASS));
	Person p = (Person) bf.getBean("serializablePrototype");
	assertNotSame("Should not be a Singleton", p, bf.getBean("serializablePrototype"));
	Person p2 = (Person) SerializationTestUtils.serializeAndDeserialize(p);
	assertEquals(p, p2);
	assertNotSame(p, p2);
	assertEquals("serializablePrototype", p2.getName());
}
 
Example #21
Source File: RequestScopeTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Before
public void setup() throws Exception {
	this.beanFactory.registerScope("request", new RequestScope());
	this.beanFactory.setBeanExpressionResolver(new StandardBeanExpressionResolver());
	XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(this.beanFactory);
	reader.loadBeanDefinitions(new ClassPathResource("requestScopeTests.xml", getClass()));
	this.beanFactory.preInstantiateSingletons();
}
 
Example #22
Source File: ScopedProxyTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test  // SPR-2108
public void testProxyAssignable() throws Exception {
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	new XmlBeanDefinitionReader(bf).loadBeanDefinitions(MAP_CONTEXT);
	Object baseMap = bf.getBean("singletonMap");
	assertTrue(baseMap instanceof Map);
}
 
Example #23
Source File: ProxyFactoryBeanTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Globals must be followed by a target.
 */
@Test
public void testGlobalsWithoutTarget() {
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	new XmlBeanDefinitionReader(bf).loadBeanDefinitions(new ClassPathResource(INVALID_CONTEXT, CLASS));
	try {
		bf.getBean("globalsWithoutTarget");
		fail("Should require target name");
	}
	catch (BeanCreationException ex) {
		assertTrue(ex.getCause() instanceof AopConfigException);
	}
}
 
Example #24
Source File: ScopedProxyTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testScopedOverride() throws Exception {
	GenericApplicationContext ctx = new GenericApplicationContext();
	new XmlBeanDefinitionReader(ctx).loadBeanDefinitions(OVERRIDE_CONTEXT);
	SimpleMapScope scope = new SimpleMapScope();
	ctx.getBeanFactory().registerScope("request", scope);
	ctx.refresh();

	ITestBean bean = (ITestBean) ctx.getBean("testBean");
	assertEquals("male", bean.getName());
	assertEquals(99, bean.getAge());

	assertTrue(scope.getMap().containsKey("scopedTarget.testBean"));
	assertEquals(TestBean.class, scope.getMap().get("scopedTarget.testBean").getClass());
}
 
Example #25
Source File: ProxyFactoryBeanTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testEmptyInterceptorNames() {
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	new XmlBeanDefinitionReader(bf).loadBeanDefinitions(new ClassPathResource(INVALID_CONTEXT, CLASS));
	try {
		bf.getBean("emptyInterceptorNames");
		fail("Interceptor names cannot be empty");
	}
	catch (BeanCreationException ex) {
		// Ok
	}
}
 
Example #26
Source File: ScopedProxyTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testJdkScopedProxy() throws Exception {
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	new XmlBeanDefinitionReader(bf).loadBeanDefinitions(TESTBEAN_CONTEXT);
	bf.setSerializationId("X");
	SimpleMapScope scope = new SimpleMapScope();
	bf.registerScope("request", scope);

	ITestBean bean = (ITestBean) bf.getBean("testBean");
	assertNotNull(bean);
	assertTrue(AopUtils.isJdkDynamicProxy(bean));
	assertTrue(bean instanceof ScopedObject);
	ScopedObject scoped = (ScopedObject) bean;
	assertEquals(TestBean.class, scoped.getTargetObject().getClass());
	bean.setAge(101);

	assertTrue(scope.getMap().containsKey("testBeanTarget"));
	assertEquals(TestBean.class, scope.getMap().get("testBeanTarget").getClass());

	ITestBean deserialized = (ITestBean) SerializationTestUtils.serializeAndDeserialize(bean);
	assertNotNull(deserialized);
	assertTrue(AopUtils.isJdkDynamicProxy(deserialized));
	assertEquals(101, bean.getAge());
	assertTrue(deserialized instanceof ScopedObject);
	ScopedObject scopedDeserialized = (ScopedObject) deserialized;
	assertEquals(TestBean.class, scopedDeserialized.getTargetObject().getClass());

	bf.setSerializationId(null);
}
 
Example #27
Source File: FieldRetrievingFactoryBeanTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testBeanNameSyntaxWithBeanFactory() throws Exception {
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	new XmlBeanDefinitionReader(bf).loadBeanDefinitions(CONTEXT);
	TestBean testBean = (TestBean) bf.getBean("testBean");
	assertEquals(new Integer(Connection.TRANSACTION_SERIALIZABLE), testBean.getSomeIntegerArray()[0]);
	assertEquals(new Integer(Connection.TRANSACTION_SERIALIZABLE), testBean.getSomeIntegerArray()[1]);
}
 
Example #28
Source File: ScopedProxyTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testCglibScopedProxy() throws Exception {
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	new XmlBeanDefinitionReader(bf).loadBeanDefinitions(LIST_CONTEXT);
	bf.setSerializationId("Y");
	SimpleMapScope scope = new SimpleMapScope();
	bf.registerScope("request", scope);

	TestBean tb = (TestBean) bf.getBean("testBean");
	assertTrue(AopUtils.isCglibProxy(tb.getFriends()));
	assertTrue(tb.getFriends() instanceof ScopedObject);
	ScopedObject scoped = (ScopedObject) tb.getFriends();
	assertEquals(ArrayList.class, scoped.getTargetObject().getClass());
	tb.getFriends().add("myFriend");

	assertTrue(scope.getMap().containsKey("scopedTarget.scopedList"));
	assertEquals(ArrayList.class, scope.getMap().get("scopedTarget.scopedList").getClass());

	ArrayList<?> deserialized = (ArrayList<?>) SerializationTestUtils.serializeAndDeserialize(tb.getFriends());
	assertNotNull(deserialized);
	assertTrue(AopUtils.isCglibProxy(deserialized));
	assertTrue(deserialized.contains("myFriend"));
	assertTrue(deserialized instanceof ScopedObject);
	ScopedObject scopedDeserialized = (ScopedObject) deserialized;
	assertEquals(ArrayList.class, scopedDeserialized.getTargetObject().getClass());

	bf.setSerializationId(null);
}
 
Example #29
Source File: ProxyFactoryBeanTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private void testDoubleTargetSourceIsRejected(String name) {
	try {
		DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
		new XmlBeanDefinitionReader(bf).loadBeanDefinitions(new ClassPathResource(DBL_TARGETSOURCE_CONTEXT, CLASS));
		bf.getBean(name);
		fail("Should not allow TargetSource to be specified in interceptorNames as well as targetSource property");
	}
	catch (BeanCreationException ex) {
		// Root cause of the problem must be an AOP exception
		AopConfigException aex = (AopConfigException) ex.getCause();
		assertTrue(aex.getMessage().contains("TargetSource"));
	}
}
 
Example #30
Source File: XmlViewResolver.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Initialize the view bean factory from the XML file.
 * Synchronized because of access by parallel threads.
 * @throws BeansException in case of initialization errors
 */
protected synchronized BeanFactory initFactory() throws BeansException {
	if (this.cachedFactory != null) {
		return this.cachedFactory;
	}

	Resource actualLocation = this.location;
	if (actualLocation == null) {
		actualLocation = getApplicationContext().getResource(DEFAULT_LOCATION);
	}

	// Create child ApplicationContext for views.
	GenericWebApplicationContext factory = new GenericWebApplicationContext();
	factory.setParent(getApplicationContext());
	factory.setServletContext(getServletContext());

	// Load XML resource with context-aware entity resolver.
	XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(factory);
	reader.setEnvironment(getApplicationContext().getEnvironment());
	reader.setEntityResolver(new ResourceEntityResolver(getApplicationContext()));
	reader.loadBeanDefinitions(actualLocation);

	factory.refresh();

	if (isCache()) {
		this.cachedFactory = factory;
	}
	return factory;
}