The first program of Spring, the first program of Spring

Source: Internet
Author: User

The first program of Spring, the first program of Spring

1. Development steps (Overview)

(1). Prepare the jar package

(2) Develop the HelloWorld Program

(32.16.complete configuration in applicationcontext.xml(xsd-config.html)

(4) Start the container, obtain the bean from the container, and call the method in the bean.

2. Steps

(1). jar package to be imported

Spring-core-3.2.0.RELEASE.jar

Spring-beans-3.2.0.RELEASE.jar

Spring-context-3.2.0.RELEASE.jar

Spring-expression-3.2.0.RELEASE.jar

Com.springsource.org. apache. commons. logging-1.1.1.jar

(2). Common POJO class

Package cn. wwh. www. spring. aaa. role of the helloworld;/*** class: *** @ author * @ version 1.0 * @ Creation Time: 03:22:26 */public class HelloWorld {public void show () {System. out. println ("Hello World! This is my first Spring! ");}}
(3) configure the helloworld. 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" xsi: schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <! -- Id: Unique Identifier of bean class: Full-qualified class name: it cannot be an abstract class or an interface. object obj = Class. forName ("cn. wwh. www. spring. aaa. helloworld. helloWorld "). newInstance (); --> <bean id = "helloworld" class = "cn. wwh. www. spring. aaa. helloworld. helloWorld "> </bean> </beans>


Reference in applicationContex. xml file

Helloworld. 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" xsi: schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <! -- Introduce other configuration files: Find files from the current path by default. you can include the path Prefix: classpath: indicates to find a file from the classpath path (recommended) file: indicates to find a file from the disk <import resource = "file: D: \ Users \ Administrator \ Workspaces \ MyEclipse 8.5 \ SpringExercise1 \ src \ cn \ wwh \ www \ spring \ aaa \ helloworld. xml "/> Note: Only the Resource interface can recognize and parse this Prefix:/SpringExercise1/src/cn/wwh/www/spring/aaa/helloworld. xml --> <import resource = "classpath: cn/wwh/www/spring/aaa/helloworld. xml "/> </beans>
(4). Test Cases

Package cn. wwh. www. spring. aaa. helloworld; import org. junit. test; import org. springframework. beans. factory. beanFactory; import org. springframework. beans. factory. xml. xmlBeanFactory; import org. springframework. core. io. classPathResource; import org. springframework. core. io. resource;/*** class function: *** @ author a leaf boat * @ version 1.0 * @ Creation Time: 03:25:12 */public class TestHelloWorld {// 1. call @ Testpublic void testOld () {// create an object HelloWorld hello = new HelloWorld (); hello. show () ;}// call @ Testpublic void testIoC () {// 1 using the Reverse Control Method of the Spring framework. load applicationContext. xml file: Find Resource resource = new ClassPathResource ("applicationContext. xml "); // 2. create and obtain the Spring container BeanFactory bean = new XmlBeanFactory (resource); // 3. obtain the specified bean. (Note that the parameter in getBean is hellowrld. the id in the bean label in the xml file) HelloWorld hello = (HelloWorld) bean. getBean ("helloworld"); // 4. operate beanhello. show ();}}

Effect Test diagram:


3. Note: 1. Put my resources folder under the project directory. It is the same directory as src, And the applicationcontext. xml file is placed in this folder.

2. Introduce other configuration files:
By default, files are retrieved from the current path.
You can include the path Prefix:
Classpath: Indicates finding a file from the classpath path (recommended)
File: searches for files from the disk.
<Import resource = "file: E: \ Users \ Administrator \ Workspaces \ MyEclipse 8.5
\ SpringExercise1 \ src \ cn \ wwh \ www \ spring \ aaa \ helloworld. xml "/>
Note: Only the Resource interface can recognize and parse this Prefix:/SpringExercise1/src/cn/wwh/www/spring/aaa/helloworld. xml

3. Under the <bean> label in the helloworld. xml file:

Bean element id and name:
It is best to use id: name is used only in special cases.
The Bean id attribute in Spring development complies with the xml syntax ID constraints:
The name of an id must start with a letter to comply with the XML Naming rules for the ID attribute. You can use letters, numbers, hyphens, underscores, sentences, colons
Using the name attribute, you can use many special characters. It was integrated with spring in the early stage (SpringMVC)
<Bean name = "/login" class = "... LoginAction"/> the name contains/. An error is returned when the id is used.

4. From the perspective of Spring, the HelloWorld program looks at: Why does Spring create/manage Bean objects after configuration.

(1). Load the configuration file through the Resource object;

(2) parse the configuration file to obtain the bean;

(3). parse the bean and use the id as the bean name. class is used to reflect the bean instance (Class. forName (className ));

Note that in this configuration, all beans must have a non-parameter constructor.

(4). When getBean is called, the object instance is returned from the container.

Conclusion: the code is transferred from the JAVA file to the XML file.

5. Three Methods of the getBean method:
(1). Retrieve beans by type:
World = factory. getBean (HelloWorld. class );
Only one instance of this type needs to be configured in spring;
If multiple <bean/> are configured for this type of class, an error is returned: -- multiple objects can be found for one type.

(2). Obtain the bean by bean Name:
World = (HelloWorld) factory. getBean ("hello2 ");
Bean naming is not secure;

Exception:

Org. springframework. beans. factory. NoSuchBeanDefinitionException: No bean named 'helloworld' is defined:
The id or name of a <bean/> element cannot be found in the container, which is helloWorld.

(3). By name and type: (recommended)
World = factory. getBean ("hello", HelloWorld. class );

Exception:

Org. springframework. beans. factory. NoSuchBeanDefinitionException:
No unique bean of type [cn. itcast. cd. day1. _ 1hello. HelloWorld] is defined:
Invalid Bean type error:







The first program integrated by struts and spring has the following exceptions.

Your spring has fewer packages. spring needs to check whether the input of form data is empty and then call a method. Then the method is not found. An error occurs. Blog.sina.com.cn/s/blog_4575678801009psk.html describes this method in detail.

How does spring obtain the context of the created application?

If there are several methods in the web project, I think this method is the most convenient.
Create classes to implement the spring ApplicationContextAware interface, that is
Public class SpringContextTool implements ApplicationContextAware {
Private static ApplicationContext context;
Public void setApplicationContext (ApplicationContext acx ){
Context = acx;
}

Public static ApplicationContext getApplicationContext (){
Return context;
}
}
Then define the bean in spring. The id is given at will. SpringContextTool. getApplicationContext () can be obtained directly.

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.