You are on page 1of 3

Uttara InfoSolutions

www.uttarainfo.com

Spring Practicals 1
Please do the following:

First unzip the zip given to you

1) In Eclipse, create a Java Project with name FirstApp


a) Right click and create folder -> lib.
copy paste jar files from PerformerIdolApp->lib folder
b) Right click on project->build path->libraries->add jars-> select lib folder jars to add to
build path

1) Create a Car class with name and int bhp as


instance variables (package com.uttara.test).Add
a dummy drive() with SOP.
Have a no-arg constructor and a param constr that
accepts name and bhp as parameter. Generate
setters&getters as well. Override equals(),hashCode(),toString()
2) Right click on project -> file->new->Spring Bean configuration file -> name it spring.xml
3) Configure in beans element this:

<bean id="nano" class="com.uttara.test.Car"/>

4) Create a TestCars class with main().


Code this:
ClassPathXmlApplicationContext ctx =
new
ClassPathXmlApplicationContext("spring.xml");
Car c1 = (Car) ctx.getBean("nano");
c1.drive();

Run this and test. Invoke ctx.getBean("nano") multiple times and verify if different obj or
same obj ref are getting injected. Why? How to change this? Put SOPs before creating
context object and after. Understand what is happening when you start the Spring
Container.

5) Create a constructor-arg sub element in bean to inject a name and bhp as values.
<bean id="nano" class="com.uttara.test.Car">
<constructor-arg value="nano"/>
<constructor-arg value="40"/>
</bean>
Verify if the car object has the injected state or not.


6) Using property sub element, inject properties into another car object
7) Create Engine interface and TruckEngine and NanoEngine classes that implement this.
8) Add an instance variable in Car of type Engine engine; Ask SpC to inject NanoEngine
into one car and TruckEngine into another.
9) Try out other examples by looking at the demoed ex code.

Turning on Annotations Steps for the prior example:

1) In spring.xml-> add context namespace


2) remove all bean configurations
3) turn on component scanning using <context:component-scan base-
package="com.uttara.spring”/>

Your spring xml should look like this now (copy paste this and it should contain only this
content):

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
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-4.0.xsd">

<context:component-scan base-package="com.uttara.test"></context:component-scan>

</beans>

4) Use @Component("<whatever id>") in Car and Engine implementation classes. Use


@AutoWired on setter methods for property injection. Use @Qualifier("<id>”) to choose
which bean to be injected based on id filtering.
Use @Value next to your fields to inject values during object construction.

Ex:
@Component("myCar")
public class Car {

@Value("MyCar")
String name;
@Value("150")
int bhp;

Engine eng;
@Autowired
@Qualifier("eng")
public void setEng(Engine eng) {
this.eng = eng;
System.out.println("inside setEng() "+eng);
}


}

5) Run the same tester class that you had coded earlier to test dependency injection with
auto wiring using annotation configurations!

6) Create a class Song. A song has a name (String) and lyrics (String). A song can be
played. When you play, it prints out its lyrics to monitor. Create the class with 2 instance
variables with 2 setter / getter methods (setName(String str), setLyrics(String n), getName(),
getLyrics(). Make sure validate for null and empty string). Cars can be started, driven,
reversed or stopped. You have to start the car to drive/reverse/stop it. When car is being
driven / reversed, the fuel reduces. Once the car has no fuel, the car stops. Every car has a
name. Create a Joke class. A Joke has a text and has print functionality (text instance
variable and print()). When you invoke print() on a Joke object, the text will be printed to the
monitor. Mark the text variable private, expose setter/getter and have a parameterised
constructor to pass in the text during Joke object creation time. Keep a track of Joke objects
that are created. How will you create a single copy count variable? A person has a name,
Car, Dog and a favourite Song (reuse classes Car, Dog and Song from earlier problems).
When you ask a Person to commute and give him a destination (String parameter), then he
will start the car, drive the car and stop the car and print that he has reached the destination.
When you ask the person to sing, he will sing (print) his favourite song with lyrics. When you
ask a person to take a walk, he will take his dog for a walk and the dog will bark. Person has
the ability to eat Food. Food has name and price. Food must be given to Person when you
invoke eat(). When a person is asked to eat, he will specify that he is eating food with name
and say out his name as well. Person also has a generatePrime() behaviour. When you give
him a number as input, then he will generate all prime numbers until that number and print
to monitor. Inject all dependancies using Spring.

You might also like