|
Wizard provides a way to add the newly created bean definition file in any of the
existing config sets. Config set is a Spring IDE way to logically group the bean definition
files. We don't have any config sets created yet so this box is shown empty we will just
click the Finish button to obtain the basic bean definition template.
The bean definition wizard will create a file and will add the default template for the bean definition
in that file. The topmost <beans> element is already added to the bean definition file. Now
we will have to add our required <bean> definitions to make real use of this file.
<?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">
</beans>
Spring IDE extends the functionality of the XML editor in Eclipse. The file demo-beans.xml can be visualized in the XML
editor and edited using the visual tabular elements.
We can also type elements directly in the Source tab of the editor.
We can add elements through the outline view by right clicking on the elements and choosing
Attribute or Child option to create a new Attribute or new Child element.
One important feature provided by the Spring IDE is code completion in the source editing of bean definition file
. Let us add one bean definition for our Developer POJO class.
<?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>
</beans>
After typing some characters of Developer class name use the CTRL+Spacebar keyboard shortcut
to invoke the code completion feature and Spring IDE provides recognition to the java elements
in the bean definition file, so we get the full class name with package to choose from.
While typing property element in the bean definition you can see how Spring IDE provides
code complete feature for the Java elements, this time for the properties of the POJO.
Add Spring Framework Dependencies
For this project to work we will need Spring Framework dependencies. The JAR files needed in the
classpath of this project to run this project are
spring.jar
This is the main dependency JAR file. The spring framework distribution contains this file in the dist
directory.
commons-logging.jar
Spring framework internally used commons-logging API and hence we will need this file as a dependency for the
spring JAR file. This file is available in the lib/jakarta-commons directory of the spring distribution.
log4j-1.2.14.jar
We will use Log4j for the logging purposes. If we don't include this file Spring will default to Java logging
mechanism through the commons-logging API. With this JAR file the log4j mechanism will be plugged
to the commons-logging API.
Right click the project name in the project explorer view and choose New > Folder option.
Give name "lib" to the folder. This will create a folder named lib in the main project directory.
Add these three JAR files in the project in the lib directory through local file system. Then refresh the Eclipse
project by choosing refresh option in the context menu of the project. These files will be shown in the project
explorer now. Select all the three files and right click on them. On the context menu choose Build path >
Add to build path option.
This will show all the libraries in the referenced Libraries node of the project explorer view and
the lib folder node will be shown empty. Don't worry Eclipse have not deleted the files they are just being
shown in a different node to indicate they are in build path now.
Viewing Bean Definitions in Spring Explorer
Spring IDE has provided a separate Spring Explorer to show only spring related artifacts. To see the Spring Explorer
right click on the demo-beans.xml bean definition file and choose the Spring Explorer option from the context
menu.
By double clicking on the nodes of the Spring Explorer view you can open the corresponding Java Bean file or the
bean definition file.
Spring Explorer opens in the bottom portion of the Eclipse Workbench where the console and problems views are shown.
Let us shift the Spring Explorer to accompany the Project Explorer and it will look like a Spring project Explorer now.
|