Lookup methods can support arguments, find a target bean based on the return type, and be identified by an @Lookup annotation
Issue: SPR-7431 Issue: SPR-5192
This commit is contained in:
@@ -0,0 +1,116 @@
|
||||
/*
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.beans.factory.annotation;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
||||
import org.springframework.beans.factory.support.RootBeanDefinition;
|
||||
import org.springframework.tests.sample.beans.TestBean;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* @author Karl Pietrzak
|
||||
* @author Juergen Hoeller
|
||||
*/
|
||||
public class LookupAnnotationTests {
|
||||
|
||||
private DefaultListableBeanFactory beanFactory;
|
||||
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
beanFactory = new DefaultListableBeanFactory();
|
||||
AutowiredAnnotationBeanPostProcessor aabpp = new AutowiredAnnotationBeanPostProcessor();
|
||||
aabpp.setBeanFactory(beanFactory);
|
||||
beanFactory.addBeanPostProcessor(aabpp);
|
||||
beanFactory.registerBeanDefinition("abstractBean", new RootBeanDefinition(AbstractBean.class));
|
||||
RootBeanDefinition tbd = new RootBeanDefinition(TestBean.class);
|
||||
tbd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
|
||||
beanFactory.registerBeanDefinition("testBean", tbd);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testWithoutConstructorArg() {
|
||||
AbstractBean bean = (AbstractBean) beanFactory.getBean("abstractBean");
|
||||
assertNotNull(bean);
|
||||
Object expected = bean.get();
|
||||
assertEquals(TestBean.class, expected.getClass());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWithOverloadedArg() {
|
||||
AbstractBean bean = (AbstractBean) beanFactory.getBean("abstractBean");
|
||||
assertNotNull(bean);
|
||||
TestBean expected = bean.get("haha");
|
||||
assertEquals(TestBean.class, expected.getClass());
|
||||
assertEquals("haha", expected.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWithOneConstructorArg() {
|
||||
AbstractBean bean = (AbstractBean) beanFactory.getBean("abstractBean");
|
||||
assertNotNull(bean);
|
||||
TestBean expected = bean.getOneArgument("haha");
|
||||
assertEquals(TestBean.class, expected.getClass());
|
||||
assertEquals("haha", expected.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWithTwoConstructorArg() {
|
||||
AbstractBean bean = (AbstractBean) beanFactory.getBean("abstractBean");
|
||||
assertNotNull(bean);
|
||||
TestBean expected = bean.getTwoArguments("haha", 72);
|
||||
assertEquals(TestBean.class, expected.getClass());
|
||||
assertEquals("haha", expected.getName());
|
||||
assertEquals(72, expected.getAge());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWithThreeArgsShouldFail() {
|
||||
AbstractBean bean = (AbstractBean) beanFactory.getBean("abstractBean");
|
||||
assertNotNull(bean);
|
||||
try {
|
||||
bean.getThreeArguments("name", 1, 2);
|
||||
fail("TestBean does not have a three arg constructor so this should not have worked");
|
||||
}
|
||||
catch (AbstractMethodError ex) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static abstract class AbstractBean {
|
||||
|
||||
@Lookup
|
||||
public abstract TestBean get();
|
||||
|
||||
@Lookup
|
||||
public abstract TestBean get(String name); // overloaded
|
||||
|
||||
@Lookup
|
||||
public abstract TestBean getOneArgument(String name);
|
||||
|
||||
@Lookup
|
||||
public abstract TestBean getTwoArguments(String name, int age);
|
||||
|
||||
public abstract TestBean getThreeArguments(String name, int age, int anotherArg);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
/*
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.beans.factory.support;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.tests.sample.beans.TestBean;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* @author Karl Pietrzak
|
||||
* @author Juergen Hoeller
|
||||
*/
|
||||
public class LookupMethodTests {
|
||||
|
||||
private DefaultListableBeanFactory beanFactory;
|
||||
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
beanFactory = new DefaultListableBeanFactory();
|
||||
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory);
|
||||
reader.loadBeanDefinitions(new ClassPathResource("lookupMethodTests.xml", getClass()));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testWithoutConstructorArg() {
|
||||
AbstractBean bean = (AbstractBean) beanFactory.getBean("abstractBean");
|
||||
assertNotNull(bean);
|
||||
Object expected = bean.get();
|
||||
assertEquals(TestBean.class, expected.getClass());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWithOverloadedArg() {
|
||||
AbstractBean bean = (AbstractBean) beanFactory.getBean("abstractBean");
|
||||
assertNotNull(bean);
|
||||
TestBean expected = bean.get("haha");
|
||||
assertEquals(TestBean.class, expected.getClass());
|
||||
assertEquals("haha", expected.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWithOneConstructorArg() {
|
||||
AbstractBean bean = (AbstractBean) beanFactory.getBean("abstractBean");
|
||||
assertNotNull(bean);
|
||||
TestBean expected = bean.getOneArgument("haha");
|
||||
assertEquals(TestBean.class, expected.getClass());
|
||||
assertEquals("haha", expected.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWithTwoConstructorArg() {
|
||||
AbstractBean bean = (AbstractBean) beanFactory.getBean("abstractBean");
|
||||
assertNotNull(bean);
|
||||
TestBean expected = bean.getTwoArguments("haha", 72);
|
||||
assertEquals(TestBean.class, expected.getClass());
|
||||
assertEquals("haha", expected.getName());
|
||||
assertEquals(72, expected.getAge());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWithThreeArgsShouldFail() {
|
||||
AbstractBean bean = (AbstractBean) beanFactory.getBean("abstractBean");
|
||||
assertNotNull(bean);
|
||||
try {
|
||||
bean.getThreeArguments("name", 1, 2);
|
||||
fail("TestBean does not have a three arg constructor so this should not have worked");
|
||||
}
|
||||
catch (AbstractMethodError ex) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static abstract class AbstractBean {
|
||||
|
||||
public abstract TestBean get();
|
||||
|
||||
public abstract TestBean get(String name); // overloaded
|
||||
|
||||
public abstract TestBean getOneArgument(String name);
|
||||
|
||||
public abstract TestBean getTwoArguments(String name, int age);
|
||||
|
||||
public abstract TestBean getThreeArguments(String name, int age, int anotherArg);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd">
|
||||
|
||||
<bean id="abstractBean" class="org.springframework.beans.factory.support.LookupMethodTests$AbstractBean">
|
||||
<lookup-method name="get"/> <!-- applying to overloaded methods, and based on return type since no bean name is given -->
|
||||
<lookup-method name="getOneArgument" bean="testBean"/>
|
||||
<lookup-method name="getTwoArguments" bean="testBean"/>
|
||||
</bean>
|
||||
|
||||
<bean id="testBean" class="org.springframework.tests.sample.beans.TestBean" scope="prototype"/>
|
||||
|
||||
</beans>
|
||||
Reference in New Issue
Block a user