【发布时间】:2015-12-28 20:00:35
【问题描述】:
在学习how to use JNDI resources in Tomcat 7的时候,做了一个定制的bean factory,叫MyBeanFactory。它为我的 Web 应用程序提供外部资源。第一次,我们假设它提供了当前的系统时间戳。
但是,我意识到 MyBeanFactory 在第一次请求时只被调用了一次。似乎 java bean MyBean 在第一次执行后已经存储在 Tomcat 中。然后MyBean 被每个请求重用。无论我何时在浏览器中重复 URL,响应总是显示与第一个相同的时间戳。以下是可能有帮助的元素列表:
-
MyBean:
MyBeanFactory提供的bean -
MyBeanFactory:实现
javax.naming.spi.ObjectFactory的工厂 - context.xml:资源的上下文描述符,位于 META-INF 文件夹中
- web.xml:部署描述符
- MyAction:http servlet
- 控制台
有人能告诉我为什么MyBeanFactory 只被调用一次,是否可以更改此功能?因为我需要修改工厂与服务器中的另一个进程建立套接字。见How to serve a socket from a Java EE application?
MyBean
public class MyBean {
private String foo = "Default Foo";
private long bar = 0;
public String getFoo() { return (this.foo); }
public void setFoo(String foo) { this.foo = foo; }
public long getBar() { return (this.bar); }
public void setBar(long bar) { this.bar = bar; }
}
MyBeanFactory
public class MyBeanFactory implements ObjectFactory {
@Override
public Object getObjectInstance(Object obj, Name name, Context nameCtx,
Hashtable<?, ?> environment) throws NamingException {
System.out.println("MyBeanFactory starts");
MyBean bean = new MyBean();
bean.setBar(System.currentTimeMillis());
// Return the customized instance
return bean;
}
}
context.xml
<?xml version="1.0" encoding="UTF-8"?>
<Context
path="/jndi"
reloadable="true"
cachingAllowed="false"
antiResourceLocking="true">
<Resource
name="bean/MyBeanFactory"
auth="Container"
type="com.mycompany.MyBean"
factory="com.mycompany.MyBeanFactory"
bar="23"/>
</Context>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID"
version="3.1">
<display-name>jndi</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>com.mycompany.MyAction</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
<!--
Apache Tomcat 7: JNDI Resources HOW-TO
https://tomcat.apache.org/tomcat-7.0-doc/jndi-resources-howto.html
-->
<resource-env-ref>
<description>
Object factory for MyBean instances.
</description>
<resource-env-ref-name>
bean/MyBeanFactory
</resource-env-ref-name>
<resource-env-ref-type>
com.mycompany.MyBean
</resource-env-ref-type>
</resource-env-ref>
</web-app>
我的行动
public class MyAction extends HttpServlet {
// ...
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("doGet");
response.getWriter()
.append("Hello coming from ")
.append(request.getRequestURI());
Context initCtx = null;
try {
System.out.println("initCtx");
initCtx = new InitialContext();
} catch (NamingException e) {
e.printStackTrace();
}
Context envCtx = null;
try {
System.out.println("envCtx");
envCtx = (Context) initCtx.lookup("java:comp/env");
} catch (NamingException e) {
e.printStackTrace();
}
MyBean bean;
try {
System.out.println("lookup");
bean = (MyBean) envCtx.lookup("bean/MyBeanFactory");
response.getWriter()
.append("foo = " + bean.getFoo() + ", ")
.append("bar = " + bean.getBar() + ";");
} catch (NamingException e) {
e.printStackTrace();
}
System.out.println("------");
}
}
控制台
Dec 28, 2015 7:41:03 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 412 ms
doGet
initCtx
envCtx
lookup
MyBeanFactory starts // called only once
------
doGet
initCtx
envCtx
lookup
------
doGet
initCtx
envCtx
lookup
------
【问题讨论】:
-
由于您正在定义一个 servlet 上下文资源,因此期望给定的上下文只有一个实例似乎是合理的。上下文资源的全部意义在于共享。话虽如此,您通过引用 Tomcat 7 让我有点失望,但使用了该版本的 Tomcat 未记录的配置属性。您实际使用的是哪个版本?
-
@JohnBollinger 我正在使用 tomcat 8.0.21
标签: java tomcat jakarta-ee jndi