Spring IOC with XML Configuration - Maven Project

  1. I consider you are aware about maven if not please refer my video or tutorial related to maven from my website.

  2. Below are steps.

    1. Create a maven project

    2. Refer below images step by step to create a project

    3. Select Wizard for Maven Project

      Select Project Name and Location for New Maven Project

      Configure New Maven Project

      Project structure will be like below:Maven Project Structure

      Open pom.xmlOpen pom.xml file in Maven Project

      Add below code in pom.xml

    4. You need to add above code in pom.xml code. I have added here context jar as a dependency. Specialty of maven is it brings all other depended jars in our project. If you want to know in detail how it brings and how you will know which jars will be coming to your project. Go to https://mvnrepository.com website browse our particular jar.

    5. Refer images for ( iii )

download Maven jar from mvnrepository website

See below of this on same webpage, you will see Compile dependencies section

Compile dependencies section in mvnrepository website

After saving pom.xml you will see dependencies are downloading from maven remote repository. To see what has downloaded go to package explorer. Open Maven Dependencies.

Maven Dependencies in Maven Project

We will use src /main/java/ any package folder and resources folder for configurations. If any folder is not there create as new folder. You may not see resources folder in your project so create it. Below image shows how to create a package.

  • src/main/java/<com/javabykiran> / <your java files goes here>
  • src/main/resources/ <your configuration/properties files goes here>

Add New Java Package in Maven Project

Now setup is complete and we are ready to code in eclipse IDE.

Let’s understand a concept:

  1. We have class Student with address, age, and list of mobile nos.

    • Address is Class type

    • Age is Primitive type

    • Mobile numbers is List type

  2. Create getter and setters for all of the above. This we need to do as dependencies are injected through setter methods of every individual variable.

    • Right-click in Editor - Go to Source - Generate getter setter option

    Above classes will look like below:

  3. Reason for setters is:

    • One dependency injection

    • Second is to implement proper encapsulation we do not give access of variables directly, but we give through methods as we can have control over variables. Same concept spring has used.

  4. We need to initialize all above global variables with IOC by using dependency injection concept. Not directly as we do in traditional programming.

  5. To do this we will need configuration file as well. So create this file in resources folder name can be anything. I made it as a spconfig.xml
    Code will look like below.

  • Every tag of this file we will understand in detail.

  • bean: we need to define every class here. Internally spring creates object of this class. Below are important attributes

    • id: unique name

    • class: fully qualified class name

    • scope: we can have this instance created with many scopes.
      Remember only 4 scopes are useful.

      1. Prototype

        • To call constructor every time.

      2. Singleton

        • To create single object of a class this is most preferable

      3. Request

      4. Session

        • This keeps instance right from session created to session destroyed.

    • init-method: we can specify any method here and will get called once bean is instantiated.

    • destroy-method: once bean is destroyed this will get called.

  • After instantiating bean whatever we want to do as a dependency injection we can specify in property tag. In our example we want to initialize 3 variables age, address, and mobile nos. see below explanation.

  • property: specify setter names here and initialize them as per there type. Every type has different concept of implementation. In our case we will see primitives, class type and collection type.

    • value: use this if you want to initialize primitives. Here in industry we will see value from property file like database username, password etc.

    • ref: use this if you are injecting some object. In our case we will have a bean defined for address will be injected.

    • list: for collection like list

      1. value: mention value for collection, you can specify n no of values here.

    • Set: explained later in this chapter

    • Map: explained later in this chapter

    • Properties: explained later in this chapter

After this we will create client from which we will be invoking class of student to create object and to inject values as specified in xml.

In above code,

    Line no 1) create a context and parse complete xml file from top to bottom. All objects will be created and if any errors those will be notified in console. Injections will also applied while reading xml file. First constructors will be called for beans. Then setters will be called which are mentioned in property tag. Application Context is container which holds all objects. Here we have used ClassPathXmlApplicationContext which is an implementation of ApplicationContext interface in spring.
    This line can be replaced with many other ways like BeanFactory etc. in details it will be explained later in this chapter.

    Line no 2) we are getting one of bean from context. This depends on scope whether to create new object again and again or just once. In our case it is singleton. Try making scope as a prototype and write getBean method multiple times you will see every time it will return new address.

    Line no 3) returns address of student class

    Line no 4) address objects as we injected from xml.

    Line no 5) landmark injected in address and address object in student so we are retrieving child object from parent object.

    Output should be as below :

Till now we have seen basic fundamentals which are very important to understand advance concepts of spring. Without understanding this concept we may not able to understand even other modules of spring.

In spring later introduced a concept called as auto wiring which detects automatically which beans to inject in which bean.in our case address in student. We will see in the next chapter about Auto Wiring