Social Media

Configuring Spring

Spring can be configured through 2 approaches –

  • XML
  • Annotations

XML Configuration

Some examples of configuring beans through xml –

[sourcecode lang=”xml”] <beans
default-init-method="init"
default-destroy-method="destroy">

<!– Basic coniguration –>
<bean id="simpleBean" class="MyClass" />

<!– Passing arguments –>

<!– Constructor – or objects using ref –>
<bean id="constructorArgBean" class="MyClass">
<constructor-arg value="constructorArgument"/>
</bean>

<!– Property – or objects using ref –>
<bean id="propertyArgBean" class="MyClass">
<property name="propertyAsArg" value="propertyAsArgExample"/>
<property name="propertyAsRef" value="propertyAsRefExample"/>
<property name="propertyAsNull"><null/></property>
</bean>

<!– Constructor and Property –>
<bean id="constructorAndPropertyArgBean" class="MyClass">
<constructor-arg value="constructorArgument"/>
<property name="propertyAsArg" value="propertyAsArgExample"/>
</bean>

<!– Factory Method –>
<bean id="factoryMethodExample" class="MyClass" factory-method="getInstance" />

<!– Property Namespace–>
<bean id="propertyNameSpaceExample" class="MyClass" p:varExample="varExample" p:refExample="refExample" />

<!– Initialising/Destroying Methods – Can be defaulted in xml header(default-init-method, default-destroy-method) –>
<bean id="initDestroyExample" class="MyClass" init-method="init" destroy-method="destroy" />

<!– Bean Scope – See Below – default – singleton –>
<bean id="prototypeExample" class="MyClass" scope="prototype"/>

<!– Autowiring – See Below –>
<bean id="autowiringExample" class="MyClass" autowire="byName" />

</beans>
[/sourcecode]

Bean Scoping

[sourcecode lang=”xml”] <beans>

<!– Bean Scope Example – default – singleton –>
<bean id="prototypeExample" class="MyClass" scope="prototype"/>

</beans>
[/sourcecode]

  • singleton – Single instance per Spring container (default)
  • prototype – Bean instantiated everytime requested
  • request – Bean definition to HTTP request. Web-capable Spring Context (Spring MVC)
  • session – Bean definition to HTTP session. Web-capable Spring Context (Spring MVC)
  • global-session – Bean definition to global HTTP session. Only valid in portlet context
  • no – no autowiring

Autowiring

[sourcecode lang=”xml”] <beans>

<!– General Syntax –>
<bean id="generalSyntax" class="MyClass" autowire="options" />

<!– Autowiring –>
<bean id="autowiringExample" class="MyClass" autowire="byName" />

</beans>
[/sourcecode]

  • none – default
  • byName – matches on name/id
[sourcecode lang=”xml”] <bean id="propertyArgBean" class="MyClass">
<property name="propertyAsRef" ref="propertyAsRefExample"/>
</bean>

<bean id="propertyAsRef" class="PropertyAsRefClass" />

<!– As match by type – automatically matches to propertyAsRef –>
<bean id="propertyArgBean" class="MyClass" autowire="byName" />
[/sourcecode]

  • byType – matches on type
  • constructor – similar to byType
  • autodetect – “Best-fit Autowiring” – constructor first, then byType

Annotations

Enabled by –

[sourcecode lang=”xml”] <beans>

<!– Enables annotation-based config – only if component-scan not used –>
<context:annotation-config/>

<!– Scan packages for config – can use include-filters, exclude-filters –>
<context:component-scan base-package="com.mypackage" />

</beans>
[/sourcecode]

@Autowired

Candidate for autowiring. Can be applied to –

[sourcecode lang=”java”]

// Constructors
@Autowired
public MyClass(MyProperty myProperty) {
super();
this.myProperty = myProperty;
}

// Property
@Autowired
private MyProperty myProperty;

// Setter Methods
@Autowired
public void setMyProperty(MyProperty myProperty) {
this.myProperty = myProperty;
}

[/sourcecode]

@Qualifier

Provides clarification over which dependency to use –

[sourcecode lang=”java”] @Autowired
@Qualifier("myQualifier")
private MyClass myInstance;
[/sourcecode]

Custom Qualifiers

[sourcecode lang=”java”] import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.beans.factory.annotation.Qualifier;

@Target({ElementType.FIELD,ElementType.PARAMETER,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Qualifier
public @interface MyQualifier {
}
[/sourcecode]

This can now be used to qualify a class –

[sourcecode lang=”java”] @Autowired
@MyQualifier
private MyClass myInstance;
[/sourcecode]

Useful links

http://refcardz.dzone.com/refcardz/spring-annotations

http://refcardz.dzone.com/refcardz/spring-configuration

About the Author Martin Farrell

My name is Martin Farrell. I have almost 20 years Java experience. I specialize inthe Spring Framework and JEE. I’ve consulted to a range of businesses, and have provide Java and Spring mentoring and training. You can learn more at About or on my consultancy website Glendevon Software

follow me on:

Leave a Comment: