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