org.springframework.aop.target.LazyInitTargetSource Java Examples

The following examples show how to use org.springframework.aop.target.LazyInitTargetSource. 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: LazyInitTargetSourceCreator.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
@Nullable
protected AbstractBeanFactoryBasedTargetSource createBeanFactoryBasedTargetSource(
		Class<?> beanClass, String beanName) {

	if (getBeanFactory() instanceof ConfigurableListableBeanFactory) {
		BeanDefinition definition =
				((ConfigurableListableBeanFactory) getBeanFactory()).getBeanDefinition(beanName);
		if (definition.isLazyInit()) {
			return new LazyInitTargetSource();
		}
	}
	return null;
}
 
Example #2
Source File: AdvisorAutoProxyCreatorTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testLazyInitTargetSource() throws Exception {
	CountingTestBean.count = 0;
	BeanFactory bf = new ClassPathXmlApplicationContext(CUSTOM_TARGETSOURCE_CONTEXT, CLASS);
	ITestBean test = (ITestBean) bf.getBean("lazyInitTest");
	assertTrue(AopUtils.isAopProxy(test));
	Advised advised = (Advised) test;
	assertTrue(advised.getTargetSource() instanceof LazyInitTargetSource);
	assertEquals("No CountingTestBean instantiated yet", 0, CountingTestBean.count);
	assertEquals("Rod", test.getName());
	assertEquals("Kerry", test.getSpouse().getName());
	assertEquals("Only 1 CountingTestBean instantiated", 1, CountingTestBean.count);
	CountingTestBean.count = 0;
}
 
Example #3
Source File: AdvisorAutoProxyCreatorTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testLazyInitTargetSource() throws Exception {
	CountingTestBean.count = 0;
	BeanFactory bf = new ClassPathXmlApplicationContext(CUSTOM_TARGETSOURCE_CONTEXT, CLASS);
	ITestBean test = (ITestBean) bf.getBean("lazyInitTest");
	assertTrue(AopUtils.isAopProxy(test));
	Advised advised = (Advised) test;
	assertTrue(advised.getTargetSource() instanceof LazyInitTargetSource);
	assertEquals("No CountingTestBean instantiated yet", 0, CountingTestBean.count);
	assertEquals("Rod", test.getName());
	assertEquals("Kerry", test.getSpouse().getName());
	assertEquals("Only 1 CountingTestBean instantiated", 1, CountingTestBean.count);
	CountingTestBean.count = 0;
}
 
Example #4
Source File: LazyInitTargetSourceCreator.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected AbstractBeanFactoryBasedTargetSource createBeanFactoryBasedTargetSource(
		Class<?> beanClass, String beanName) {

	if (getBeanFactory() instanceof ConfigurableListableBeanFactory) {
		BeanDefinition definition =
				((ConfigurableListableBeanFactory) getBeanFactory()).getBeanDefinition(beanName);
		if (definition.isLazyInit()) {
			return new LazyInitTargetSource();
		}
	}
	return null;
}
 
Example #5
Source File: AdvisorAutoProxyCreatorTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testLazyInitTargetSource() throws Exception {
	CountingTestBean.count = 0;
	BeanFactory bf = new ClassPathXmlApplicationContext(CUSTOM_TARGETSOURCE_CONTEXT, CLASS);
	ITestBean test = (ITestBean) bf.getBean("lazyInitTest");
	assertTrue(AopUtils.isAopProxy(test));
	Advised advised = (Advised) test;
	assertTrue(advised.getTargetSource() instanceof LazyInitTargetSource);
	assertEquals("No CountingTestBean instantiated yet", 0, CountingTestBean.count);
	assertEquals("Rod", test.getName());
	assertEquals("Kerry", test.getSpouse().getName());
	assertEquals("Only 1 CountingTestBean instantiated", 1, CountingTestBean.count);
	CountingTestBean.count = 0;
}
 
Example #6
Source File: MessageBusAdapterConfiguration.java    From spring-bus with Apache License 2.0 5 votes vote down vote up
private <T> T createLazyProxy(ListableBeanFactory beanFactory, Class<T> type) {
	ProxyFactory factory = new ProxyFactory();
	LazyInitTargetSource source = new LazyInitTargetSource();
	source.setTargetClass(type);
	source.setTargetBeanName(getBeanNameFor(beanFactory, MessageBus.class));
	source.setBeanFactory(beanFactory);
	factory.setTargetSource(source);
	factory.addAdvice(new PassthruAdvice());
	factory.setInterfaces(new Class<?>[] { type });
	@SuppressWarnings("unchecked")
	T proxy = (T) factory.getProxy();
	return proxy;
}