Java Development Engineer (web direction)-04.Spring Framework-2nd Chapter. IOC container

Source: Internet
Author: User

2nd Chapter. IOC containerIOC Container Overview

Abstract: Introduction to the usefulness and use of IOC and beans

The IOC container is located at the core of the entire spring frame: core Container:beans, Core, Context, Spel

Provide support for Upper aop/aspects/instrumentation/messaging

Use of the IOC container:

Create dependencies on objects and finally assemble them into desired business objects

Containers generate available objects through business objects and configurations (Application-context.xml; xxxcontroller.java)--

In the spring scenario, the object a/b/c is called the bean

From a code perspective, an ApplicationContext (under the Org.springframework.context package, which belongs to the Spring-context dependency) is an IOC container

Initialize the IOC container:

In Web project XML, use the

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath:application-context.xml</param-value>

</context-param>

ApplicationContext context = new Classpathxmlapplicationcontext ("Application-context.xml");

(In File system: ApplicationContext context = new Filesystemxmlapplicationcontext ("/home/user/conf/application-context.xml") ;)

To initialize.

Define a bean:

In the Application-context.xml

<beans>

...

<bean id= "screwdriver" class= "Package_url" ></bean>

</beans>

Using beans:

1. Initialize the container: applicationcontext context = new Classpathxmlapplicationcontext ("Application-context.xml");

2. Get the object by Getbean (): Screwdriver screwdriver = Context.getbean ("screwdriver", screwdriver.class);

3. Objects used: Screwdriver.use ();

Example Walkthrough:

1. New Maven Project, Create a simple project (skip archetype selection)

GroupId:com.netease.nanodegree

Artifactid:spring-container

2. Add the required dependencies to the Pom.xml

<Dependencies>     <Dependency>        <groupId>Org.springframework</groupId>        <Artifactid>Spring-context</Artifactid>        <version>4.2.1.RELEASE</version>    </Dependency></Dependencies>

Discover dependent jar packages are automatically added

3. Create a new application-context.xml under src/main/resources/

The initial is:

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:context= "Http://www.springframework.org/schema/context"xmlns:p= "http://www.springframework.org/schema/p"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd " >    </Beans>

At this point, no beans have been defined, so no beans can be fetched from the container

4. Create a new class Screwdriver.java under src/main/java/

Package:com.netease.nanodegree;

 Package Com.netease.nanodegree;  Public class Screwdriver {    publicvoid  ues () {        System.out.println (' Use screw Driver ");}    }

5. With the bean at this point, add the definition of the relevant bean in the Application-context.xml

<Bean id = "screwdriver" class = "Com.netease.nanodegree.ScrewDriver"></ Bean>

6. Create a new test class under Src/main/java/com.netease.nanodegree Testcontainer.java

7. Run as--Java application

Bean scope: The effective range of the bean

Singleton: A bit like the singleton in design mode

Ensure that the object can have at most one singleton in the entire container, and that only one instance will be created throughout the life cycle

The default scope of the bean definition in the example above is singleton

You can also explicitly define <bean id= "screwdriver" class= "Com.netease.nanodegree.ScrewDriver" scope= "Singleton" ></bean>

Instance test:

Screwdriver.java

 Public class Screwdriver {        private String color = "Red";      Public void Use () {        //System.out.println ("Use screw driver");        SYSTEM.OUT.PRINTLN ("use" + Color + "Screw driver");    }          Public void setcolor (String color) {        this. color = color;    }} 

Testcontainer.java

To test whether it is a singleton object, use Getbean () to get two objects and set the property to "green", and if it is a singleton, the properties of two objects are "green"

Prototype

appear in Web scenarios:

Request

Global session

Session

Application

Dependency Injection

Java Development Engineer (web direction)-04.Spring Framework-2nd Chapter. IOC container

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.