Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Car.

java

/**
*
*/
package com.vehicles;

/**
* @author Rajani Ramsagar
*
*/
public class Car {

private String name;

private int cost;

/**
* @return the name
*/
public String getName() {
return name;
}

/**
* @param name
* the name to set
*/
public void setName(String name) {
this.name = name;
}

/**
* @return the cost
*/
public int getCost() {
return cost;
}

/**
* @param cost
* the cost to set
*/
public void setCost(int cost) {
this.cost = cost;
}

carDemo-beans.xml

<?xml version="1.0" encoding="UTF-8"?>


<beans xmlns="http://www.springframework.org/schema/beans"
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">
</beans>

carDemo-beans.xml

<?xml version="1.0" encoding="UTF-8"?>


<beans xmlns="http://www.springframework.org/schema/beans"
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">

<bean id="car" class="com.vehicles.Car">


<property name="name" value="Honda"></property>
<property name="cost">
<value>2500000</value>
</property>
</bean>
</beans>

ShowRoom.java

/**
*
*/
package com.vehicles;

/**
* @author Rajani Ramsagar
*
*/
public class ShowRoom {

private Car mediumCars;

private Car bigCars;

/**
* @return the mediumCars
*/
public Car getMediumCars() {
return mediumCars;
}

/**
* @param mediumCars
* the mediumCars to set
*/
public void setMediumCars(Car mediumCars) {
this.mediumCars = mediumCars;
}

/**
* @return the bigCars
*/
public Car getBigCars() {
return bigCars;
}

/**
* @param bigCars
* the bigCars to set
*/
public void setBigCars(Car bigCars) {
this.bigCars = bigCars;
}

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

<bean id="car" class="com.vehicles.Car">


<property name="name" value="Honda"></property>
<property name="cost">
<value>2500000</value>
</property>
</bean>

<bean id="showRoom" class="com.vehicles.ShowRoom">


<property name="bigCars">
<ref bean="car" />
</property>
<property name="mediumCars">
<bean class="com.vehicles.Car">
<property name="name">
<value>Hyundai</value>
</property>
<property name="cost">
<value>500000</value>
</property>
</bean>
</property>
</bean>
</beans>

SpringDemoApp.java

/**
*
*/
package com.vehicles;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;

/**
* @author Rajani Ramsagar
*
*/
public class SpringDemoApp {

private static final Logger logger =


Logger.getLogger(SpringDemoApp.class);

/**
* @param args
*/
public static void main(String[] args) {
BeanFactory beanFactory = new XmlBeanFactory(new
ClassPathResource(
"carDemo-beans.xml"));

ShowRoom showRoom = (ShowRoom) beanFactory.getBean("showRoom");

logger.info("Big Cars: " + showRoom.getBigCars().getName());


logger.info("Medium Cars: " +
showRoom.getMediumCars().getName());

log4j.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration PUBLIC

"http://logging.apache.org/log4j/docs/api/org/apache/log4j/xml/log4j.dt
d"

"http://logging.apache.org/log4j/docs/api/org/apache/log4j/xml/log4j.dt
d">

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

<appender name="console" class="org.apache.log4j.ConsoleAppender">


<layout class="org.apache.log4j.SimpleLayout" />
</appender>

<root>
<level value="info" />
<appender-ref ref="console" />
</root>

</log4j:configuration>

You might also like