[Spring tutorial] create a simple spring example, spring Tutorial example
1. First, the main idea of spring is dependency injection. Simply put, there is no need to manually create new objects, and these objects are managed by the spring container in a unified manner.
2. EXAMPLE STRUCTURE
As shown in, the maven project is used.
2. pom. xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>SpringExample001</groupId> <artifactId>SpringExample001</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>3.2.5.RELEASE</version> </dependency> </dependencies></project>
3. spring. xml
<?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" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> <bean id="do" class="com.test.pro.Do"/></beans>
4. Do. java
package com.test.pro;public class Do {public void speaking(){System.out.println("speaking.......");}}
5. Testing
package com.test.pro;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Main {public static void main(String[] args) {// TODO Auto-generated method stubApplicationContext ctx=new ClassPathXmlApplicationContext("spring.xml");Do did=(Do)ctx.getBean("do");did.speaking();}}
6. Output
7. Analysis
We can see that spring in the core spring configuration file. there is only one sentence in xml: <bean id = "do" class = "com. test. pro. do "/>, which indicates that there is a bean file named do and Its Class address is com. test. pro. do.
Then there is a paragraph in our test class:
ApplicationContext ctx = new ClassPathXmlApplicationContext ("spring. xml ");
Do did = (Do) ctx. getBean ("do ");
Did. speaking ();
This indicates that a context class is declared. This context class loads the configuration file. Note that if the maven project is not used here, pay attention to spring. if you are not sure about the relative address of xml, you can use the absolute address method, for example:
ApplicationContext ctx = new ClassPathXmlApplicationContext ("file: H:/spring. xml ");
The context object is used to obtain the bean declared in the configuration file and can be called directly.
So when the bean file is instantiated, if the bean scope is prototype, then the Bean instantiation is the first time to use the Bean instantiation, can refer to this article: http://www.iteye.com/problems/93479