0

There is any open source generic bean container?

Let me explain the scenario.

I am working in a Java EE project, and we are using a Swing client. We are using JSON to serialize objects. In client side we can receive a JSON message representing a List. Sometimes some A instances can be the same object, but actually for 2 instances of A that are the same object we construct 2 beans that are equals between them but aren't the same instance. This give us several problems:
memory overhead, changes made to one instance don't update the other one, ...

What I think we need is a container that can make instance wiring, when a new instance is constructed we should check if it already exist in the container, in that case assign it instead the constructed one. Something like JPA L2 cache, but in client side.

Is there any implementation about it?
Or maybe i should use another approach?

4
  • 1
    Could you please use a more specific word than "bean"? Any serializable Java object with a default constructor and getters/setters is a bean. Spring is a good way to handle such beans on client-side. Do you actually mean something more specific?
    – Nicolas Raoul
    Commented Apr 4, 2014 at 9:14
  • In my case there are "domain objects". But any instance could be used for my problem. Instances that are Objects.equals(instanceA, instanceB);
    – Ezequiel
    Commented Apr 4, 2014 at 9:26
  • Do you need persistency or not? Should the objects remain on the local side even after quitting your app?
    – Nicolas Raoul
    Commented Apr 4, 2014 at 9:43
  • I dont need persistence, I just need a framework or library to help me in not to duplicate instances when they are rehydrated in client side.
    – Ezequiel
    Commented Apr 5, 2014 at 1:52

2 Answers 2

2

You might want to try HK2 (http://hk2.java.net). It has things like Singleton scope for those "Bean" instances that should only be there once and things like PerLookup for things that should get created every time they are looked up for or injected. Furthermore, it works well in the JavaSE environment, and even has provisions for things like security in that environment (which other frameworks like this often overlook).

It is a lot like CDI, except that it was designed from the ground up to work in JavaSE.

2
  • This framework is capable to delete instances from pool when they are not more needed?.
    – Ezequiel
    Commented Apr 7, 2014 at 5:56
  • Yes it does. It is fully dynamic (adds and deletes) since it is designed to support plugin-in type capabilities, where sometimes the plugins are removed Commented Apr 7, 2014 at 10:47
0

I suggest you store your beans in a HashSet.

HashSet implements the Set interface, which stipulates that all objects are stored uniquely inside:

A collection that contains no duplicate elements. More formally, sets contain no pair of elements e1 and e2 such that e1.equals(e2) [...]

Just implement your beans' .equals and .hashCode methods to specify what is equality between 2 objects (if you have not done it already).

HashSet is available in any Java since 1.2, so you won't even need any extra libraries.

1
  • 1
    I dont want to reinvent the wheel. Implementing self-made solutions usually are poorer solutions than open source, actively maintained, project. By the way, i think that a better implementation for Set in this case is ConcurrentHashSet. But both implementantions has low insert performance. Thats why im looking for a specific solution instead doing it myself. Thanks anyway.
    – Ezequiel
    Commented Apr 5, 2014 at 19:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.