Spring two things (1), spring
Let's give a brief introduction to spring. On the one hand, let's get started with new users, and on the other hand, let's repeat them.
Maven is not used for the first small factory, and maven is used for configuration next time.
Only one jar package is required. The spring version is 2.5 (2.5 for the time being, and the basic core is the same for subsequent updates). Introduce spring. jar to lib as follows:
Create the applicationContext. xml file under src. The result is as follows:
The content is as follows:
<? 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/sprin G-context-2.5.xsd "> <! -- Put a class into the spring container. This class is called bean --> <! -- Describe a class id that uniquely identifies the class name --> <bean id = "helloWorld" class = "com. lee. spring001.createobject. HelloWorld"> </bean> </beans>
Create a new class HelloWorld. java under the com. lee. spring001.createobject package:
package com.lee.spring001.createobject;public class HelloWorld { public HelloWorld() { System.out.println("HelloWorld..."); } /** * */ public void hello() { System.out.println("Hello world!"); }}
Test:
1 package com.lee.spring001.createobject; 2 3 import org.junit.Test; 4 import org.springframework.context.ApplicationContext; 5 import org.springframework.context.support.ClassPathXmlApplicationContext; 6 7 public class HelloWorldTest { 8 9 @Test10 public void testHelloWorld() {11 12 ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");13 15 HelloWorld hello = (HelloWorld)context.getBean("helloWorld");16 hello.hello();17 }18 }
The junit operation is successful.
Github address: https://github.com/leechenxiang/maven-spring001-helloworld