📄 xmlbeanfactorytests.java
字号:
tbArg.setName("arg1");
TestBean tbArg2 = new TestBean();
tbArg2.setName("arg2");
FactoryMethods fm1 = (FactoryMethods) xbf.getBean("testBeanOnlyPrototype", new Object[]{tbArg});
FactoryMethods fm2 = (FactoryMethods) xbf.getBean("testBeanOnlyPrototype", new Object[]{tbArg2});
assertEquals(0, fm1.getNum());
assertEquals("default", fm1.getName());
// This comes from the test bean
assertEquals("arg1", fm1.getTestBean().getName());
assertEquals("arg2", fm2.getTestBean().getName());
assertEquals(fm1.getNum(), fm2.getNum());
assertEquals(fm2.getStringValue(), "testBeanOnlyPrototypeDISetterString");
assertEquals(fm2.getStringValue(), fm2.getStringValue());
// The TestBean reference is resolved to a prototype in the factory
assertSame(fm2.getTestBean(), fm2.getTestBean());
assertNotSame(fm1, fm2);
}
public void testCannotSpecifyFactoryMethodArgumentsOnSingleton() {
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf);
reader.setValidating(true);
reader.loadBeanDefinitions(new ClassPathResource("factory-methods.xml", getClass()));
try {
xbf.getBean("testBeanOnly", new Object[]{new TestBean()});
fail("Shouldn't allow args to be passed to a singleton");
}
catch (BeanDefinitionStoreException ex) {
// OK
}
}
public void testFactoryMethodWithDifferentReturnType() {
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf);
reader.setValidating(true);
reader.loadBeanDefinitions(new ClassPathResource("factory-methods.xml", getClass()));
// Check that listInstance is not considered a bean of type FactoryMethods.
assertTrue(List.class.isAssignableFrom(xbf.getType("listInstance")));
String[] names = xbf.getBeanNamesForType(FactoryMethods.class);
assertTrue(!Arrays.asList(names).contains("listInstance"));
names = xbf.getBeanNamesForType(List.class);
assertTrue(Arrays.asList(names).contains("listInstance"));
xbf.preInstantiateSingletons();
assertTrue(List.class.isAssignableFrom(xbf.getType("listInstance")));
names = xbf.getBeanNamesForType(FactoryMethods.class);
assertTrue(!Arrays.asList(names).contains("listInstance"));
names = xbf.getBeanNamesForType(List.class);
assertTrue(Arrays.asList(names).contains("listInstance"));
List list = (List) xbf.getBean("listInstance");
assertEquals(Collections.EMPTY_LIST, list);
}
public void testFactoryMethodForJavaMailSession() {
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf);
reader.setValidating(true);
reader.loadBeanDefinitions(new ClassPathResource("factory-methods.xml", getClass()));
Session session = (Session) xbf.getBean("javaMailSession");
assertEquals("someuser", session.getProperty("mail.smtp.user"));
assertEquals("somepw", session.getProperty("mail.smtp.password"));
}
public void testCannotSpecifyFactoryMethodArgumentsExceptWithFactoryMethod() {
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf);
reader.setValidating(true);
reader.loadBeanDefinitions(new ClassPathResource("overrides.xml", getClass()));
try {
xbf.getBean("overrideOnPrototype", new Object[]{new TestBean()});
fail("Shouldn't allow args to be passed to a Setter-Injected object");
}
catch (BeanDefinitionStoreException ex) {
// OK
}
}
public void testConstructorArgWithSingleSimpleTypeMatch() {
XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("constructor-arg.xml", getClass()));
SingleSimpleTypeConstructorBean bean = (SingleSimpleTypeConstructorBean) xbf.getBean("beanWithBoolean");
assertTrue(bean.isSingleBoolean());
SingleSimpleTypeConstructorBean bean2 = (SingleSimpleTypeConstructorBean) xbf.getBean("beanWithBoolean2");
assertTrue(bean2.isSingleBoolean());
}
public void testConstructorArgWithDoubleSimpleTypeMatch() {
XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("constructor-arg.xml", getClass()));
SingleSimpleTypeConstructorBean bean = (SingleSimpleTypeConstructorBean) xbf.getBean("beanWithBooleanAndString");
assertTrue(bean.isSecondBoolean());
assertEquals("A String", bean.getTestString());
SingleSimpleTypeConstructorBean bean2 = (SingleSimpleTypeConstructorBean) xbf.getBean("beanWithBooleanAndString2");
assertTrue(bean2.isSecondBoolean());
assertEquals("A String", bean2.getTestString());
}
public void testDoubleBooleanAutowire() {
XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("constructor-arg.xml", getClass()));
DoubleBooleanConstructorBean bean = (DoubleBooleanConstructorBean) xbf.getBean("beanWithDoubleBoolean");
assertEquals(Boolean.TRUE, bean.boolean1);
assertEquals(Boolean.FALSE, bean.boolean2);
}
public void testDoubleBooleanAutowireWithIndex() {
XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("constructor-arg.xml", getClass()));
DoubleBooleanConstructorBean bean = (DoubleBooleanConstructorBean) xbf.getBean("beanWithDoubleBooleanAndIndex");
assertEquals(Boolean.FALSE, bean.boolean1);
assertEquals(Boolean.TRUE, bean.boolean2);
}
public static class DoSomethingReplacer implements MethodReplacer {
public Object lastArg;
public Object reimplement(Object obj, Method method, Object[] args) throws Throwable {
assertEquals(1, args.length);
assertEquals("doSomething", method.getName());
lastArg = args[0];
return null;
}
}
public static class BadInitializer {
/** Init method */
public void init2() throws ServletException {
throw new ServletException();
}
}
public static class DoubleInitializer {
private int num;
public int getNum() {
return num;
}
public void setNum(int i) {
num = i;
}
/** Init method */
public void init() {
this.num *= 2;
}
}
public static class InitAndIB implements InitializingBean, DisposableBean {
public static boolean constructed;
public boolean afterPropertiesSetInvoked, initMethodInvoked, destroyed, customDestroyed;
public InitAndIB() {
constructed = true;
}
public void afterPropertiesSet() {
if (this.initMethodInvoked) {
fail();
}
this.afterPropertiesSetInvoked = true;
}
/** Init method */
public void customInit() throws ServletException {
if (!this.afterPropertiesSetInvoked) {
fail();
}
this.initMethodInvoked = true;
}
public void destroy() {
if (this.customDestroyed) {
fail();
}
if (this.destroyed) {
throw new IllegalStateException("Already destroyed");
}
this.destroyed = true;
}
public void customDestroy() {
if (!this.destroyed) {
fail();
}
if (this.customDestroyed) {
throw new IllegalStateException("Already customDestroyed");
}
this.customDestroyed = true;
}
}
public static class PreparingBean1 implements DisposableBean {
public static boolean prepared = false;
public static boolean destroyed = false;
public PreparingBean1() {
prepared = true;
}
public void destroy() {
destroyed = true;
}
}
public static class PreparingBean2 implements DisposableBean {
public static boolean prepared = false;
public static boolean destroyed = false;
public PreparingBean2() {
prepared = true;
}
public void destroy() {
destroyed = true;
}
}
public static class DependingBean implements InitializingBean, DisposableBean {
public static boolean destroyed = false;
public DependingBean() {
}
public DependingBean(PreparingBean1 bean1, PreparingBean2 bean2) {
}
public void setBean1(PreparingBean1 bean1) {
}
public void setBean2(PreparingBean2 bean2) {
}
public void setInTheMiddleBean(InTheMiddleBean bean) {
}
public void afterPropertiesSet() {
if (!(PreparingBean1.prepared && PreparingBean2.prepared)) {
throw new IllegalStateException("Need prepared PreparedBeans!");
}
}
public void destroy() {
if (PreparingBean1.destroyed || PreparingBean2.destroyed) {
throw new IllegalStateException("Should not be destroyed before PreparedBeans");
}
destroyed = true;
}
}
public static class InTheMiddleBean {
public void setBean1(PreparingBean1 bean1) {
}
public void setBean2(PreparingBean2 bean2) {
}
}
public static class HoldingBean implements DisposableBean {
public static boolean destroyed = false;
public void setDependingBean(DependingBean dependingBean) {
}
public void destroy() {
if (DependingBean.destroyed) {
throw new IllegalStateException("Should not be destroyed before DependingBean");
}
destroyed = true;
}
}
public static class DoubleBooleanConstructorBean {
private Boolean boolean1;
private Boolean boolean2;
public DoubleBooleanConstructorBean(Boolean b1, Boolean b2) {
this.boolean1 = b1;
this.boolean2 = b2;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -