Java Personal Learning Note: Spring container

Source: Internet
Author: User

To configure a bean in an XML file through a bean node

<bean id= "hellowordl" class= "Com.cy.test.HelloWorld" ></bean>

Name of the Id:bean

Must be unique within the IOC container.

If the ID is not specified, spring automatically assigns the privilege class name to the Bean.

ID can specify more than one name, with a good, a few, or a space separated.

Spring IOC Container

Before the IOC container reads the bean configuration to create the bean instance. It must be instantiated. The instance of the bean can be obtained from the IOC container and used only after the container is instantiated.

Spring provides two types of IOC container implementations.

Basic implementation of the Beanfactory IOC container

ApplicationContext provides more advanced features and is a sub-interface of the beanfactory.

Beanfactory is the infrastructure of the spring framework, facing spring itself.

ApplicationContext for developers using the spring framework, the ApplicationContext binary is used directly in almost all applications Beanfactory

The main implementation class of ApplicationContext

Classpathxmlapplicationcontext: Loads the configuration file from the classpath.

Filesystemxmlapplicationcontext: Loads the configuration file from the file system.

Configurableapplicationcontext: Extension with ApplicationContext new two main methods refresh () and close ()/in Appplicationcontext with startup, The ability to refresh and close the context.

ApplicationContext instantiates all singleton beans when initializing the context

Webapplicationcontext is intended for Web applications and allows initialization to be done from a path relative to the Web root directory.

Get the bean from the IOC container

Getbean () method of ApplicationContext;

The way of dependency injection

Attribute Injection

Constructor injection

Factory method Injection (rarely used, not recommended)

Attribute Injection

Property injection is a setter method that injects the Bean's property value or the object since

Attribute injection uses the <property> element, using the Name property to specify the Bean's property name, value or the <value> child node to specify the property value,

<bean id= "HelloWorld" class = "Cy.test.HelloWorld" >

<property name= "UserName" value= "test" ></property>

</bean>

Construction injection

By constructing a method to inject the bean's properties or dependent objects, it guarantees that the bean instance can be used after it is instantiated.

constructor injection in the <constructor-arg> element declaration attribute, no name attribute.

referencing other beans

The beans that make up the application often need to collaborate with each other to complete the functionality of the application, and to enable the bean to access each other, you must specify a reference to the bean in the bean configuration file.

In the bean configuration file, you can refer to the bean's properties or construction parameters through the <ref> element or ref attribute.

You can also include the Bean's declaration in the property builder. Such a bean is called an internal bean.

<bean id= "service" class= "Com.cy.test.service" ></bean>

<bean id= "Action" class= "Com.cy.test.action" >

<property name= "service" ref= "service" ></property>

</bean>

Internal Bean

When a bean instance is used only for a specific property, it can be declared as an internal bean inner bean declaration directly contained in the <property> or <construct-arg> element. You do not need to set any ID or Name property. Internal beans cannot be used anywhere else. (no ID or name reference)

Collection Properties

Collection properties can be configured in spring through a set of built-in XML tags <list><set><map>

Java Personal Learning Note: Spring 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.