Book Image

Mockito for Spring

By : Sujoy Acharya
Book Image

Mockito for Spring

By: Sujoy Acharya

Overview of this book

Table of Contents (12 chapters)
Mockito for Spring
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Working with autowiring and annotations


The Spring container can autowire dependencies between the collaborating beans without using the <constructor-arg> and <property> elements that simplify the application context XML configuration.

The following autowiring modes can be used to instruct a Spring container to use autowiring for dependency injection:

  • no: By default, the settings is no. This means no autowiring.

  • byName: The container tries to match and wire bean properties with the beans defined by the same name in the configuration file.

  • byType: The container tries to match a property if its type matches with exactly one of the bean names in the configuration file. If more than one such bean exists, an exception is thrown.

  • constructor: This is similar to type but looks at the constructor type matching. If more than one bean of the constructor argument type is found in the container, an exception is thrown.

  • default: This tries to wire using autowire by constructor; if it does not work, then it tries autowire by byType.

Let's modify our HelloWorld example and try wiring by name:

    <bean name="message" class="java.lang.String">
       <constructor-arg value="auto wired" />
    </bean>
    
   <bean id="helloWorld" class="com.packt.lifecycle.HelloWorld" autowire="byName">
    </bean>

It will print auto wired.

Spring provides annotations to wire collaborators. The following are the annotations:

  • @Required: This annotation applies to the bean property setter method

  • @Autowired: This can be applied to bean property setter methods, constructor, and properties

  • @Qualifier: This annotation along with @Autowired can be used to wire a bean with the qualifier name

To enable autowiring through an annotation, the application context needs to be configured to indicate the annotation. Add the following entry to the application context:

<context:annotation-config/>

Modify the application context to enable an annotation:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    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.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<context:annotation-config/> 
   <bean name="message" id="message" class="java.lang.String">
       <constructor-arg value="auto wired" />
    </bean>
    
    <bean id="helloWorld" class="com.packt.lifecycle.HelloWorld">
    </bean>
       
</beans>

Modify the HelloWorld class to annotate the setter method (setMessage) or the private message property with @Autowired:

public class HelloWorld implements ApplicationContextAware,BeanNameAware, InitializingBean,
    BeanFactoryAware,BeanPostProcessor,  DisposableBean {

  private String message;

  public String getMessage() {
    return message;
  }
  
  @Autowired
  public void setMessage(String message) {
    this.message = message;
  }
     //code omitted for brevity
}

Rerun the application; you will see the auto wired message.