Java:我们可以使用DAO作为Singleton实例吗?(Java: Can we use DAO as a Singleton instance)
这是一个普遍的问题,不是特定于我目前的应用程序。
在繁忙的多线程应用程序中,有什么办法可以做到:假设有一个DAO包含一个方法
updateData
来更新数据库中的一些数据。问题:
- 是否有一个很好的方法来获得该DAO类的Singleton实例并访问它的方法
updateData
?- 或者我应该每次创建该DAO的新对象并调用
updateData
方法?This is a general question, not specific to my current application.
In a heavy Traffic MultiThreaded application, what is the approach to do following: assume that there is a DAO which contains a method
updateData
to update some data inside of a database.Questions:
- Is it a good approach to have a Singleton instance of that DAO class and access it's method
updateData
?- Or should I every time create a new object of that DAO and call the method
updateData
?
原文:https://stackoverflow.com/questions/12579880
最满意答案
在您的示例中,@ Component创建的bean的默认名称是“demo”,与您的XML匹配,因此只会创建一个bean - 由XML创建的实例。
如果XML指定了不同的名称(或
@Component
批注给出了不同的名称),则将创建两个实例。In your example the default name for the bean created by @Component would be "demo", matching your XML, so only a single bean will be created - the instance created by the XML.
If the XML specified a different name (or the
@Component
annotation gave a different name), two instances would be created.
相关问答
更多-
是不是有一款什么手机软件,可以很自然的拉长腿部?不是美图秀秀,有知道的不?[2022-10-20]
spring -
春天的英语怎么读?[2022-05-21]
spring -
在阅读了一些相关的文章并在团队中进一步讨论后,我们得出以下结论。 我希望这对其他人有用。 关于XML配置 (我们现在正在使用的),我们决定保留用于库的依赖关系(由我们或由第三方开发)。 按照定义,图书馆只应提供某些功能,并可用于各种不一定涉及DI的情况。 因此,在我们的库项目中使用注释将会把DI框架(在我们的例子中为Spring)的依赖关系带到库中,并且在没有DI的情况下使用库时,这是没有用的。 拥有额外的依赖性在我们的团队中并不被认为是一种好的做法(一般为恕我直言)。 当我们组装应用程序时,应用程序具有所 ...
-
我们是否需要使用Annotation进行Spring配置的xml文件?(Do we need xml file for doing Spring configuration using Annotation?)[2022-03-12]
Spring配置不需要 XML。 您可以使用纯Java配置(注释)配置Spring。 例如,不是使用您在问题中发布的XML,而是使用@Configuration和@ComponentScan注释创建一个类: @Configuration @ComponentScan(basePackages = {"com.example.point", "com.example.shapes"}) public class MySpringConfig { public static void main(Str ... -
哪个具有更高的优先级:Spring注释或xml配置(Which have more priority: Spring annotation or xml configuration)[2023-09-29]
在您的示例中,@ Component创建的bean的默认名称是“demo”,与您的XML匹配,因此只会创建一个bean - 由XML创建的实例。 如果XML指定了不同的名称(或@Component批注给出了不同的名称),则将创建两个实例。 In your example the default name for the bean created by @Component would be "demo", matching your XML, so only a single bean will be cr ... -
我发现在Spring的XML配置中使用Hibernate相当直观,但是如果你以前从未将它添加到项目中,那么正常工作可能会很痛苦。 使用Spring的XML配置是Hibernate 4的首选选项。 所以你已经建立了一个Spring项目并且一切正常。 您现在想要添加Hibernate。 我总是喜欢在一个单独的XML文件中配置Hibernate,这个文件叫做WEB-INF目录中的database-servlet.xml ,但只要它在类路径上,名称就没关系了。 我的新database-servlet.xml如下所示 ...
-
您必须使用ApplicationContext而不是简单的BeanFactory 。 看起来像BeanFactory不运行后处理器,包括寻找@Autowired注释的那个。 我将尝试为此找到一份文档,同时尝试: new ClassPathXmlApplicationContext("/spring.xml").getBean(B.class); BTW @Autowired在setter,构造函数,字段等方面完全有效( 来源 ): 将构造函数,字段,setter方法或配置方法标记为由Spring的依赖注入 ...
-
你不能直接通过@Scheduled来做到这一点。 它不提供任何注释成员来指定bean引用名称。 相反,您必须使用SchedulingConfigurer 。 定义一个@Configuration类。 使用@EnableScheduling和@ComponentScan为具有@Scheduled注释方法的组件类型的包注释它。 然后让类实现SchedulingConfigurer 。 通过其configureTasks方法提供的ScheduledTaskRegistrar允许您设置任务计划程序。 例如: @Co ...
-
基于spring注释的appengine-web.xml配置(spring annotation based configuration of appengine-web.xml)[2023-05-24]
谷歌应用程序引擎默认支持Java 7.添加true 到你的appengine-web.xml中,并使用灵活的环境来处理java 8。 AppEngine上-web.xml中xxxxxxxxxxxx true tr ... -
如何在没有任何.xml配置的情况下使用spring 3.X注释(How to use spring 3.X annotation without any .xml configuration)[2022-08-30]
在搜索“无XML的Spring配置”或基于Java的Spring配置时,您应该可以在几分钟内自己想出来。 您还没有定义任何由@Configuration注释的类托管的bean-creatiion方法 - 因此Spring能够知道您实际想要注入的内容。 @Configuration // optional @ComponentScan(basePackages = {"name.your.package.here"} public class AppConfig { @Bean // or @Bean ...