@Override public PropertyValues postProcessPropertyValues( PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) { if (!this.validatedBeanNames.contains(beanName)) { if (!shouldSkip(this.beanFactory, beanName)) { List<String> invalidProperties = new ArrayList<>(); for (PropertyDescriptor pd : pds) { if (isRequiredProperty(pd) && !pvs.contains(pd.getName())) { invalidProperties.add(pd.getName()); } } if (!invalidProperties.isEmpty()) { throw new BeanInitializationException(buildExceptionMessage(invalidProperties, beanName)); } } this.validatedBeanNames.add(beanName); } return pvs; }
/** * Is the supplied property required to have a value (that is, to be dependency-injected)? * <p>This implementation looks for the existence of a * {@link #setRequiredAnnotationType "required" annotation} * on the supplied {@link PropertyDescriptor property}. * @param propertyDescriptor the target PropertyDescriptor (never {@code null}) * @return {@code true} if the supplied property has been marked as being required; * {@code false} if not, or if the supplied property does not have a setter method */ protected boolean isRequiredProperty(PropertyDescriptor propertyDescriptor) { Method setter = propertyDescriptor.getWriteMethod(); return (setter != null && AnnotationUtils.getAnnotation(setter, getRequiredAnnotationType()) != null); }
@Test public void testWithFactoryBean() { DefaultListableBeanFactory factory = new DefaultListableBeanFactory(); RootBeanDefinition beanDef = new RootBeanDefinition(RequiredTestBean.class); beanDef.setFactoryBeanName("testBeanFactory"); beanDef.setFactoryMethodName("create"); factory.registerBeanDefinition("testBean", beanDef); factory.registerBeanDefinition("testBeanFactory", new RootBeanDefinition(RequiredTestBeanFactory.class)); RequiredAnnotationBeanPostProcessor bpp = new RequiredAnnotationBeanPostProcessor(); bpp.setBeanFactory(factory); factory.addBeanPostProcessor(bpp); factory.preInstantiateSingletons(); }
@Test public void testWithCustomAnnotation() { try { DefaultListableBeanFactory factory = new DefaultListableBeanFactory(); BeanDefinition beanDef = BeanDefinitionBuilder .genericBeanDefinition(RequiredTestBean.class) .getBeanDefinition(); factory.registerBeanDefinition("testBean", beanDef); RequiredAnnotationBeanPostProcessor rabpp = new RequiredAnnotationBeanPostProcessor(); rabpp.setRequiredAnnotationType(MyRequired.class); factory.addBeanPostProcessor(rabpp); factory.preInstantiateSingletons(); fail("Should have thrown BeanCreationException"); } catch (BeanCreationException ex) { String message = ex.getCause().getMessage(); assertTrue(message.contains("Property")); assertTrue(message.contains("name")); assertTrue(message.contains("testBean")); } }
@Test public void testWithRequiredPropertyOmitted() { try { DefaultListableBeanFactory factory = new DefaultListableBeanFactory(); BeanDefinition beanDef = BeanDefinitionBuilder .genericBeanDefinition(RequiredTestBean.class) .addPropertyValue("name", "Rob Harrop") .addPropertyValue("favouriteColour", "Blue") .addPropertyValue("jobTitle", "Grand Poobah") .getBeanDefinition(); factory.registerBeanDefinition("testBean", beanDef); factory.addBeanPostProcessor(new RequiredAnnotationBeanPostProcessor()); factory.preInstantiateSingletons(); fail("Should have thrown BeanCreationException"); } catch (BeanCreationException ex) { String message = ex.getCause().getMessage(); assertTrue(message.contains("Property")); assertTrue(message.contains("age")); assertTrue(message.contains("testBean")); } }
@Test public void testWithStaticFactoryMethod() { try { DefaultListableBeanFactory factory = new DefaultListableBeanFactory(); BeanDefinition beanDef = BeanDefinitionBuilder .genericBeanDefinition(RequiredTestBean.class) .setFactoryMethod("create") .addPropertyValue("name", "Rob Harrop") .addPropertyValue("favouriteColour", "Blue") .addPropertyValue("jobTitle", "Grand Poobah") .getBeanDefinition(); factory.registerBeanDefinition("testBean", beanDef); factory.addBeanPostProcessor(new RequiredAnnotationBeanPostProcessor()); factory.preInstantiateSingletons(); fail("Should have thrown BeanCreationException"); } catch (BeanCreationException ex) { String message = ex.getCause().getMessage(); assertTrue(message.contains("Property")); assertTrue(message.contains("age")); assertTrue(message.contains("testBean")); } }
@Override public PropertyValues postProcessPropertyValues( PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) { if (!this.validatedBeanNames.contains(beanName)) { if (!shouldSkip(this.beanFactory, beanName)) { List<String> invalidProperties = new ArrayList<>(); for (PropertyDescriptor pd : pds) { if (isRequiredProperty(pd) && !pvs.contains(pd.getName())) { invalidProperties.add(pd.getName()); } } if (!invalidProperties.isEmpty()) { throw new BeanInitializationException(buildExceptionMessage(invalidProperties, beanName)); } } this.validatedBeanNames.add(beanName); } return pvs; }
/** * Is the supplied property required to have a value (that is, to be dependency-injected)? * <p>This implementation looks for the existence of a * {@link #setRequiredAnnotationType "required" annotation} * on the supplied {@link PropertyDescriptor property}. * @param propertyDescriptor the target PropertyDescriptor (never {@code null}) * @return {@code true} if the supplied property has been marked as being required; * {@code false} if not, or if the supplied property does not have a setter method */ protected boolean isRequiredProperty(PropertyDescriptor propertyDescriptor) { Method setter = propertyDescriptor.getWriteMethod(); return (setter != null && AnnotationUtils.getAnnotation(setter, getRequiredAnnotationType()) != null); }
@Test public void testWithThreeRequiredPropertiesOmitted() { try { DefaultListableBeanFactory factory = new DefaultListableBeanFactory(); BeanDefinition beanDef = BeanDefinitionBuilder .genericBeanDefinition(RequiredTestBean.class) .addPropertyValue("name", "Rob Harrop") .getBeanDefinition(); factory.registerBeanDefinition("testBean", beanDef); factory.addBeanPostProcessor(new RequiredAnnotationBeanPostProcessor()); factory.preInstantiateSingletons(); fail("Should have thrown BeanCreationException"); } catch (BeanCreationException ex) { String message = ex.getCause().getMessage(); assertTrue(message.contains("Properties")); assertTrue(message.contains("age")); assertTrue(message.contains("favouriteColour")); assertTrue(message.contains("jobTitle")); assertTrue(message.contains("testBean")); } }
@Override public PropertyValues postProcessPropertyValues( PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException { if (!this.validatedBeanNames.contains(beanName)) { if (!shouldSkip(this.beanFactory, beanName)) { List<String> invalidProperties = new ArrayList<String>(); for (PropertyDescriptor pd : pds) { if (isRequiredProperty(pd) && !pvs.contains(pd.getName())) { invalidProperties.add(pd.getName()); } } if (!invalidProperties.isEmpty()) { throw new BeanInitializationException(buildExceptionMessage(invalidProperties, beanName)); } } this.validatedBeanNames.add(beanName); } return pvs; }
/** * Is the supplied property required to have a value (that is, to be dependency-injected)? * <p>This implementation looks for the existence of a * {@link #setRequiredAnnotationType "required" annotation} * on the supplied {@link PropertyDescriptor property}. * @param propertyDescriptor the target PropertyDescriptor (never <code>null</code>) * @return <code>true</code> if the supplied property has been marked as being required; * <code>false</code> if not, or if the supplied property does not have a setter method */ protected boolean isRequiredProperty(PropertyDescriptor propertyDescriptor) { Method setter = propertyDescriptor.getWriteMethod(); return (setter != null && AnnotationUtils.getAnnotation(setter, getRequiredAnnotationType()) != null); }
@Test public void testWithAllRequiredPropertiesSpecified() { DefaultListableBeanFactory factory = new DefaultListableBeanFactory(); BeanDefinition beanDef = BeanDefinitionBuilder .genericBeanDefinition(RequiredTestBean.class) .addPropertyValue("age", "24") .addPropertyValue("favouriteColour", "Blue") .addPropertyValue("jobTitle", "Grand Poobah") .getBeanDefinition(); factory.registerBeanDefinition("testBean", beanDef); factory.addBeanPostProcessor(new RequiredAnnotationBeanPostProcessor()); factory.preInstantiateSingletons(); RequiredTestBean bean = (RequiredTestBean) factory.getBean("testBean"); assertEquals(24, bean.getAge()); assertEquals("Blue", bean.getFavouriteColour()); }
@Override public PropertyValues postProcessPropertyValues( PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException { if (!this.validatedBeanNames.contains(beanName)) { if (!shouldSkip(this.beanFactory, beanName)) { List<String> invalidProperties = new ArrayList<String>(); for (PropertyDescriptor pd : pds) { if (isRequiredProperty(pd) && !pvs.contains(pd.getName())) { invalidProperties.add(pd.getName()); } } if (!invalidProperties.isEmpty()) { throw new BeanInitializationException(buildExceptionMessage(invalidProperties, beanName)); } } this.validatedBeanNames.add(beanName); } return pvs; }
/** * Is the supplied property required to have a value (that is, to be dependency-injected)? * <p>This implementation looks for the existence of a * {@link #setRequiredAnnotationType "required" annotation} * on the supplied {@link PropertyDescriptor property}. * @param propertyDescriptor the target PropertyDescriptor (never {@code null}) * @return {@code true} if the supplied property has been marked as being required; * {@code false} if not, or if the supplied property does not have a setter method */ protected boolean isRequiredProperty(PropertyDescriptor propertyDescriptor) { Method setter = propertyDescriptor.getWriteMethod(); return (setter != null && AnnotationUtils.getAnnotation(setter, getRequiredAnnotationType()) != null); }
@Test public void testWithStaticFactoryMethodAndRequiredPropertiesSpecified() { DefaultListableBeanFactory factory = new DefaultListableBeanFactory(); BeanDefinition beanDef = BeanDefinitionBuilder .genericBeanDefinition(RequiredTestBean.class) .setFactoryMethod("create") .addPropertyValue("age", "24") .addPropertyValue("favouriteColour", "Blue") .addPropertyValue("jobTitle", "Grand Poobah") .getBeanDefinition(); factory.registerBeanDefinition("testBean", beanDef); factory.addBeanPostProcessor(new RequiredAnnotationBeanPostProcessor()); factory.preInstantiateSingletons(); RequiredTestBean bean = (RequiredTestBean) factory.getBean("testBean"); assertEquals(24, bean.getAge()); assertEquals("Blue", bean.getFavouriteColour()); }
@Test public void testExtendedResourceInjectionWithAtRequired() { bf.addBeanPostProcessor(new RequiredAnnotationBeanPostProcessor()); RootBeanDefinition bd = new RootBeanDefinition(TypedExtendedResourceInjectionBean.class); bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); bf.registerBeanDefinition("annotatedBean", bd); TestBean tb = new TestBean(); bf.registerSingleton("testBean", tb); NestedTestBean ntb = new NestedTestBean(); bf.registerSingleton("nestedTestBean", ntb); TypedExtendedResourceInjectionBean bean = (TypedExtendedResourceInjectionBean) bf.getBean("annotatedBean"); assertSame(tb, bean.getTestBean()); assertSame(tb, bean.getTestBean2()); assertSame(tb, bean.getTestBean3()); assertSame(tb, bean.getTestBean4()); assertSame(ntb, bean.getNestedTestBean()); assertSame(bf, bean.getBeanFactory()); }
@Test public void testExtendedResourceInjectionWithAtRequired() { bf.addBeanPostProcessor(new RequiredAnnotationBeanPostProcessor()); RootBeanDefinition bd = new RootBeanDefinition(TypedExtendedResourceInjectionBean.class); bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); bf.registerBeanDefinition("annotatedBean", bd); TestBean tb = new TestBean(); bf.registerSingleton("testBean", tb); NestedTestBean ntb = new NestedTestBean(); bf.registerSingleton("nestedTestBean", ntb); TypedExtendedResourceInjectionBean bean = (TypedExtendedResourceInjectionBean) bf.getBean("annotatedBean"); assertSame(tb, bean.getTestBean()); assertSame(tb, bean.getTestBean2()); assertSame(tb, bean.getTestBean3()); assertSame(tb, bean.getTestBean4()); assertSame(ntb, bean.getNestedTestBean()); assertSame(bf, bean.getBeanFactory()); }