Spring Bean Definition and scope understand the basic concepts Spring Spring simple example

table of Contents:

  1. Learn the basics of Spring

  2. Spring simple example

Bean definition

Simply put Java Bean is an object managed by the Spring container, Spring container will automatically instantiate the Bean.

So what is the container it? If you have read Part Spring simple example.

Wherein the following codes:

ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");

context is a container, of course, this is not the most accurate official definition, but a preliminary study, we know that this is a container to instantiate an object on the line.

The second essay, the configuration file reads as follows:

<? XML Version = "1.0" encoding = "UTF-. 8" ?> 
< Beans xmlns = "http://www.springframework.org/schema/beans" 
       xmlns: the xsi = "http://www.w3.org / 2001 / the XMLSchema-instance " 
       the xsi: the schemaLocation =" http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd " > 

<! -     ID own name, class attributes that need to inject class -> 
    < the bean ID = "C" class = "the category" > 
<-!         name is the name of the attribute, value is injected into the value of the property -> 
        <property name="name" value="Hello Word"/>
    </bean>
</beans>

When the bean attribute list is defined as follows (taken from w3cschool):

Attributes description
class This property is mandatory and specified to create the bean's bean class.
name This attribute specifies the unique identifier bean. XML-based metadata configuration, you can use the ID and / or name attribute to specify the bean identifier.
scope This attribute specifies the object created by the special bean definitions of scope, it will be discussed in chapter bean in scope.
constructor-arg It is used dependency injection, and will be discussed in the next sections.
properties It is used dependency injection, and will be discussed in the next sections.
autowiring mode It is used dependency injection, and will be discussed in the next sections.
lazy-initialization mode Lazy Initialization of bean told IoC container when it is first requested, rather than to create a bean instance when it starts.
initialization method After all the necessary properties of the bean container is set, call the callback method. It will be discussed chapter in the life cycle of the bean.
destruction method When the container containing the bean is destroyed, using the callback method. It will be discussed chapter in the life cycle of the bean.

 

 

 

 

 

 

 

Bean scopes

The table above, scope attribute is used to specify the scope. And its value is described as follows:

Scope description
singleton

In the spring IoC container exists only one example Bean, Bean manner in the presence of a single embodiment, the default value

prototype Each time the call Bean, returns a new instance of the container, i.e., each call to the getBean (), equivalent to the implementation newXxxBean ()
request Each HTTP request will create a new Bean, that scope applies only to the environment WebApplicationContext
session Share a same HTTP Session Bean, different Session using different Bean, applies only to the environment WebApplicationContext
global-session Portlet applications for the general environment, the use of domain applies only to the environment WebApplicationContext

 

 

 

 

 

Here we have to write two common scope.

First, prepare a class that do not change

public  class the Category {
     // attributes 
    Private String name;
     // Set the attribute method 
    public  void the setName (String name) {
         the this .name = name; 
    } 
    // Get the attribute method 
    public  void getName () { 
        the System.out. the println (name); 
    } 
}

singleton scope:

Profiles:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<!--    id自己命名,class就是需要注入属性的类-->
    <bean id="c" class="Category" scope="singleton">
   </bean>
</beans>

Test category

public  class TestSpring {
     public  static  void main (String [] args) {
         // the Spring of API ApplicationContext. applicationContext.xml own configuration file is created 
        ApplicationContext context = new new ClassPathXmlApplicationContext ( "applicationContext.xml" );
         // c is the configuration file behind the above mentioned id 
        the Category category = (the Category) context.getBean ( "c" );
         // not in the configuration set in the file, by setting the value of the property set methods 
        category.setName ( "first set" ); 
        category.getName (); 
        // a second object 
        the Category Category1 = (the Category) context.getBean ( "C" );
         //Not set, direct access to the value of the property 
        category1.getName (); 
    } 
}

The end result is:

First set up
the first set

The output of the same content twice, indicating that the bean id is only 1 c, two return value are the same.

prototype scope:

Configuration file scope into a prototype. All other code unchanged, including the test class

Final output:

The first set
null

Description Returns the calling getBean each container is a new instance, the first set for the second object is invalid.

Guess you like

Origin www.cnblogs.com/lbhym/p/11898253.html