我还没有找到我能够理解的Spring bean的高级定义。我经常在Grails文档和书籍中看到它们,但我认为了解它们是什么将是有益的。那么春豆是什么呢?如何使用它们?它们与依赖注入有关吗?
当前回答
Spring beans are classes. Instead of instantiating a class (using new), you get an instance as a bean cast to your class type from the application context, where the bean is what you configured in the application context configuration. This way, the whole application maintains singleton-scope instance throughout the application. All beans are initialized following their configuration order right after the application context is instantiated. Even if you don't get any beans in your application, all beans instances are already created the moment after you created the application context.
其他回答
Bean是一个POJO(普通旧Java对象),由spring容器管理。
默认情况下,Spring容器只创建bean的一个实例。 此bean被缓存在内存中,因此对该bean的所有请求将返回对同一bean的共享引用。
@Bean注释返回一个对象,spring在应用程序上下文中将该对象注册为bean。 方法内部的逻辑负责创建实例。
什么时候使用@Bean注释?
当没有自动配置选项时。 例如,当我们想从第三方库连接组件时,因为源代码不可用,所以我们不能用@Component注释类。
实时场景可能是某人想要连接到Amazon S3桶。 因为源不可用,所以他必须创建一个@bean。
@Bean
public AmazonS3 awsS3Client() {
BasicAWSCredentials awsCreds = new BasicAWSCredentials(awsKeyId, accessKey);
return AmazonS3ClientBuilder.standard().withRegion(Regions.fromName(region))
.withCredentials(new AWSStaticCredentialsProvider(awsCreds)).build();
}
源代码以上的代码-> https://www.devglan.com/spring-mvc/aws-s3-java
因为我上面提到了@Component Annotation。
@Component表示带注释的类是一个“组件”。在使用基于注释的配置和类路径扫描时,这些类被认为是自动检测的候选类。
组件注释将类注册为单个bean。
Spring核心技术参考文档描述了bean是什么。
根据Spring IoC容器和bean的介绍部分(其中“IoC”表示“控制反转”):
在Spring中,构成应用程序主干并由Spring IoC容器管理的对象称为bean。bean是由Spring IoC容器实例化、组装和管理的对象。否则,bean只是应用程序中的众多对象之一。bean以及它们之间的依赖关系反映在容器使用的配置元数据中。
Bean和作用域在Bean作用域部分中描述:
When you create a bean definition, you create a recipe for creating actual instances of the class defined by that bean definition. The idea that a bean definition is a recipe is important, because it means that, as with a class, you can create many object instances from a single recipe. You can control not only the various dependencies and configuration values that are to be plugged into an object that is created from a particular bean definition but also control the scope of the objects created from a particular bean definition. This approach is powerful and flexible, because you can choose the scope of the objects you create through configuration instead of having to bake in the scope of an object at the Java class level. Beans can be defined to be deployed in one of a number of scopes.
Spring beans are classes. Instead of instantiating a class (using new), you get an instance as a bean cast to your class type from the application context, where the bean is what you configured in the application context configuration. This way, the whole application maintains singleton-scope instance throughout the application. All beans are initialized following their configuration order right after the application context is instantiated. Even if you don't get any beans in your application, all beans instances are already created the moment after you created the application context.
bean是Spring Container管理的类的实例。
Spring bean只是由Spring容器管理的实例对象,也就是说,它们是由框架创建和连接的,并放入一个“对象袋”(容器)中,您以后可以从那里获取它们。
“连接”部分是依赖注入的全部内容,它的意思是你可以说“我将需要这个东西”,框架将遵循一些规则来为你获得适当的实例。
对于那些不习惯Spring的人来说,我认为Wikipedia Spring的文章有一个很好的描述:
Central to the Spring Framework is its inversion of control container, which provides a consistent means of configuring and managing Java objects using reflection. The container is responsible for managing object lifecycles of specific objects: creating these objects, calling their initialization methods, and configuring these objects by wiring them together. Objects created by the container are also called managed objects or beans. The container can be configured by loading XML files or detecting specific Java annotations on configuration classes. These data sources contain the bean definitions which provide the information required to create the beans. Objects can be obtained by means of either dependency lookup or dependency injection. Dependency lookup is a pattern where a caller asks the container object for an object with a specific name or of a specific type. Dependency injection is a pattern where the container passes objects by name to other objects, via either constructors, properties, or factory methods.
推荐文章
- 禁用IntelliJ星(包)导入?
- 面试问题:检查一个字符串是否是另一个字符串的旋转
- 将文件加载为InputStream的不同方法
- 到底是什么导致了堆栈溢出错误?
- 为什么Android工作室说“等待调试器”如果我不调试?
- Java:路径vs文件
- ExecutorService,如何等待所有任务完成
- Maven依赖Servlet 3.0 API?
- 如何在IntelliJ IDEA中添加目录到应用程序运行概要文件中的类路径?
- getter和setter是糟糕的设计吗?相互矛盾的建议
- Android room persistent: AppDatabase_Impl不存在
- Java的String[]在Kotlin中等价于什么?
- Intellij IDEA上的System.out.println()快捷方式
- 使用Spring RestTemplate获取JSON对象列表
- Spring JPA选择特定的列