'How can i access to application context from a web service implementation
i'm having problem with web services. I have the endpoint class and the implementation class as follow:
Endpoint:
@WebService
public class LogExposer {
private LogExposerManager manager;
public boolean Login(String username, String password) {
return manager.doLogin(username, password);
}
public String documentJSONLog(Long idDocument) {
return manager.documentJSONLog(idDocument);
}
public String getService() {
return manager.getService();
}
}
Implementation:
public class LogExposerManager implements LogExposerInterface {
private JSONObject JSONLog;
@Autowired
protected ApplicationContext context;
private boolean autorized = false;
/**
* @param username Username used for the login operation
* @param password The password for the user
* @return
*/
@Override
public boolean doLogin(String username, String password) {
autorized = username.equals(password);
try {
Service service = (Service) context.getBeans("service");
} catch (NullPointerException e) {
autorized = false;
}
return autorized;
}
/**
* @param idDocument
* @return The JSON rappresentation of the documentlog file
*/
@Override
public String documentJSONLog(Long idDocument) {
JSONLog = new JSONObject();
JSONLog.put("Document id:", idDocument);
return JSONLog.toString();
}
/**
* @return
*/
@Override
public String getService() {
return "";
}
}
However i can't used the ApplicationContext because it is null. How can i access to the ApplicationContext from this class? Thanks
Solution 1:[1]
I find how to retrieve the application context:
WebApplicationContext context = ContextLoader.getCurrentWebApplicationContext();
Service service = context.getBean(Service.class);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | BlazekaMarko |
