spring summary --Spring Bean

What is Bean:

Simply put, Spring bean is Spring Framework runtime management of objects . Spring bean is any Spring application of the basic building blocks . Most application logic code you write will be placed in a Spring bean.

Spring bean management include:

  • Create an object
  • Providing dependencies (e.g. other bean, configuration properties)
  • Intercept method calls the object framework to provide additional functionality
  • Destroy an object

Spring bean is a basic conceptual framework. As the Spring user, you should have a deep understanding of the abstract the core.

Create a collaborative relationship between the application object's behavior is called: Assembly (Wiring) , which is the essence of dependency injection.

How to define a Spring bean:

1. configuration type @Component annotation (or derivatives thereof) Note your class

2. Write in a custom Java class configuration used @Bean bean factory method annotated

3. Declare bean definitions in the XML configuration file

between bean definition method of choice depends on the access to be used as the Spring bean class source code. If you have the source code, it typically will be used directly on the class @Component comments

If a class is part of an external library and you can not use @Component be a comment, you must use the @ bean in a custom configuration class  Bean to create a factory method comment.

Spring bean property:

Whichever method you choose bean definition, they are allowed to describe the same set of bean properties. Spring should provide information about the properties of how details create an object:

Spring bean properties list includes:

  • class
  • name
  • rely
  • range
  • Initialization mode
  • Initialization callback
  • Destroy callback

To simplify the bean definition, Spring provides default values ​​for almost all properties. However, to learn how to customize the default value is very important. Let us study one by one Spring bean property.

1.Bean class

When creating bean definitions, which are connected to a single particular type of application. The class itself is the main attribute of the bean.

When Spring locate dependencies, class property is the default identifier bean. Does this mean that you can not provide more bean defined as a single class? No, this is possible. But in this case, for the avoidance of doubt, you should use another bean identifiers: name a name.

2. Bean name name

Spring bean Spring name is used to identify the user-defined string bean. The bean class, the name must be unique within the entire application. You can not define two bean with the same name, even if they are of different types.

Fortunately, you do not have to set the name for each bean you create. Spring at runtime using its internally generated name. Unless you want to identify by name bean, otherwise you can safely use the default settings.

3. How named Spring bean?

Use @Bean annotation name attribute. This is an example having the same type of two bean.

@Configuration
class MyConfigurationClass {
 
 @Bean (name = "myBeanClass")
 MyBeanClass myBeanClass () {
 return new new MyBeanClass ();
 }
 
 @Bean (name = "anotherMyBeanClass")
 MyBeanClass anotherMyBeanClass () {
 return new new MyBeanClass ();
 }
 
}
In practice you do not often define the bean name. For a single type having a plurality of bean is quite rare case. However, named understand the possibilities if the bean is good.

4. Bean Dependencies

Bean object as its job may be performed using other bean. When Spring creates a certain dependency object definitions, frameworks need to create these dependencies. These dependencies can also have their own dependencies.

In object-oriented applications, we usually use a huge chart related objects. Fortunately, we do not have to consider how to build this figure. We do not consider the order should create an object. Spring for us to do all these.

Spring The only hope for you is dependent on a specific list of items the bean.

5. How to define bean dependence?

Bean dependency definition is a complex subject worthy of a separate article. Consider the following paragraphs as an introduction to the subject.

When you have a @Component labeled with a class constructor and only when, Spring use as an essential parameter list constructor dependency list. By default, frame constructor parameter types used to provide the appropriate object.

@Component
class BeanWithDependency {
 
 private final MyBeanClass beanClass;
 
 BeanWithDependency(MyBeanClass beanClass) {
 this.beanClass = beanClass;
 }
 
}
 

In the past, we used @Autowired comment on the constructor. However, starting with Spring 4.3, if only a constructor, it is not mandatory. However, if the bean class defines several constructors, use @Autowired a tag. So you know which constructor Spring bean that contains the list of dependencies.

For the same reason, bean factory method can define its dependencies. Spring uses the appropriate object method call.

@Bean
BeanWithDependency beanWithOptionalDependency(MyBeanClass beanClass) {
 return new BeanWithDependency(beanClass);
}

6. Bean scopes

Spring bean range defines the number of instances of a particular class at runtime to create the frame. Scope also describes the conditions for creating new objects.

Spring provides several scopes for your bean. There are two core framework:

Singleton - a single instance of
a prototype - multiple instances
addition, Spring also comes bean scope specifically for the Web application:

Requesting
session
global session
at the application level Application
for all bean default scope of a single embodiment. When a single embodiment scope bean, Spring create and share only one instance of it throughout the application. Singleton is perfect stateless object. Today, the vast majority of our applications are stateless bean singleton.

On the other hand, if the object contains the state should consider other scopes. To choose the right one, you should ask yourself this framework should state how long to keep in memory. But that is another article.

7. How to set the scope?

Whether using direct comments @Component class or use @Bean create factory method is the same. Its use @Scope annotation string attribute selection.

@Component
@Scope ( "the prototype")
class MyPrototypeClass {
 // ...
}
@Bean
@Scope ( "the prototype")
MyPrototypeClass myPrototypeClass () {
 return new new MyPrototypeClass ();
}
More importantly, the scope for the Web, Spring It comes with additional alias comment. You can use these comments instead of the @Scope:
@RequestScope
@SessionScope
@ApplicationScope

Bean initialization mode
when your application starts, Spring will create all singleton bean immediately at startup. This default behavior allows us to quickly detect errors in the bean definition. On the other hand, immediately start of eager bean initialization makes the application slower.

Fortunately, you can create the bean delay to the time actually needed. You can do this using @Lazy comment.

@Component
@Lazy
class MyLazyClass {
 //...
}

Bean initialization callback
once the Spring bean definitions according to create a new instance, you might want to run some object initialization logic.

If this logic does not depend on the frame, it can run in the constructor object. However, to ensure run after Spring initialization logic objects (for example, after an optional dependency injection), you should use initialization callback.

How to set the bean initialization callbacks?
If you use @Component defined bean, you have two options:

The bean class implements InitializingBean. Interface forces you to realize initialization method.

Writing custom initialization method using javax @PostContruct notes are marked.

In either case, Spring will run the initialization callback for you.

How to use the factory methods defined bean?

You can use its properties @Bean setting named initMethod initialization callback. This property requires a string having the name of the initialization method.

@Bean (the initMethod = "someInitMethodName")
MySpringBeanClass meBeanClass () {
 return new new MySpringBeanClass ();
}
Interestingly, as the method of initialization callback may have private access control. Spring uses reflection to invoke the method.

Bean destroy callback

And initialization callback is similar to the destruction of Spring bean method should call you can define. Use Predestroy callback much less, but pay attention to their existence is very good.

How to set bean destroy callback?

Similarly, if you have access to the source code of the bean class, you can use one of the following two options:

DisposableBean implement the interface. The only way to Spring using its destruction callback.

Write custom methods and use Javax API @PreDestroy comments in it.
For the factory method, and destroyMethod @Bean annotation properties.
@Bean (name = "myBeanClass", and destroyMethod = "cleanUpMethod")
MySpringBeanClass meBeanClass () {
 return new new MySpringBeanClass ();
}

Spring define how to create objects from the bean?
When you start the Spring application framework first creates a special object called the ApplicationContext. The ApplicationContext, also known as inversion of control (IoC) container, the core framework.

ApplicationContext object is present bean container.

Responsible for detecting and reading the context Spring bean definitions. Once loaded ApplicationContext bean definitions, it can be provided according to bean properties and their dependencies to start creating bean object for the application.
 

Published 184 original articles · won praise 32 · views 110 000 +

Guess you like

Origin blog.csdn.net/wjandy0211/article/details/103762167