-- THIS POLL HAS BEEN CLOSED. FUTURE POLLS WILL TAKE PLACE ON WWW.SPRINGFRAMEWORK.ORG --
To get a feel for what parts of the Spring Framework you're using, from now on, we will be hosting a series of polls on the forum. Each poll will not only ask you a very simple (multiple choice) question, but also give you some insight into the techniques the poll is about. This is the first poll. Future polls can be found through the central polls page. If you have a good idea for a poll, just send me a private message. Discussions about the poll itself can be done in the comments of course.
Last but not least, we very much value your feedback and will be using all of it throughout the further development of the Spring Framework. So we appreciate it a lot if you could take the time to fill out this poll!
thanks!
Alef
More information for poll - how do you verify if dependencies are set?
Using the dependency-check attribute in Spring XML
From the beginning, Spring has features a dependency checking mechanism you could enable in the XML format. The following example will trigger an Exception upon loading the application context:
The dependency checking attribute can be set for objects, simple properties or both. The default for Spring is to not enable dependency checking. More information on the dependency checking mechanism can be found in chapter 3 of the reference manual. Dependency checking works by inspecting the class that is configured. For all setters, the configuration is inspected. If a certain property does not have a matching configuration in XML, an Exception is thrown and the loading of the ApplicationContext does not proceed.Code:package com.interface21; public class Service { private Helper helper; public void setHelper(Helper helper) { this.helper = helper; } } package com.interface21; public class Service { private Helper helper; public void setHelper(Helper helper) { this.helper = helper; } } <bean class="com.interface21.Service" dependency-check="objects"/>
Using the @Required annotation
In Spring we introduced a way to check dependencies using an annotation. By simple adding the @Required annotation to a setter in your class. The difference with the attribute (see above) is that you don't mention it in the XML file(s), but in your Java classes. Enabling dependency checking using the @Required annotation, you have to install a BeanPostProcessor. So the configuration below for how to do this. By default, Spring uses the [font-courier]org.springframework.beans.factory.annnotation.Requ ired[/font] annotation. If you don't want to tie your code to Spring (which in general is a good idea anyway, you can use your own annotation, by configuring the RequiredAnnotationBeanPostProcessor. More information on the @Required annotation can be found in the reference manual.
Using InitializingBean or init-method (and possibly Spring's Assert class)Code:package com.interface21; import org.springframework.beans.factory.annotation.Required; public class Service implements InitializingBean { private Helper helper; @Required public void setHelper(Helper helper) { this.helper = helper; } } <bean class="com.interface21.Service"/> <bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/> <!-- or, if you want to use your own annotation --> <bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"> <property name="requiredAnnotationType" value="com.mycompany.MyAnnotation"/> </bean>
Another way of checking dependencies is to use either Spring's InitializingBean interface or a custom init-method. Both features are thoroughly explained in the reference manual. Using either Spring's Assert class or the Java assert keyword, you can make sure arguments are set correctly. The example below shows using the InitializingBean.
Using constructor injectionCode:package com.interface21; import org.springframework.beans.factory.InitializingBean; import org.springframework.util.Assert; public class Service implements InitializingBean { private Helper helper; public void afterPropertiesSet() throws Exception { Assert.notNull(helper, "Helper should not be null"); } public void setHelper(Helper helper) { this.helper = helper; } }
Spring features two types of dependency injection: setter injection and constructor injection. Both have their respective differences, but one pro of constructor injection is that objects can never be instantiated in an invalid state. So one (also very clean) way of ensuring required dependencies are set is using constructor injection such as in the example below. Spring by the way has always featured constructor injection (from the first final release that is). Because Spring first implemented setter injection and got to doing constructor injection only until later in the development process, lots of examples from the early beginning are using setter injection and it is sometimes being regarded as the Spring-recommended way of doing DI. This is definitely not the case. Many of us are using constructor injection and prefer it over setter injection for required properties. More information on constructor and setter injection can be found in chapter 3 of the reference manual.
Code:package com.interface21; public class Service { private Helper helper; public Service(Helper helper) { this.helper = helper; } }



