Spring Development in MyEclipse
1. What is Spring?
Introducing Spring is harder than most frameworks because it’s not a single-purpose technology. Spring can be thought of as a huge framework of best practices for almost every area of Java software development. Everything from Plain-Old-Java-Object (POJO) development, to web application development, to enterprise application development, to persistence layer management and aspect oriented programming (AOP). Spring supports it all and does so with some of the most well designed and heavily tested code in the Java industry.
Because of its size, we need to focus this tutorial on the simplest application of Spring which is POJO development utilizing dependency injection. To clarify, dependency injection is a mechanism by which Spring handles creation and initializaton of the proper type of child object for a parent object at the moment the parent object needs the reference to the child. An example of where this is useful in the context of enterprise Java programming is for your web application to instantly have access to your data-access layer in order to read or write an object from the database. This is a classic example of allowing Spring to inject the reference to the data-access layer into a POJO in the web application in order to load or save an object.
To immediately demystify this process, Spring handles this by mapping out object reference relationships in XML configuration files in Spring 1.2, and with Spring 2.0, this is handled with Java 5 annotations.
Using this declarative method, Spring developers can actually soft-wire their application portions together using these annotations or XML configuration files such that when the application runs, Spring creates and instantiates all the object relationships “on demand”. The advantage to having everything soft-wired is that portions of an application can quickly and easily be swapped out for alternative implementations (e.g. testing implementations) by simply changing around the annotations or XML configuration information and rerunning the application. There isn’t even a need to recompile the application in some cases. This can be a huge boon to developers working on large applications that require constant testing or when delivering a larger application in iterations. Spring provides a very natural way of thinking about these problems, encouraging you to maintain your application in a modular architecture that supports this plugging and unplugging capability.
2. Spring Project Configuration
To add the Spring facet to any Java project, right-click the project in the Explorer and select Configure Facets>Install Spring Facet.
Adding Spring facets
You can also add multiple facets to a project simultaneously by opening the Project Facets property of the project.
Adding facet via project properties
MyEclipse supports Spring 1.2, Spring 2, Spring 2.5, Spring 3.0, Spring 3.1, and Spring 4.1. Corresponding libraries are categorized and bundled with MyEclipse. Spring Java Configuration and Spring Web Flow support is also included.
Selecting Spring libraries
Use the Spring project properties page to add and group related bean configuration files. Open project properties by right-clicking the project, and selecting Properties>Project Facets>Spring>Beans.
Spring Beans project properties page
The Spring Bean Definition File wizard allows you to easily create configuration files with required namespaces. An applicationContext.xml definition file is generated for you by default when you add the Spring facet to a project. Or, if you create your own, you can specify an existing bean definition file when adding the facet.
Creating a Spring bean definition file
MyEclipse includes a set of advanced bean wizards that you can access by either right-clicking the Outline view or the applicationContext.xml source tab. Using the bean definition editor, you can access the New Bean wizard as well as edit bean details for each namespace selected on the Namespaces tab.
Spring Bean menu and bean definition editor
Spring bean wizards validate user input and provide content assist (Ctrl +Space) where possible.
Spring bean details
The Data Source wizard allows you to easily create a Spring data source bean corresponding to an existing MyEclipse driver. Add a new data source by right-clicking anywhere in the applicationContext.xml file in the source view editor, and selecting Spring Tools>New DataSource. You can also access this menu by right-clicking anywhere in the Outline view.
Creating a Spring datasource
The Hibernate SessionFactory wizard is a specialized wizard for Spring’s LocalSessionFactoryBean.
4. Managing Multiple Beans & Projects
In addition to using the bean configuration file editor to manage Spring bean, you can also use the Outline view.
You can also change the outline to a Spring-style outline using the Outline view menu.
The Spring Explorer is a filtered tree view that displays all Spring projects in your workspace along with any beans, config sets and Spring Web Flow elements it may contain.
Spring Explorer view
This view is namespace aware and can be highly filtered and customized. To customize the view, click the drop down arrow on the view toolbar and select Customize View.
Customizing the Filters and Content in the Spring Explorer view
The Spring Explorer view also specially annotates beans defined via Spring Java Configuration.
Spring Explorer Java Config support
All Spring projects contain a Spring Elements child when seen in the Project Explorer view using the Spring perspective. This item can be expanded to show beans, config sets and web flow definitions just like the Spring Explorer view described above. The ability to see a list of all beans that reference a particular Java class is a key feature.
Project Explorer with Spring Elements node
Like the Spring Explorer, the Spring elements shown in this view can be filtered and customized. You can also use a special Spring working set to cut down the clutter in the Project Explorer.
Spring working sets
The Graph viewer presents a graphical view of the relationships between various beans in a given configuration file. Access the graphical view by clicking the Beans Graph in the applicationContext.xml file editor.
Spring graph editor
Use the Open Bean wizard (ALT+Shift+B) or double-click a bean in the graph to quickly navigate to any bean.
Navigating to a bean
Perform various bean searches and Pointcut Matches in your workspace from the Search menu on the main toolbar.
Bean pointcut matches search
5. Spring Config Editor
The Spring Config editor is the default editor for Spring bean configuration files.
Spring Config Editor
The Source view provides content assist for class, bean Id and property attributes in addition to standard XML support.
Class content assist
Bean content assist
Property content assist
The Spring Configuration editor is also namespace aware and provides intelligent content assist and validation for defined elements.
AOP namespace content assist
p namespace content assist
Use the visual editor tabs to manage bean entities. Edit namespaces and bean details for each namespace selected in the configuration. Right-click elements to add child elements, and configure elements using the Element Details section.
Using the visual Spring Bean Config editor
To learn more details about the Spring Bean Config editor, scroll to the bottom of the Element Details section to access additional documentation.
Accessing additional editor documentation
Renaming a Java class automatically makes changes in bean definitions referencing that class. Java classes can also be renamed directly from the bean configuration file.
Renaming a bean’s Java class
Bean Ids and properties can also be renamed. Renaming a bean property makes changes in the corresponding Java class. Rename elements in the following ways:
- On the Beans tab of applicationContext.xml, select the element and edit the element details.
- In the Source view of applicationContext.xml, right-click the element, and select Refactor>Rename Bean Element from the menu.
- In the Source view of applicationContext.xml, place the cursor in the element, and press ALT+SHIFT+R.
Renaming a bean Id
Renaming a bean property
In addition to standard XML validation, the editor also performs Spring-specific validation. These validators can be controlled both at the project and workspace level. Validation errors are noted in the config file Source view, and detailed in the Problems view.
6. AOP Support
The Beans Cross References view shows you all beans being advised as well as those that advise other beans.
Beans Cross References view
The Spring Configuration editor as well as the Java editor displays special markers for methods and classes affected by your project’s AOP configuration.
The Spring AOP Event Trace view gives you an idea of what is going on under the hood as Spring IDE’s internal AOP model is being built.
AOP Event trace view
AOP support requires the AOP Reference Model Builder, which can be enabled/disabled at both the project and global level. If you do not use AOP in your projects, you can turn it off globally.
7. Web Flow Support
MyEclipse also adds Spring Web Flow support and includes multiple versions of Spring Web Flow libraries. Include Spring Web Flow when you install the Spring facet to your project. Or, you can include it at a later time in the project’s Spring properties.
Including Spring Webflow when adding Spring facet
Including web flow
Web flow container
Defining a Web Flow definition file
Use the visual editor to easily create states, actions, and transitions usIng the right-click menu. Use the Element Details section to add element information. Use the Flow-graph tab to visually design your web flow.
Opening the Web Flow editor
Web Flow editor
The Source view provides web flow-specific content assist support.
Editor Source view
Validation for web flow definition files is customizable.
Web Flow validation customization