I have class
@ApplicationScoped
public class Service{
private Map<String, Integer> something ;
private final Logger LOGGER = LoggerFactory.getLogger(Service.class);
@PostConstruct
public void initialize(){
something = new HashMap<>();
}
public void increase(String userName){
something.put(userName, something.getOrDefault(userName, 0) + 1);
}
public Map<String, Integer> getSomething(){
return this.something;
}
public Integer getSomethingForUser(String userName){
return something.getOrDefault(userName, 0);
}
}
Which I want to be globally one instance.
The problem is, that when I'm injecting this service in two different places, I have two different instances of the service - which causes to return always counter 0
. .toString()
returns as follows:
package.services.Service@492e4f4b
package.services.Service@4bc86c4d
I created this service to test my HK2-Jersey implementation, which is apparently not working as should.
Web.xml:
<servlet-name>jersey-serlvet</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>io.swagger.jaxrs.listing,mypackage.rest</param-value>
</init-param>
<init-param>
<param-name>jersey.config.server.provider.classnames</param-name>
<param-value>
io.swagger.jaxrs.listing.ApiListingResource,
io.swagger.jaxrs.listing.SwaggerSerializers
</param-value>
</init-param>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>mypackage.config.ApplicationConfiguration</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersey-serlvet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
ApplicationConfiguration.java:
public class ApplicationConfiguration extends ResourceConfig {
public ApplicationConfiguration() {
register(new AbstractBinder() {
@Override
protected void configure() {
bind(Service.class).to(Service.class);
}
});
packages(true, "com.mypackage.rest");
}
}
Without this bind
function, server throws exception that @Inject
wasn't satisfied.
Can anybody point out, what is wrong?