Default ScopeIn Spring IOC containers, there are two default bean scopes:
Singleton, the Bean's lifetime and container are synchronized once the bean is created, the bean is destroyed only when the container is closed, the bean with the same ID is instantiated only
The behavior of creating collaborative relationships between application objects is called assembly (wiring), which is also the nature of Di.How to assemble beans in spring
Spring provides three ways to assemble beans.
Implicit bean discovery mechanism and automatic assembly
Java Config
XML Config
Spring
( Mywebserverconfigurationproperties.class) public class mywebserverconfiguration { @Autowired private mywebserverconfigurationproperties properties; /** * Below you can refer to the configuration in the Mywebserverconfigurationproperties class */void setmyconfig () {String port = properties.getport (); Span class= "hljs-comment" >//.....} }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
The @enableconfigurationproperties
Spring Bean life cycleIn traditional Java applications, the Bean life cycle is very simple.Java's keyword new is used to instantiate a bean (perhaps he is a non-serialized). That's enough.Instead, the bean's life cycle is more granular in the spring container.Understanding t
OverviewIn spring, whether you use XML or annotations, you essentially provide the bean's definition information for the spring container. The three key factors that spring containers can successfully start are bean definition information, bean implementation classes, and
allows us to use the values in the external file to complete the injection.3. Environment API in springThis interface represents the running environment of the current running program and can retrieve properties directly from environment.(Detailed analysis reference: http://jinnianshilongnian.iteye.com/blog/2000183)4. Parsing attribute placeholdersPlaceholder form: "${disc.title}" (property name in parentheses)Parsing an external property enables you to defer processing of a value to the runtim
First, aware related interfaceFor applications, you should minimize the coupling to the Sping API, but sometimes it is necessary to let the bean know the details of how the spring container manages it, in order to take advantage of some of the features provided by spring, such as letting the bean know that the containe
• When configuring beans in a configuration file, it is sometimes necessary to mix the details of the system deployment in the bean's configuration (for example: file path, data source configuration information, etc.). And these deployment details actually need to be separated from the bean configuration
The Spring provides a propertyplaceholderconfigurer Beanfactory post processor that allows the user to
The content is referenced from the spring in action book.The behavior of creating collaborative relationships between application objects is often called assembly, which is also the essence of dependency injection.1. Create a spring configurationSpring is a container-based framework. If spring is not configured, then it is an empty container, so it is necessary t
. Create using constructors (common)Creating a Bean,bean using the constructor must have a default constructor2, the use of static Factory modeProvide a static factory:1 Public classFactorybean {2 /**3 * Must be a static method4 * @return5 */6 Public StaticUser CreateUser () {7 return NewUser ();8 }9}To configure:1 ID: uniquely identifies 2class : Static Factory clas
Transfer from http://elim.iteye.com/blog/2017466Execution order:Beanfactorypostprocessor.postprocessbeanfactoryUserBean ' s constructorUserBean ' s Username property setBeanpostprocessor.postprocessbeforeinitializationInitializingbean.afterpropertiessetBeanpostprocessor.postprocessafterinitializationSpring Bean processing--callback functionSpring defines three interfaces that can be used to process the beanfactory of a
The behavior of creating collaborative relationships between application objects is called assembly (wiring), which is also the nature of Di.How to assemble beans in springSpring provides three ways to assemble beans.
Implicit bean discovery mechanism and automatic assembly
Java Config
XML Config
Spring can choose one or more combinations of three ways. However, it is recommended to us
"Bean Configuration"To configure a bean in an XML file through a bean node1 2 Configuration Bean3 the full class name of the Class:bean, which creates the bean in the IOC container in a reflective manner, requires that the bean must have an argument-free constructor. 4 ID:
;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext; Public classTestspringxml { Public Static voidMain (string[] args)throwsperformanceexception {//assembling the Spring.xml configuration fileApplicationContext CTX =NewClasspathxmlapplicationcontext ("Spring.xml"); //The bean that gets the claim from the spring containerPerformer performer = (performer) Ctx.getbean ("Duke"); //methods
How to use the scope of spring:This scope is used to configure the scope of the spring bean, which identifies the scope of the bean.Before spring2.0 beans had only 2 scopes: Singleton (singleton), Non-singleton (also known as prototype), Spring2.0 later, added session, request, global Session three beans dedicated to the context of the Web application. Therefore, by default Spring2.0 now has five types of b
Beans that are managed by spring can be created directly, and can also be created through Factory mode. For example, the tracing bean definition for brave:1 BeanID= "Tracing"class= "Brave.spring.beans.TracingFactoryBean">2 ...3 Propertyname= "Propagationfactory">4 BeanID= "Propagationfactory"class= "Brave.propagation.ExtraFieldPropagation"5 Factory-method= "Newfactory">6 Constructor-argIndex= "
Scope of Spring Bean
Scope
Description
Singleton
A bean definition corresponds to an object instance in each spring IOC container.
Prototype
A bean definition corresponds to multiple objec
The spring container does not have special requirements on bean, or even does not require this bean image.Standard JavaBeanThe corresponding getter and setter methods must be provided for each attribute. Bean in spring is a Java instance and Java component. In traditional Ja
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.