martes, 29 de junio de 2010
viernes, 14 de mayo de 2010
Setting up Spring Framework and JDO in Google App Engine
I'm currently playing with Google App Engine, the cloud computing platform from Google. In this platform, you're allowed to use two orm technologies: JDO and JPA.
I've been setting up spring to manage JDO persistence. First step, is adding Spring listener in web.xml:
Then, create an application context file in war/WEB-INF/applicationContext.xml:
Take a look at the first bean, persistenceManagerFactory. This bean acts as your Persistence Manager Factory. To link with GAE's jdo you must set the property persistenceManagerFactoryName with the name of your persistence-manager-factory that is located in src/META-INF/jdoconfig.xml. By default, when you create a GAE's project with eclipse, this name is transactional-optional.
This setup, uses the helper class org.springframework.orm.jdo.JdoTemplate. Is a very common pattern in Spring's orm. JdoTemplate contains very usual methods to work with jdo, methods to persist objects and query methods. To inject this template in your DAOs you must extend org.springframework.orm.jdo.support.JdoDaoSupport in all of them.
There is another way to setup your DAOs. Instead of extend JdoDaoSupport, you can inject directly your Persistence Manager Factory. Take a look at this article for detailed instructions. The problem with this setup is that you lost all methods available in JdoTemplate but, you gain more control over JDO.
I've been setting up spring to manage JDO persistence. First step, is adding Spring listener in web.xml:
Then, create an application context file in war/WEB-INF/applicationContext.xml:
Take a look at the first bean, persistenceManagerFactory. This bean acts as your Persistence Manager Factory. To link with GAE's jdo you must set the property persistenceManagerFactoryName with the name of your persistence-manager-factory that is located in src/META-INF/jdoconfig.xml. By default, when you create a GAE's project with eclipse, this name is transactional-optional.
This setup, uses the helper class org.springframework.orm.jdo.JdoTemplate. Is a very common pattern in Spring's orm. JdoTemplate contains very usual methods to work with jdo, methods to persist objects and query methods. To inject this template in your DAOs you must extend org.springframework.orm.jdo.support.JdoDaoSupport in all of them.
There is another way to setup your DAOs. Instead of extend JdoDaoSupport, you can inject directly your Persistence Manager Factory. Take a look at this article for detailed instructions. The problem with this setup is that you lost all methods available in JdoTemplate but, you gain more control over JDO.
lunes, 17 de agosto de 2009
Using DWR with Springframework (II)
In this chapter, I will explain how to call our dwr remote service from the client. But before, let me tell you a problem with Spring Framework and DWR integration.
Is very usual using aop for transactions with spring. In that case, if our dwr-allowed service is proxied the remote call will fail. To avoid this we only need to add this section of code in the service bean definition:
Now, the DWR call will arrive to the proxied bean. Don't forget that because is a very common issue.
Now, let's go with the dwr client side explanation as I promise in the previous post. The first step is including dwr's javascript files in our jsp file:
This loads DWR base code. Note that /dwr was the url-pattern from the DWR servlet. Engine.js and util.js are created on runtime (these files are inside dwr jar). And... where is located our service? Very easy:
Note that DWR uses the name of the java class as the js filename and for the javascript object. Now, to call our service method:
Very easy, don't you think? Once we can call our service method, we need a callback function to get the data. So let's create one:
DWRUtil.addRows creates all the rows of our table and fills the cells with the received data. Look up the third parameter of addRows. Is an array of functions that tells DWR wich data we want to be showed on each column. In that case, data is a JSON representation of our Article bean. We are telling here to DWR that we want to show id property in the first column, title in the second column and bodytext in the third one.
That's all. I've use for this example a method without parameters. To use with some parameters, is the same procedure, you only have to put callback as last parameter. Suppose this new method from our service class:
So now, to call this service:
That's all. I will publish in the future a third article, when DWR 3 is launched. This version will add support to use annotations in the service instead of xml setup in the application context bean definition file.
Is very usual using aop for transactions with spring. In that case, if our dwr-allowed service is proxied the remote call will fail. To avoid this we only need to add this section of code in the service bean definition:
Now, the DWR call will arrive to the proxied bean. Don't forget that because is a very common issue.
Now, let's go with the dwr client side explanation as I promise in the previous post. The first step is including dwr's javascript files in our jsp file:
This loads DWR base code. Note that /dwr was the url-pattern from the DWR servlet. Engine.js and util.js are created on runtime (these files are inside dwr jar). And... where is located our service? Very easy:
Note that DWR uses the name of the java class as the js filename and for the javascript object. Now, to call our service method:
Very easy, don't you think? Once we can call our service method, we need a callback function to get the data. So let's create one:
DWRUtil.addRows creates all the rows of our table and fills the cells with the received data. Look up the third parameter of addRows. Is an array of functions that tells DWR wich data we want to be showed on each column. In that case, data is a JSON representation of our Article bean. We are telling here to DWR that we want to show id property in the first column, title in the second column and bodytext in the third one.
That's all. I've use for this example a method without parameters. To use with some parameters, is the same procedure, you only have to put callback as last parameter. Suppose this new method from our service class:
So now, to call this service:
That's all. I will publish in the future a third article, when DWR 3 is launched. This version will add support to use annotations in the service instead of xml setup in the application context bean definition file.
lunes, 27 de julio de 2009
Using DWR with Springframework (I)
Recently, I have integrated DWR (direct web remoting) in a spring application. There are some ways to do that, I'm going to explain only one, that uses spring support for namespaces introduced since Spring 2.0.
The first step is adding a new servlet in web.xml and map it to /dwr/*:
Second step is adding the namespace to our applicationContext:
This allows us to use DWR namespace for spring. Now, we are able to add the DWR controller to manage DWR requests:
We have setup dwr with spring, it's as easy as it seems. Supposing we have a service that returns a java.util.List of Article class. Article could be a simple bean like this:
To setup getLastArticles() as a remote ajax method with Spring we only have to annotate the related ArticleService spring bean:
That's all. We are telling DWR to convert our bean Article to JSON. And, only the method specified in dwr:include tag, is callable by ajax. We can call this method from any view in our web application. In the next episode, I will explain how.
Using DWR with Springframework (II)
The first step is adding a new servlet in web.xml and map it to /dwr/*:
Second step is adding the namespace to our applicationContext:
This allows us to use DWR namespace for spring. Now, we are able to add the DWR controller to manage DWR requests:
We have setup dwr with spring, it's as easy as it seems. Supposing we have a service that returns a java.util.List of Article class. Article could be a simple bean like this:
To setup getLastArticles() as a remote ajax method with Spring we only have to annotate the related ArticleService spring bean:
That's all. We are telling DWR to convert our bean Article to JSON. And, only the method specified in dwr:include tag, is callable by ajax. We can call this method from any view in our web application. In the next episode, I will explain how.
Using DWR with Springframework (II)
Deploying your own jar in OC4J 10.1.3
I recently had a big problem working with Oracle Application Server 10.1.3 at work. We are developing a web application based on spring and hibernate. The application was only an Eclipse project, and I thought that it was a mistake because we were mixing in a single project business layer, controller, view, and data acces. So, I thought splitting the project in some little ones, would be a great deal:
Extension-Name: com.yourcompany.yourpackage
Specification-Title:
Specification-Version:
Specification-Vendor:
Implementation-Version:
Implementation-Vendor-Id: com.yourcompany.yourpackage
Implementation-Vendor:
Implementation-Title:
Obviously, you have to fill-in the file with the appropiate values. The second step is including our manifest.mf file in maven's build:
Now, the next time you run mvn package your manifest.mf will be included in your jar and oc4j classloader will load your own jars.
- Model: a project containing domain code, in our case, hibernate entities.
- DAO: Data acces objects.
- DTO: Data transfer objects.
- Util: util classes.
- Service: Business code.
- Web: Web app, controllers and configuration files (application context, hibernate maps, etc...)
Extension-Name: com.yourcompany.yourpackage
Specification-Title:
Specification-Version:
Specification-Vendor:
Implementation-Version:
Implementation-Vendor-Id: com.yourcompany.yourpackage
Implementation-Vendor:
Implementation-Title:
Obviously, you have to fill-in the file with the appropiate values. The second step is including our manifest.mf file in maven's build:
Now, the next time you run mvn package your manifest.mf will be included in your jar and oc4j classloader will load your own jars.
Suscribirse a:
Comentarios (Atom)