Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always getting the same instance. (Does single() mean singleton?). #80

Open
mcculls opened this issue Jun 22, 2015 · 3 comments
Open

Always getting the same instance. (Does single() mean singleton?). #80

mcculls opened this issue Jun 22, 2015 · 3 comments

Comments

@mcculls
Copy link
Member

mcculls commented Jun 22, 2015

Originally reported on Google Code with ID 82

When using peaberry, I always get the same instance of an object. 
I don't specify the binding as being a Singleton Scope or use the @Singleton annotation

on the class. The binding is however annotated (with @Server). 

How do I get a unique instance? (A non-singleton). 
Am I using Peaberry wrongly? 

Bundle1: 
--------
Module1: 

bind(export(IDataProvider.class)).annotatedWith(Server.class)
                .toProvider(service(ServerCDODataProvider.class).export());

Activator1: 

@Inject
@Server
Export<IDataProvider> dataProvider;

.... start(BundleContext ctx){

createInjector(osgiModule(ctx), new Module1()).injectMembers(); 



Bundle2:
-------
Module2: 
        bind(IDataProvider.class).annotatedWith(Server.class).toProvider(
                service(IDataProvider.class).single());

ConsumerClass1: 

@Inject
@Server
IDataProvider provider1


ConsumerClass2: 
@Inject
@Server
IDataProvider provider2

provider1 == provider2 => true






Reported by bouhier.c on 2013-06-07 08:50:22

@mcculls
Copy link
Member Author

mcculls commented Jun 22, 2015

Peaberry works on top of OSGi services, so the reason you only see one instance is because
the service is a singleton and not a ServiceFactory: http://www.osgi.org/javadoc/r4v43/core/org/osgi/framework/ServiceFactory.html

The multiple() method is for situations where there are multiple services registered
to the same interface and you want to see all these services. Otherwise with single()
you would only see the highest-ranked matching service.

Reported by mcculls on 2013-06-14 12:23:03

@mcculls
Copy link
Member Author

mcculls commented Jun 22, 2015

If you want to get multiple copies of the same service then you need to implement ServiceFactory
- peaberry should then export the service as a ServiceFactory and you should then be
able to get a new instance per-bundle. If you want to generate multiple instances per-bundle
then you need to write your own factory API and export that as a service for the other
bundles to consume.

Reported by mcculls on 2013-06-14 12:27:43

@mcculls
Copy link
Member Author

mcculls commented Jun 22, 2015

Thanks Stuart. This clarifies!

Reported by bouhier.c on 2013-06-14 13:35:10

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant