0

I have a class with annotation @Service :

@Service
public class NeoEntityService {

    @Override
    public List<NeoEntity> getRenamedEntitiesForUser(DirectoryUser user, String siloId) throws OxwayException {
        List<NeoEntity> entities = new ArrayList<NeoEntity>(); 
        return entities;
    }
}

I want to use this service in another class like this :

@Service("briefcaseService")
public class BriefcaseService {

    @Resource
    private NeoEntityService neoEntityService;
    
    public void test(DirectoryUser user, String siloId) {
        try {
            System.out.println(neoEntityService.getRenamedEntitiesForUser(user, siloId).size());
        } catch (OxwayException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

When I start my server and try to use my BriefcaseService i have this error :

javax.servlet.ServletException: Error injecting resources into managed bean 'BriefcaseService'

And also this one :

java.lang.NullPointerException
    com.ixxo.wms.iface.jsf.pages.briefcase.views.BriefcaseService.test(BriefcaseService.java:9)

I've tried a lot of things, including adding @Service("neoEntityService") in my class NeoEntityService but it still doesn't work. It seems like BriefcaseService is not able to get NeoEntityService so it's null when I am using it in BriefcaseService.

Any idea?

Thanks

EDIT : I ran this code on my colleague's PC and it works on his computer. Any idea why and how? It's probably not an error of annotation or anything, just an environment problem I guess.

v0ld3m0rt
  • 866
  • 3
  • 12
  • 47
user19013678
  • 95
  • 1
  • 4
  • 1
    Take a look at this thread. It replies to your problem: https://stackoverflow.com/questions/4093504/resource-vs-autowired – sigur Jun 28 '22 at 07:44

1 Answers1

0

Since I see no Reason to JSR-250 annotations (Jarkata EE), I would suggest using @Autowired (Spring) instead.

In your BriefcaseService class add a constructor annotated with @Autowired accepting NeoEntityService:

@Service("briefcaseService")
public class BriefcaseService {

    private NeoEntityService neoEntityService;
    
    @Autowired
    public BriefcaseService(NeoEntityService neoEntityService){
        this.neoEntityService = neoEntityService;
    }

// rest of class

}

Please take a look at this baeldung tutorial: https://www.baeldung.com/spring-annotations-resource-inject-autowire (especially look at section 5 at the end)

Also please avoid using field injection if possible (https://stackoverflow.com/a/40620318/7142748)

jhueg
  • 483
  • 2
  • 13
  • Hello, can you pls consider the edit – user19013678 Jun 28 '22 at 08:53
  • I see no reason this could be an environment problem. Only thing i could imagine is, that you use an other java version (because of JSR-250 annotations), but I think its unrealistic this is the cause. – jhueg Jun 28 '22 at 09:07
  • Is there a good reason to not use the Spring annotations? `@Autowired` instead `@Resource`. If not, try it the way I suggested – jhueg Jun 28 '22 at 09:09