哪种方法是最好的?
我有一个关于struts的问题。
我有一个 HashMap
,其中有近 50 个条目。现在我必须在操作类中定义此映射,例如 TestAction
。如您所知,此操作类扩展了 Action
类。现在我的疑问是根本性的:我应该将地图加载为静态吗?加载这个Map静态有什么好处呢?如果我要调用此 Action 类 100 次,那么此地图是否只加载一次?
另外,并非每次调用动作类时我都可以访问此地图。如果我调用此操作类 100 次,则可能只有 40 次需要访问此映射。
如果我加载这张地图有
public Map getMap()
{
Map testMap= new HashMap();
testMap.put("Harish",25);
testMap.put("Ravi",55);
return testMap();
}
什么缺点?哪种方法是最好的?
I have a question regarding struts.
I have a HashMap
which has nearly 50 entries. Now I have to define this map inside an action class, say TestAction
. As you know, this action class extends the Action
class. Now my doubt is fundamental: Should I load the map as static? What are the advantages of loading this Map static? If I am going to call this Action class 100 times, would this map have been loaded only once?
Also, not every time of the action class call I may access this map. If I call this action class 100 times, only 40 times there may be a need to access this map.
If I load this map as
public Map getMap()
{
Map testMap= new HashMap();
testMap.put("Harish",25);
testMap.put("Ravi",55);
return testMap();
}
what's the disadvantage? Which is the best approach?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您的数据永远不会改变,那么静态加载它似乎是最好的选择。它只会被加载一次。
如果您有时需要更改它,其他因素(例如测试和线程)可能会导致其他选项更可取。
If your data never changes, loading it statically seem the best option. It will be loaded only once.
If you sometimes need to change it, other factors like testing, and threading, might cause other options to be preferable.
现在的编码方式 - 每次调用 getMap() 方法时都会重新创建 testMap。我不确定“如果调用操作类”是什么意思 - 如果在你的类上调用 getMap() 方法,那么是的 - 如果调用了其他不以某种方式调用 getMap() 的方法,那么没有。
如果将地图引用设为静态,它将在类级别存在一次。您还可以静态加载地图,这只会发生一次。实际上,只有当您不打算更改地图的内容时才应该这样做,因为您可能会遇到线程问题,或者如果地图不断添加数据但从未删除,则必须确定何时清空地图, ETC。
如果您担心可能永远不需要地图,您可能需要考虑延迟加载 - 使地图成为静态引用,但始终通过静态方法访问它,检查地图是否已创建 - 如果没有创建是在第一次需要时创建的,而不是在启动时创建的。尽管在这种情况下这可能并不重要。
我认为这更多的是一个假设而不是一个要求 - 因为这是一个网络应用程序 - 更好的解决方案可能是在启动时将映射放入应用程序范围 - 几乎就像静态一样,只是它不与类绑定。
您可以添加 ServletContextListener 来监听您的网络应用程序启动和停止,并通过调用将地图添加到应用程序范围
然后在您的操作(或网络应用程序中的任何其他位置)中,您可以调用
并访问您的地图。
The way you have it coded now - testMap will be re-created every time the getMap() method is called. I'm not sure what you mean by "if the action class is called" - if the getMap() method is called on your class, then yes - if some other method is called that doesn't call getMap() in some way, then no.
If you make the Map reference static it will exist once at the class level. You can also statically load the Map, and this will only happen once. This should really only be done if you're not going to change the contents of the map, since you may run into threading problems or have to deal with figuring out when to empty the map if it's constantly getting data added to it but never removed, etc.
If you're worried that you may never need the Map, you may want to look into lazy loading - making the map a static reference, but always accessing it through a static method that checks if the Map's been created yet - and if not it gets created the first time it's needed rather than on startup. Though in this case it probably doesn't matter.
I think that's more of an assumption rather than a requirement - Since this is a web app - the better solution may be to put the Map into Application scope at startup - pretty much just like static, only it's not tied to a class.
You can add a ServletContextListener to listen for your web app to start and stop, and add the Map to Application scope by calling
Then in your action (or anywhere else in your webapp) you can call
and get access you your Map.
如果映射的内容将保持静态,请将其设为静态 - 50 个条目只是很小的数据量,并且对类加载时间的影响可以忽略不计。
If the contents of the map will remain static, make it static - 50 entries is a tiny amount of data and will have negligible effect on the class load time.