[Spring] analyzes the establishment of the Spring framework and the spring framework
Directory structure:
Contents structure [-]
ReferencesWhat is Spring?
Spring is a container framework used to configure beans and maintain the relationship between beans.
Structure Diagram of Spring:
Simple Spring framework setup Demo
My structure directory:
Create a User class
package com.server;public class User { private String name;public void getName() { System.out.println("Hello "+name);}public void setName(String name) { this.name = name;}}
Create a Test classPackage com. test;/** introduce ApplicationContext and ClassPathXmlApplication */import org. springframework. context. applicationContext; import org. springframework. context. support. classPathXmlApplicationContext; import com. server. user; public class Test {public static void main (String [] args) {ApplicationContext ac = new ClassPathXmlApplicationContext ("applicationContext. xml "); User user = (User) ac. getBean ("user"); // identifies the user based on the Bean id. getName ();}}
Applicationcontextis an interface. classpathxmlapplicationcontextsearches for applicationcontext. you can find an index.html webpage file in the development kit. You can find the API documentation of the corresponding Spring version.
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
Create an applicationContext File<? Xml version = "1.0" encoding = "UTF-8"?> <Beans xmlns = "http://www.springframework.org/schema/beans" xmlns: context = "http://www.springframework.org/schema/context" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: aop = "http://www.springframework.org/schema/aop" xmlns: tx = "http://www.springframework.org/schema/tx" xmlns: p = "http://www.springframework.org/schema/p" xmlns: util = "http://www.springframework.org/schema/util" xmlns: jdbc = "h Ttp: // www.springframework.org/schema/jdbc "xmlns: cache =" http://www.springframework.org/schema/cache "xsi: schemaLocation =" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springfram Ework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http: // ww Export springframework.org/schema/util/spring-util.xsd "> <! -- Configure bean (service/dao. domain/action/Data Source) in the container file; --> <! -- Bean is used to automatically create a bean object and put it into the memory when our Spring framework is loaded. Id is the identifier, and the class specifies the path. In this example, it is equivalent to: User user = new User (); user. setName ("" chauncy); --> <bean id = "user" class = "com. server. user "> <property name =" name "> <value> chauncy </value> </property> </bean>
</Beans>
References
Http://blog.csdn.net/zoutongyuan/article/details/27073683
This article is an original blog article. If you need to reprint it, please indicate the source.