2014-12-15

Source: Internet
Author: User

Spring, as an open-source framework, has an indispensable place in today's Java development. Start learning Spring Today


The rack package required by spring.

The first is the toolkit that spring needs to use, the others are spring's own package, and today's video Spring 4.0 should not need to org.springframework.asm this package. However, 3.0 will cause classnotfoundexception anomalies.

First, we need to create a spring configuration file: Applicationcontext.xml.

<?xml version= "1.0" encoding= "UTF-8"? ><beansxmlns= "Http://www.springframework.org/schema/beans" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:p= "http://www.springframework.org/schema/p" xsi: schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-3.0.xsd "><bean id=" HelloWorld "class=" Com.spring.Bean.HelloWorld "><property name=" name " Value= "Spring" ></property></bean></beans>

The Bear ID gets the name of the bean for the IOC container, and class points to JavaBean.

The property name corresponds to the method in JavaBean, and value is an assignment.

public static void Main (string[] args) {/*//creates an object for HelloWorld HelloWorld hello = new HelloWorld ();//assigns a value to the Name property Hello.setna Me ("ssss"); *///1. Create a Spring IOC container object ApplicationContext CTX = new Classpathxmlapplicationcontext (" Applicationcontext.xml ");//2. Get the bean instance from the IOC HelloWorld hello = (HelloWorld) ctx.getbean (" HelloWorld ");//3. Call the Hello method Hello.hello ();}

The steps to call spring first we need to create a spring container, and then we get our configured bean instance through the spring container and finally call the method in the bean. Equivalent to the previous instance of new.

The IOC's role is to automatically help you create a container object with a reflection mechanism, without having to go to the new object yourself.

Spring verifies the configuration of each bean in the container when it is created, including verifying that the attributes referenced by those beans point to a valid bean. The Bean's properties are not set until the bean is actually created. As the bean is actually created, the dependent bean that is the bean and the dependent bean that relies on the bean are also created and allocated.

At the same time, spring's dependency injection method does not really have this attribute in the class file to do the dependency injection, but rather check that there is no Setxxx method to determine if there is no such attribute, but that is to say that spring does not have this attribute, and get, is judged by the get and set methods.

2014-12-15

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.