Web Services 如何从web服务实现访问应用程序上下文?

wnvonmuf  于 2022-11-15  发布在  其他
关注(0)|答案(1)|浏览(146)

我遇到了Web服务的问题。我的端点类和实现类如下:
终点:

@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();
}   
}

实施:

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 "";
}

}

但是,我不能使用应用程序上下文,因为它是空的。我如何才能从这个类访问应用程序上下文?谢谢

wd2eg0qa

wd2eg0qa1#

我找到了如何检索应用程序上下文:

WebApplicationContext context = ContextLoader.getCurrentWebApplicationContext();
Service service = context.getBean(Service.class);

相关问题