0

I am trying to build a basic webpage with Spring + JSF. Basically, I have one HelloController and HelloSpringService. It gives error on the return helloSpringService.sayHello(); line in HelloController class.


public class HelloController {


    @ManagedProperty("#{helloSpringService)")
    private HelloSpringService helloSpringService;

    public String showHello() {

        return helloSpringService.sayHello();
    }

    public void setHelloSpringService(HelloSpringService helloSpringService) {
        this.helloSpringService = helloSpringService;
    }
    public HelloSpringService getHelloSpringService() {
        return helloSpringService;
    }

}

import org.springframework.stereotype.Service;

@Service
public class HelloSpringService {

    public String sayHello() {
        return "hellofromspringservice";
    }
}
Moshe Slavin
  • 5,127
  • 5
  • 23
  • 38
emresahin
  • 33
  • 11

3 Answers3

0

Use @Autowired instead of @ManagedProperty("#{helloSpringService)")

Ali Ben Zarrouk
  • 1,891
  • 16
  • 24
0

You have created the bean HelloSpringService as a service bean so Spring core is responsible for the life cycle of the bean so you must call it with @Autowired instead of @ManagedProperty.

Ismail
  • 2,322
  • 1
  • 12
  • 26
-1
helloSpringService

should be autowired rather than included as managedproperty

Amit
  • 391
  • 4
  • 9
  • How does this differ (and is significantly better) than the other answers? – Kukeltje Mar 26 '19 at 17:26
  • This comment was added almost the same time the other ones were added, when i posted this, I did't see the other comments. Could be due to the fact that i was on the page for a longer time, and didn't reload the page before publishing my comment – Amit Apr 02 '19 at 16:04