Elastic Social Manual / Version 2406.0
Table Of ContentsElastic Core uses dependency injection for configuration of components, specifically JSR-330: Dependency Injection for Java and JSR 250: Common Annotations for the Java Platform. These standards are supported by Spring 3.0 and later versions.
Use the @Inject
annotation to get Elastic Core and
Elastic Social services injected into any Spring Bean. The following example shows a
Spring controller which uses the UserService
.
import com.coremedia.elastic.core.api.user.User; import com.coremedia.elastic.core.api.user.UserService; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.Controller; import javax.inject.Inject; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ExampleController implements Controller { @Inject private UserService userService; public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { User user = userService.getUserById( request.getParameter("userId")); response.setContentType("text/plain"); response.getWriter().format("Hello %s!", user == null ? "World" : user.getName()); return null; } }
Example 4.19. Spring controller with UserService