简体   繁体   English

在春季4拥有“单身人士”的正确方法是什么?

[英]What is the proper way to have a “singleton” in Spring 4?

I have a java file "DatabaseMan.java" that helps connect to a database and connects helper functions. 我有一个Java文件“ DatabaseMan.java”,可帮助连接到数据库并连接帮助程序功能。 How can I make it such that it is created once for the life of my spring application, and I can call of its methods "getAllRows" for example, in each of my other resource classes? 如何使它在spring应用程序的生命周期内创建一次,并且可以在其他每个资源类中将其方法称为“ getAllRows”?

Should I be declaring a bean in my Application.java or using some sort of annotation on my "DatabaseMan" class to indicate that it is "injectable"/"resusable"? 我应该在Application.java中声明一个bean还是在“ DatabaseMan”类上使用某种注释来表明它是“可注入的” /“可重复使用的”?

I see the following Spring3 example: http://www.mkyong.com/spring3/spring-3-javaconfig-example/ 我看到以下Spring3示例: http ://www.mkyong.com/spring3/spring-3-javaconfig-example/

The issue is, do I have to include this within every single resource: 问题是,是否必须在每个资源中都包含此内容:

    ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
HelloWorld obj = (HelloWorld) context.getBean("helloBean");
 obj.printHelloWorld("Spring3 Java Config");

Is there a better way to get to the "HelloWorld" with less code and more annotation in Spring 4? 在Spring 4中,有更少的代码和更多的注释可以更好地进入“ HelloWorld”吗?

Remember, the ApplicationContext is a container to manage all your beans and their inter-dependencies. 请记住, ApplicationContext是用于管理所有bean及其相互依赖关系的容器。 It is the entry point to your application. 这是您的应用程序的入口点。 Once you've set it up, all the managed objects are linked up and ready to go. 设置好之后,所有托管对象都已链接好,可以使用了。

Is there a better way to get to the "HelloWorld" with less code and more annotation in Spring 4? 在Spring 4中,有更少的代码和更多的注释可以更好地进入“ HelloWorld”吗?

It depends where you want to get it. 这取决于您要在哪里获得它。 If you want to get it from outside the ApplicationContext , then you need to do what you did. 如果要从ApplicationContext外部获取它,则需要执行您的操作。 If you want to get into another bean, just inject it and the ApplicationContext will do the rest. 如果您想进入另一个bean,只需注入它,其余的将由ApplicationContext完成。

@Component
class SomeOtherBean {
    @Autowired
    private HelloWorld helloWorldBean;

    // do something with it
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 单身人士 - 正确的方式 - Singleton – the proper way 使用Spring MVC流媒体的正确方法是什么 - What is proper way to stream media with Spring MVC 实现单例SqliteDatabaseHelper类的正确方法 - Proper way of implementing a singleton SqliteDatabaseHelper class 通过接口公开单例类的正确方法 - proper way of exposing singleton class through interface 使用Spring MVC修改数据库对象列表的正确方法是什么? - What is a proper way to modify a list of DB Objects using Spring MVC? 在spring Web应用程序中创建数据源的正确方法是什么? - What is the proper way to create a datasource in a spring web application? 将 spring 引导与 mongo 连接并阅读文档的正确方法是什么? - What is the proper way to connect spring boot with mongo and read document? 使用Spring和DBCP处理JDBC连接的正确方法是什么? - What's the proper way to handle JDBC connections with Spring and DBCP? 在 spring 集成中将 XML 转换为 Java 对象的正确方法是什么? - What is the proper way to convert XML to Java objects in spring integration? 将初始化代码添加到 Spring 引导应用程序的正确方法是什么? - What's the proper way to add initialization code to a Spring Boot application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM