JavaBeat
calling cards | international calling cards | phone card
Search JavaBeat

JAVABEAT
home
articles
tips
QnA
Books
forums
ARTICLE TOPICS
All Articles
Java 5.0
Java 6.0
EJB 3.0
JCA
Struts
JSF
Spring
Groovy
JBoss Seam
Hibernate
Eclipse
JavaFx
Google Guice
J2ME
GWT
WebServices
AJAX
ARCHIVE
2007 | 12 11 10 09 08 07 06 05 04 03
2008 | 07 06 05 04 03 02 01
CERTIFICATION KITS
350 SCJP 1.5 Mock Exams
400 SCJP 1.6 Mock Exams
300 SCWCD 5.0 Mock Exams
300 SCBCD 5.0 Mock Exams
Enter email address:

Latest JavaBeat Articles Delivered by FeedBurner
OUR NETWORK
javabeat
planetoss
Favorites
Java Jobs eCommerce software Get Ubuntu 8.04 Get FireFox 3.0  

Pages : 1 2 3 4 5 6

Introduction to Spring IDE 2.0

Author : TusharJoshi
Date : Fri Sep 7th, 2007
Topic : spring  
Enter email address:

Latest JavaBeat Articles Delivered

Bean Cross Reference View

You can see the bean definitions in a cross referenced way through the beans cross reference view. To open this view choose the Windows Menu, Show View option and selecting the Beans Cross Reference view from the Spring Node in the Show View Dialog Box. The beans cross reference view will also be shown in the bottom portion where console view is shown. Let us keep this view accompanying to the outline view as shown in the image below.

Bean Cross Reference View will not show anything unless you use the "link open editors with contents in Navigator" button available on the view top right portion.

Graphical representation of Spring Beans

Spring IDE provides a Graphical Visualizer for the bean definitions. Right click the demo-beans.xml file shown in Spring Explorer or the Bean Cross Reference view and close "Open Graph" option from the context menu.

Graph view for the beans definition file will be shown in the editor pane of the Eclipse Workbench. This graphical representation provides a picture of the complete bean definition file.

Let us add one more POJO to our project. This time we will write a class which references our Developer class.


/**
 * 
 */
package com.company.demo;

/**
 * @author Tushar Joshi
 * 
 */
public class SoftwareCompany {

	private Developer leadDeveloper;
	private Developer developer;

	public Developer getLeadDeveloper() {
		return leadDeveloper;
	}

	public void setLeadDeveloper(Developer leadDeveloper) {
		this.leadDeveloper = leadDeveloper;
	}

	public Developer getDeveloper() {
		return developer;
	}

	public void setDeveloper(Developer developer) {
		this.developer = developer;
	}

}

SoftwareCompany class has two properties of Developer type with names leadDeveloper and developer. We want these properties to be injected from the spring mechanism through the bean definition file.

Our completed bean definition file will be like this


<?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-2.0.xsd">

	<bean id="developer" class="com.company.demo.Developer">
		<property name="name" value="Shekhar"></property>
		<property name="experienceYears">
			<value>5</value>
		</property>
	</bean>

	<bean id="softwareCompany"
		class="com.company.demo.SoftwareCompany">
		<property name="leadDeveloper">
			<ref bean="developer" />
		</property>
		<property name="developer">
			<bean class="com.company.demo.Developer">
				<property name="name">
					<value>Venkat</value>
				</property>
				<property name="experienceYears">
					<value>2</value>
				</property>
			</bean>
		</property>
	</bean>


</beans>

I have used one reference property pointing to the earlier created developer bean and other property with a new developer bean definition in place. When we see the Graph for this bean definition file we will see this graph view.

Let us add a launcher class for these beans and a main method to invoke the spring bean factory mechanism. We will add DemoApp class to the project with a main method.


/**
 * 
 */
package com.company.demo;

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 Tushar Joshi
 * 
 */
public class DemoApp {

	private static final Logger logger = Logger.getLogger(DemoApp.class);

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

		SoftwareCompany company = (SoftwareCompany) beanFactory
				.getBean("softwareCompany");

		logger.info("Lead Developer: " + company.getLeadDeveloper().getName());
		logger.info("Developer     : " + company.getDeveloper().getName());

	}

}

The main method obtains a BeanFactory reference through the XmlBeanFactory concrete class. We pass object of ClassPathResource object created by passing the name of the bean definition file. This way we can keep the beans-demo.xml in the classpath and the spring framework will locate it automatically through the class path searching mechanism.

We will need a minimal log4j.xml file to define the way we want the output of the application to be presented. Let us add a log4j.xml file to the src folder of the project.


<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration PUBLIC
		"http://logging.apache.org/log4j/docs/api/org/apache/log4j/xml/log4j.dtd"
		"http://logging.apache.org/log4j/docs/api/org/apache/log4j/xml/log4j.dtd">


<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>

Note the level of the root element is kept at INFO for demonstration purpose. Running this class as a Java Application will provide output like


INFO - Loading XML bean definitions from class path resource [demo-beans.xml]
INFO - Lead Developer: Shekhar
INFO - Developer     : Venkat

Source Code

Source code of the sample application is kept here. This is a archive file with the folder structure used in Eclipse.

Conclusion

Spring IDE provides features like Spring Explorer, Beans Cross Reference, Graph View and Code completion in the bean XML editor hence making the life of a Spring Framework user easier. It provides intuitive ways to visualize the beans definition file and helps increase the understanding of user about the loaded beans through spring inversion of control mechanism.

Pages : 1 2 3 4 5 6
 

Favorites
AffiliatedAds.com
Buy movies
Access Control
Busby seo challenge contest
Sohbet
Chat
Webmaster Hosting Forum
Java Jobs
MyVideoLib
India News
Internet Advances
Sohbet
chat
Latest QnA
SCJD Tips
When we start a thread by applying start() method on it ,how does it knows that to execute run()method?
About Wrapper class in Java
How to configure weblogic 7.0 in MyEclipse?
Static Block and Static Initializer in Java

JavaBeat Website (2004-2008), India
javabeat | about us | planetoss
Copyright (2004 - 2008), JavaBeat