sta*_*ker 2 java servlets dependency-injection seam
我在seam-gen(2.1.2)应用程序中创建了一个普通的servlet,现在我想使用注入.因此,我用@Name注释它,它被识别为组件:
INFO [Component] Component: ConfigReport,
scope: EVENT, type: JAVA_BEAN, class: com.mycompany.servlet.ConfigReport
Run Code Online (Sandbox Code Playgroud)
不幸的是记录器的注射不起作用NullPointerException在init()
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.log.Log;
@Name("ConfigReport")
public class ConfigReport extends HttpServlet {
@Logger
private Log log;
public void init(ServletConfig config) throws ServletException {
log.info( "BOOM" );
}
Run Code Online (Sandbox Code Playgroud)
}
我的做法是滥用吗?
什么是替代方案(客户端向servlet发送请求是curl,而不是浏览器)?
对于记录:
除了亚瑟 - 罗纳德 - 弗雷西亚给出的重要提示.
注入组件仍然不起作用,至少我可以手动查找组件,因此需要上下文ContextualHttpServletRequest.
components.xml中
<web:context-filter regex-url-pattern="/config/*"/>
Run Code Online (Sandbox Code Playgroud)
servlet.java
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
final HttpServletRequest req = request;
final HttpServletResponse res = response;
new ContextualHttpServletRequest(req) {
public void process() throws Exception {
wrappedGet( req, res );
}
}.run();
}
Run Code Online (Sandbox Code Playgroud)
在包装的servlet代码中
entityManager = (EntityManager) Component.getInstance( "entityManager" );
Run Code Online (Sandbox Code Playgroud)
链接:
| 归档时间: |
|
| 查看次数: |
3113 次 |
| 最近记录: |