一,Service
Add interface
public interface XicoService {
public String getData();
}
Add Class
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Service;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Activate;
import java.util.Map;
@Component(immediate=true, label="Xico Service", description="This is a Service", metatype=true)
@Service
public class XicoServiceImpl implements XicoService {
@Property(value="http://companyservices/myservice?wsdl")
static final String SERVICE_ENDPOINT_URL = "service.endpoint.url";
private String serviceEndpointUrl;
@Activate
public void activate(final Map<String, Object> props) {
this.serviceEndpointUrl = (String)props.get(SERVICE_ENDPOINT_URL);
}
@Override
public String getData() {
return serviceEndpointUrl;
}
}
二,查看效果
http://localhost:4502/system/console/services
http://localhost:4502/system/console/configMgr
三,获取数据
import org.apache.felix.scr.annotations.*;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.HttpConstants;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.apache.sling.commons.json.JSONArray;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import java.io.IOException;
@Component(immediate = true)
@Service(value = Servlet.class)
@Properties({
@Property(name = "service.description", value = { "Generating Page Servlet" }),
@Property(name = "sling.servlet.paths", value = {"/libs/sling/generating/page"}),
@Property(name = "sling.servlet.methods", value = { HttpConstants.METHOD_GET }) })
public class GeneratingPageServlet extends SlingAllMethodsServlet {
@Reference(policyOption = ReferencePolicyOption.GREEDY) private XicoService xicoService;
@Override
protected void doGet(final SlingHttpServletRequest req,
final SlingHttpServletResponse resp) throws ServletException, IOException {
try {
resp.getOutputStream().println( "error!!" + xicoService.getData());
} catch (IOException e) {
resp.getOutputStream().println( "error!!");
}
}
}
http://localhost:4502/libs/sling/generating/page