1

My goal in this entire endeavor is to try and inject a data source into a resource that can change at runtime. I am trying to avoid having to bloat the logic inside my resource if at all possible.

This is a legacy application with Jersey 1.19.1 using Spring 4.1.9.RELEASE (spring-aop, spring-beans, spring-context, spring-core, spring-expression, spring-web)

So here is my example resource:

@Path("/test)
public class TestResource {
  public TestResource(){
     System.out.println("Constructor Hit");
  }
}

If I run hit this resource I get the following output

...request...
Constructor Hit
...request...
Constructor hit

So, the resource is created everytime there is a request, great!

Now, I want to use spring to inject a bean into this resource, so I get the following class.

@Path("/test)
@Component
public class TestResource {
  @Inject
  public TestResource(MyBean test){
     System.out.println("Constructor Hit");
  }
}

Now I get the following output

Constructor hit
...request...
...request...

The resource is only created once (I'm guessing at Spring application creation time).

Is there a way to fix this? I believe it is because I annotate with @Component but I can't seem to get the injection part without that.

xenteros
  • 15,586
  • 12
  • 56
  • 91
Erik L
  • 1,104
  • 15
  • 31
  • And why is that an issue? As long as your class doesn't hold state it can be perfectly well be a singleton. – M. Deinum Aug 02 '16 at 15:02
  • The idea is that I want to switch out at runtime what `MyBean` instance actually is. So for instance, it is a datasource that I want to change at runtime and rather than adding logic to the resource of how to extract from a factory, I just wanted to have that instance injected into the resource to keep the logic cleaner. – Erik L Aug 02 '16 at 15:04
  • Then use AOP to wrap your `MyBean` which contains the logic. You still need to implement the logic somewhere, instead of doing it manually and have creational overhead there might be smarter ways... – M. Deinum Aug 02 '16 at 15:10
  • Was trying to avoid the proxy layer but I will investigate it further. – Erik L Aug 02 '16 at 15:15
  • You still need something, be it a factory or not. Either inject the factory into yur resource and call a method to get the actual bean, if you use a factorybean you still need the logic (which would in turn be quite a contraption because for some reason you need it to differ per request not sure why). – M. Deinum Aug 02 '16 at 18:15
  • I won't be every request that differs, its more like hey, this data source has changed and I need to flip it over without restarting the application. Also, I know I'm being pedantic over not injecting a factory and then extracting, it was that I read in the jersey-spring and jersey documents that every request is a new instance, so I was wondering if I was doing something wrong. – Erik L Aug 03 '16 at 03:02
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/118965/discussion-between-erik-l-and-m-deinum). – Erik L Aug 03 '16 at 03:04

1 Answers1

0

Spring's default scope is singleton. when you call prototype class in singleton, spring DI inject prototype class just once at instantiate time. read this issue if you want the singleton bean get a new instance of the prototype bean at runtime.

Community
  • 1
  • 1
MTB
  • 405
  • 1
  • 5
  • 12