"Three big SSH framework" Spring Basics First: Building the spring environment, instantiating beans, managing the scope of beans, and the bean's life cycle

Source: Internet
Author: User

First, build the spring environment:

With the introduction of the jar package in the Lib directory and add to path, it is not too much to say.

Ii. three ways to instantiate a bean:

First, let's start by writing two Java classes:

Interface class:

Public interface Personservice {public abstract void Save ();

Implementation class:

public class Personservicebean implements Personservice {@Overridepublic void Save () {System.out.println ("I Am the Save () method") ;}}

Write a test method again:

public class Springtest {@BeforeClasspublic static void Setupbeforeclass () throws Exception {} @Testpublic void Instancesp Ring () {ApplicationContext ctx = new Classpathxmlapplicationcontext ("Beans.xml"); Personservice Personservice = (personservice) ctx.getbean ("Personservice");p Ersonservice.save ();}}

1. Instantiating a bean using the class constructor

If we instantiate the bean using the class constructor, we need to configure the Beans.xml file:

<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:context= "Http://www.springframework.org/schema/context"        xsi:schemalocation= "Http://www.springframework.org/schema/beans        Http://www.springframework.org/schema /beans/spring-beans-2.5.xsd        http://www.springframework.org/schema/context/        http Www.springframework.org/schema/context/spring-context-2.5.xsd ">         <bean id=" Personservice "class=" Cn.itcast.service.impl.PersonServiceBean "></bean></beans>
The ID is the unique identifier of the bean, and class is the path that the bean points to.

We run the Instancespring method in the test class, which can be printed out in the console:

I am the Save () method

2. Using a static factory to instantiate a bean

First, we build a factory class, where one method is static:

public class Personservicebeanfactory {public static Personservicebean Createpersonservicebean () {return new Personservicebean ();}}
Then we need to configure the Beans.xml file:

<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:context= "Http://www.springframework.org/schema/context"        xsi:schemalocation= "Http://www.springframework.org/schema/beans        Http://www.springframework.org/schema /beans/spring-beans-2.5.xsd        http://www.springframework.org/schema/context/        http Www.springframework.org/schema/context/spring-context-2.5.xsd ">         <bean id=" Personservicefactory "class= "Cn.itcast.service.impl.PersonServiceBeanFactory" factory-method= "Createpersonservicebean" ></bean>  </beans>
The ID is the unique identifier for this bean, and class indicates a pointer to a factory-method that uses a method in the factory method.

We run the Instancespring method in the test class, which can be printed out in the console:

I am the Save () method

3. Instantiate the bean using the instance factory method

There are two beans that need to be configured to use an instance factory:

Let's configure the Beans.xml file:

<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:context= "Http://www.springframework.org/schema/context"        xsi:schemalocation= "Http://www.springframework.org/schema/beans        Http://www.springframework.org/schema /beans/spring-beans-2.5.xsd        http://www.springframework.org/schema/context/        http Www.springframework.org/schema/context/spring-context-2.5.xsd ">         <bean id=" Personservicefactory "class= "Cn.itcast.service.impl.PersonServiceBeanFactory" ></bean>  <bean id= "Personservice" factory-bean= "Personservicefactory" factory-method= "Createpersonservicebean" ></bean></beans>
As you can see, we configured two beans, the second bean referenced the first bean through Factory-bean, and called Factory-method

Let's take a look at Personservicebeanfactory.java:

public class Personservicebeanfactory {public Personservicebean Createpersonservicebean () {  //note, There is a static      return of New Personservicebean () in relation to the Quiescent factory;}  }
It is important to note that the method to be called in the class to which the called Bean is directed cannot be static because this is not a static factory instantiation.

We run the Instancespring method in the test class, which can be printed out in the console:

I am the Save () method

Third, the scope of the management bean

There is a property in spring beans scope is used to configure the scope of the spring bean, which identifies the scope of the bean

There are five types of beans by default after Spring 2.0:

Singleton, prototype, request, session, global session (application)

Of these, the last three are bean objects for the web, and only the first two are described here.

PS: One thing to note is that if the bean does not have a scope set, the default is singleton type

1,. Singleton

When the scope of a bean is set to the. singleton,srping IOC container, only a shared bean instance exists.

In all requests to the bean, the instance they return is the same as long as the ID of the request is the same as the Bean's ID.

That is, when set to Singleton, the container will only create a unique instance. This single instance is stored in the singleton cache (singleton cache), and all subsequent requests and references to the bean will return the cached object instance.

2,.prototype

When the bean's scope is set to prototype, the Spring IOC container creates a new bean instance each time the bean is requested.

Iv. Scope of the bean

1, when the scope of the Bean is a singleton (scope= "singleton" or no scope attribute)

When the container is instantiated, the bean is instantiated at the same time.

In addition, if the Bean's Lazy-init property is set to True, the initialization is deferred, that is, the bean is initialized when the Getbean () method is called

2, when the scope of the Bean is (scope= "prototype")

The bean is instantiated when the Getbean () method is called.


There are several properties for all bean instances, the Init-method method and the Destroy-method method.

The Init-method method is to call this method after instantiating the bean (this is achieved through the IOC container reflection technique)

The Destroy-method method is to call this method after the container is closed











SSH three frames Spring Basics first: Building a spring environment, instantiating beans, managing bean scopes, and the bean's life cycle

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.