Can we convert fields to/from GemFire PdxSerializer using annotations or Java Reflection? - caching

Currently we are using GemFire for caching. We are in our initial phase of integrating it.
While we are working on integrating GemFire using the Spring Data GemFire libraries, and using GemFire's PdxSerializer, too, I am wondering if there is a way to convert fields to/from PDXReader/PDXWriter using basic annotations and Java Reflection.
We are using Sprint Boot's 2.0.3.RELEASE JARs.
Note: I have looked at the below link:
https://gemfire.docs.pivotal.io/95/geode/developing/data_serialization/auto_serialization.html
I am more curious how to use a non-XML way of doing this.

Have a look at the following:
SDG's o.s.d.g.mapping.MappingPdxSerializer as described in the documentation.
Then read how to configure it.
If you are using Spring Boot for Pivotal GemFire, then PDX is auto-configured automatically, by default, for you and you do not need to explicitly declare SDG's #EnablePdx annotation.
NOTE: SDG's MappingPdxSerializer, when configured and registered with Pivotal GemFire, automatically de/serializes your application domain object types without any special config, like you have to do when using GemFire's own ReflectionBasedAutoSerializer and clumsy REGEX to identify your types properly, blah! SDG's MappingPdxSerializer is much more robust, using first-class filters to express exclusions and even inclusions (which can override the default exclusions, that is any class types in java.*, com.gemstone.gemfire.*, org.apache.geode.* or org.springframework.*, by default).
Anyway...
Take 2 pills and call me in the morning - the "doc", ;-)

Related

Spring Security - XML vs Java Configuration

I'm just learning Spring Security, and a lot of Spring's documentation appears to use Java-based bean configuration (as opposed to XML.) Overall, this seems to be the way a lot of their projects are going. However, portions of their documentation tend to start with Java configuration and then switch to XML config later on. I found a blurb in one document (http://docs.spring.io/spring-security/site/docs/3.2.0.RELEASE/reference/htmlsingle/) stating the following:
Spring Security’s Java Configuration does not expose every property of every object that it configures. This simplifies the configuration for a majority of users. . . . While there are good reasons to not directly expose every property, users may still need more advanced configuration options. To address this Spring Security introduces the concept of an ObjectPostProcessor which can used to modify or replace many of the Object instances created by the Java Configuration.
Can everything that can be done in XML configuration be done with Java config? Is there a definite direction that the Spring community is taking overall in terms of configuration style?
You can choose either java based or xml based configuration.Stick to one, don't mix both.But don't forget to use the annotation based configuration.You just need to annotate spring managed components with #component,#service etc.You don't need to have that bean defenition in xml or java class.
<context:annotation-config/>
<context:component-scan base-package="com.package"/>
or
#Configuration
#ComponentScan({"com.foo.bar", "org.foo.bar"})
http://docs.spring.io/spring-security/site/docs/3.2.0.RC2/reference/htmlsingle/#jc
You can use Java or XML based. But there is a thing
Usage of xml based configuration is decreasing in newer versions of Spring.
Like #EnableAutoConfiguration tag...
With this, web applications doesnt need any XML conf even web.xml

Benefits to using Spring templates

What are the enumerated benefits to using the Spring template classes such as RestTemplate, JdbcTemplate, JpaTemplate, JdoTemplate, JndiTemplate JdoTemplate etc?
I'm looking for the general benefits of using these classes as best practice design, for example the thread safeness of these classes. Or if someone could point me to the section of the Spring documentation that discusses the same I would accept that as well.
Less boilerplate code
More cohesive exceptions handling (e.g. JDBC drivers checked exceptions are translated to meaningful runtime exceptions)
Easier and uniform configuration (almost everything can be configured in Spring context files)
Automatized resource management (like, for instance, closing DB connections)
Note, that not all templates are equally useful. For instance jdbcTemplate is a pure gem, I can't live without that (mostly because JDBC is not the best interface one can imagine), on the other hand jpaTemplate does not bring so many advantages.
Note, that if you use given template, your code becomes dependent on Spring interfaces, so Spring is not only dependency mechanism for your application, but becomes also part of it - Spring is no longer easily replacable with something else (Google Guice, CDI). However, given the fact Spring is a pretty good peace of code, I would not be worried about that aspect.

Use of Spring oxm

I am new to spring. I was looking into spring-oxm's XStreamMarshaller. I was hoping to find a way to convert my objects into xml using this. The spring site tells me clearly how to do it but it still needs me to add a XStream dependency in my POM. I don't understand what the use of spring-oxm is? If i had to add the xstream dependency anyway then i can directly use xstreams toXml operation and be done with it? I would really appreciate any help I could get in understanding the use of spring-oxm.
Thanks a lot in advance!
Spring provides a higher level abstraction for you by eliminating the scaffolding code you need to write. For e.g. in case of OXM you will be working with Marshaller and Unmarshaller abstractions irrespective of the underlying implementations uses (XStream, JAXB, Castor, XmlBeans etc). Moreover it lets you use DI for injecting marshalling/unmarshalling services to your own services. Another advantage is consistent exception hierarchy irrespective of the underlying implementation. All these are well explained on their reference documentation.
If you have very simple needs and doesn't already use Spring then I suggest you stick to JAXB that comes with JDK 6.

Spring Data JPA like project not dependent on Spring

Does anyone know any Java frameworks that follows the repository approach with automatic implementation of query methods (e.g. findByNameAndLastName(…)) but not tied to Spring, only pure JPA. Such feature also exists in GORM. I would like to see if there is any project that can be used in Guice or pure JavaEE environment without bringing Spring as a dependency.
(Disclaimer: I am the author of Spring Data JPA)
There is the CDI Query Module which is very similar to what Spring Data JPA. There's also a DeltaSpike module.
Note that Spring Data JPA ships with a CDI extension that creates repository proxies as plain CDI beans and does not bootstrap a Spring container. There are APIs that allow the creationg of repository proxies programmatically such as:
EntityManager em = // … obtain EntityManager
JpaRepositoryFactory factory = new JpaRepositoryFactory(em);
UserRepository repository = factory.getRepository(UserRepository.class);
Yes, it still requires Spring libraries to be present on the classpath but it is then using them similar to how you would use Commons Collection or the like. We try not to reinvent the wheel and the Spring libraries we depend on provide a lot of useful code that we do not have to re-code.
So if it's Spring as DI container you're worrying about, feel free to give the CDI extension of Spring Data JPA a choice. If you don't want to use any Spring whatsoever (for whatever reason), have a look at the alternatives.
Based on Oliver's information, followed up as also interested in this topic --
CDI Query joining Deltaspike mail thread: http://apache-deltaspike-incubator-discussions.2316169.n4.nabble.com/Porting-the-CDI-Query-extension-project-to-DeltaSpike-td4329922.html
Deltaspike base link: http://deltaspike.apache.org/index.html
Getting started: http://deltaspike.apache.org/documentation.html
Just did their 0.4th release as of 5/31/2013.
However, have not done enough of a review to contrast/compare Deltaspike versus Spring-Data w/ CDI extensions (spring-data being very mature).
Take a look at Tomato on github!
It is a functional replacement for Spring JPA, has zero dependencies, performs better and is far easier to use. It will reduce your data access code by 98% and deliver the results you want right out of the box.
https://rpbarbati.github.io/Tomato.
If you want free, fully functional dynamic forms and/or tables for any Tomato entity or hierarchy, that can also be customized easily, try the angular based companion project...
https://rpbarbati.github.io/Basil
Both are current, maintained projects.
Try it yourself, or contact the author at rodney.barbati#gmail.com with questions.

Which Spring Entity Manager Factory should I use?

There are two entity manager factory beans in Spring that would work for my application. The org.springframework.orm.jpa.LocalEntityManagerFactoryBean and org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean. I am using Spring 3.0 with EclipseLink JPA 2.2.
What I've read about these two are that they are the same. Except that LocalContainerEntityManagerFactoryBean uses weaving. What is it? And, why would I want to use it?
"Weaving" is a term for program transformation, usually heard around "aspect oriented programming" area. The transformation is usually not done to the source but the .class (the bytecode) and a techy term for changing bytecode is "bytecode instrumentation".
why would I want to use it?
The JPA implementation you use may rely on such bytecode instrumentation for some features it provides and hence you might be forced into using it.
And for weaving to work correctly, you might need to specify a -javaagent: For eg,see section 'Eclipse Junit' here.
It looks like LocalContainerEntityManagerFactoryBean allows you to configure a weaver implementation ( one of DefaultContextLoadTimeWeaver, GlassFishLoadTimeWeaver, InstrumentationLoadTimeWeaver, OC4JLoadTimeWeaver, ReflectiveLoadTimeWeaver, SimpleLoadTimeWeaver, WebLogicLoadTimeWeaver
at an XML file, instead of relying on a -javaagent runtime argument.
This configuration isn't such a big factor, I'd guess.
Other features which the docs explain, sound like deciding factors.
LocalEntityManagerFactoryBean bootstrap is appropriate for standalone applications which solely use JPA for data access. If you want to set up your persistence provider for an external DataSource and/or for global transactions which span multiple resources, you will need to either deploy it into a full Java EE 5 application server and access the deployed EntityManagerFactory via JNDI, or use Spring's LocalContainerEntityManagerFactoryBean with appropriate configuration for local setup according to JPA's container contract.
If you plan on deploying your application to an Application Server and letting the Application Server manage the Entity Manager Factory and Transactions than the LocalContainerEntityManagerFactoryBean might be a better option. If you rather have the Application be more isolated, than the LocalEntityManagerFactoryBean would be more appropriate.
This blog can help provide more insight: http://second-kind-demon.blogspot.com/2011/06/spring-jpa-java-ee-jboss-deployment.html

Resources