in Spring

Spring wiring with XML

Introduction

In this post I am going to show you how to wire beans using xml. Beans that we are going to create are Knight and Weapon. Here are interfaces for both:

Here are the implementations.

Component scan

Previously I have wrote about autowiring and component scanning and this is an awesome way to wire the beans. Here is the xml required to enable component scan.

We have enabled component scan using context:component-scan tag, this tells spring to scan package rs.pscode.model for beans. Problem is that this will not work until we add annotation @Component or @Named to MajesticKnight and Sword classes.

XML Wiring

We can accomplish bean wiring using xml only without using @Component tag. Here is the xml example:

Usually apps have multiple beans, to create a nice example I am going to create Weapon bean and wire correctly using XML.

There is one thing that is wrong in the upper listing, we did not put the sword into knights hands.

Dependency injection

We need to inject weapon into knight bean instance. To do this we have multiple choices

  1. Inject it as a property
  2. Inject it as a contructor arg

To inject weapon into knight as a property is done using the <property> tag with attributes name and ref. Name is the name of the property in Knight bean and ref is the id of the bean that is supposed to be used, in this case is weapon.

To inject weapon as a contructor argument can be done in multiple ways we can use <contructor-arg> like this. What we need to add is contructor and few lines of XML. Spring will workout the argument mapping by itself.

Mapping does not work if we have two argument of same type, then we need to add additional information. Here we have several options:

  • index, is index of the argument starting from 0
  • name
  • type

Injecting values

MajesticKnight has a name that is hardcoded, so lets change this. If we want to name the knight “Awesome knight” we will just set value argument like this. Note: you must add contructor argument 🙂

or in case of property injection we will

 

Injecting lists, sets…

If you have ever played any game you did notice that every time your hero makes a kill he usually shouts out something. Lets give our knight list of shoutWords 🙂

additionally spring offers other structures like set, map … lets see this in action

Here is an example to inject java.util.Map. What is a knight without the pants to put all of the magic items.

We can inject list of beans instead of literals as well. Here is a simple example of how to do that.

P namespace

P namespace is used to inject value of the property. Lets inject name of the knight and his weapon using p namespace.

Here is how p arribute is formed p:{name of the property}[-ref], ref is optional in case you are referencing other bean.

Note: Add p namespace in root beans  tag using

xmlns:p="http://www.springframework.org/schema/p"

C namespace

C namespace is used for contructor injection, lets inject knightName and weapon using C namespace

Note: add c namespace in root beans tag using

xmlns:c="http://www.springframework.org/schema/c"

 

Spring is awesome, right ?

Write a Comment

Comment

Webmentions

  • Spring Profiles Tutorial – pscode.rs

    […] property knightName to be “Dev Knight”, here I have used p namespace to set the […]