Sample-spring-basic » History » Revision 17
Revision 16 (Henning Blohm, 08.11.2013 22:48) → Revision 17/33 (Henning Blohm, 09.11.2013 14:11)
h1. A basic Spring with Z2 modularity sample The sample contained in the repository "z2-samples.spring-basic":http://redmine.z2-environment.net/projects/z2-samples/repository/z2-samples-spring-basic is a clean room example on how to use the Spring integration features described in [[how_to_spring]]. There is no further pre-requisite to running this sample, and you may proceed as described in [[How to run a sample]]. Here's the really fast version: <pre><code class="ruby"> mkdir install cd install git clone -b master http://git.z2-environment.net/z2-base.core git clone -b master http://git.z2-environment.net/z2-samples.spring-basic # on Linux / Mac OS: cd z2-base.core/run/bin ./gui.sh # on Windows: cd z2-base.core\run\bin gui.bat </code></pre> If you want to inspect the code using Eclipse, please create a workspace in install (i.e. *install/workspace*) and import the Git repositories and projects (see also [[Step_3_-_First_steps_with_Z2_on_Git|First steps]]). There are three modules contained in this sample. For the moment only consider the following two: h2. com.zfabrik.samples.spring-basic.services This module has a classpath defined "applicationContext":http://redmine.z2-environment.net/projects/z2-samples/repository/z2-samples-spring-basic/revisions/master/entry/com.zfabrik.samples.spring-basic.services/java/src.impl/META-INF/applicationContext.xml and exposes an annotation defined bean "ComputationServiceImpl":http://redmine.z2-environment.net/projects/z2-samples/repository/z2-samples-spring-basic/revisions/master/entry/com.zfabrik.samples.spring-basic.services/java/src.impl/com/zfabrik/samples/impl/services/ComputationServiceImpl.java bean from it as a service to be consumed from another module: The *computations* bean. The application context enables discovery of Spring beans that are marked by annotations such as @Service, @Component, @Repository: <pre><code class="xml"> <beans ...> <!-- Turn on annotation based config --> <context:annotation-config/> <!-- Turn on auto discovery --> <context:component-scan base-package="com.zfabrik.samples.impl"/> </beans> </code></pre> The *computations" bean implementation implements the interface "IComputationService":https://redmine.z2-environment.net/projects/z2-samples/repository/z2-samples-spring-basic/revisions/master/entry/com.zfabrik.samples.spring-basic.services/java/src.api/com/zfabrik/samples/services/IComputationService.java and is itself not visible to consumers. It's structure is roughly like this: <pre><code class="java"> @Service("computations") public class ComputationServiceImpl implements IComputationService { ... } </code></pre> The *computations* bean is exposed via the Z2 component "com.zfabrik.samples.spring-basic.services/computations":https://redmine.z2-environment.net/projects/z2-samples/repository/z2-samples-spring-basic/revisions/master/entry/com.zfabrik.samples.spring-basic.services/computations.properties: <pre><code class="ruby"> com.zfabrik.component.type=org.springframework.bean # # the context that defines the bean (more than one # bean can be exposed like this) # bean.context=com.zfabrik.samples.spring-basic.services/applicationContext # # the bean name # bean.name=computations </code></pre> Note that the Z2 Bean component names its application context. At runtime this means that an attempt to retrieve the bean will make sure the application context is loaded - and not any earlier. h2. com.zfabrik.samples.spring-basic.frontend This module has a Web application with an application context defined in "WEB-INF/applicationContext":https://redmine.z2-environment.net/projects/z2-samples/repository/z2-samples-spring-basic/revisions/master/entry/com.zfabrik.samples.spring-basic.frontend/web/WebContent/WEB-INF/applicationContext.xml. WEB-INF/applicationContext. It uses Spring AspectJ based annotation driven configuration to inject dependencies into instances of "ControllerServlet":http://redmine.z2-environment.net/projects/z2-samples/repository/z2-samples-spring-basic/revisions/master/entry/com.zfabrik.samples.spring-basic.frontend/java/src.impl/com/zfabrik/samples/impl/frontend/ControllerServlet.java. Note the Java component descriptor that, apart from referencing the service module, holds the minimum declaration to make sure of Spring with AspectJ supported, compile-time-woven annotation based configuration: <pre><code class="ruby"> com.zfabrik.component.type=com.zfabrik.java java.privateReferences=\ com.zfabrik.servletjsp,\ org.springframework.transaction,\ org.springframework.orm,\ org.springframework.web,\ com.zfabrik.springframework,\ com.zfabrik.samples.spring-basic.services java.privateIncludes=\ org.springframework.foundation/aspects java.compile.order=java,spring_aspectj </code></pre> It's "application context":http://redmine.z2-environment.net/projects/z2-samples/repository/z2-samples-spring-basic/revisions/master/entry/com.zfabrik.samples.spring-basic.frontend/web/WebContent/WEB-INF/applicationContext.xml imports the *computations* service from the other module above and enables the use of Spring configuration (note: Unlike above it does not enable discovery of Spring beans): <pre><code class="xml"> <beans ... > <!-- turn on @Configurable support --> <context:spring-configured/> <!-- Turn on annotation based config --> <context:annotation-config/> <!-- application config: Bind the bean at samples.spring.simplemodules.services/computations --> <bean id="computations" class="com.zfabrik.springframework.ComponentFactoryBean"> <property name="componentName" value="com.zfabrik.samples.spring-basic.services/computations"/> <property name="className" value="com.zfabrik.samples.services.IComputationService"/> </bean> </beans> </code></pre> above. h2. Finally Open a browser and navigate to http://localhost:8080/frontend to verify you get this: !frontend.png! h1. An extended Spring with Z2 modularity and some Spring Security sample The third module *com.zfabrik.samples.spring-basic.secured* contained in the sample repository implements a very similar basic frontend to the one above but illustrating in addition: * How to use Spring Security to secure the access to a Web application * How to use Spring Security to secure methods of a bean * How to use Spring Security with Spring AspectJ weaving tbc