1: the first step, the introduction of the package, after all, the use of the framework it! First introduce the 5 required packages as Follows:
Commons-logging-1.1.3.jar Log
Spring-beans-3.2.5.release.jar Bean Node
Spring-context-3.2.5.release.jar Spring Context Node
Spring-core-3.2.5.release.jar Spring Core Features
Spring-expression-3.2.5.release.jar Spring expression-related Tables
2: Core Configuration File:
(1): Spring config file: applicationcontext.xml/bean.xml
(two names can be, generally use APPLICATIONCONTEXT.XML)
(2): How to configure the constraints of the core configuration file, here is a method:
The Applicationcontext.xml configuration template looks like This:
<?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"Xmlns:context= "http://www.springframework.org/schema/context"xsi:schemalocation= "http://www.springframework.org/schema/beans Http://www.springframework.org/schema/beans/spring-beans. XSD Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-conte Xt.xsd "> </Beans>
The Applicationcontext.xml development configuration is as Follows:
1 <?XML version= "1.0" encoding= "UTF-8"?>2 <Beansxmlns= "http://www.springframework.org/schema/beans"3 Xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"4 xmlns:p= "http://www.springframework.org/schema/p"5 Xmlns:context= "http://www.springframework.org/schema/context"6 xsi:schemalocation="7 Http://www.springframework.org/schema/beans8 http://www.springframework.org/schema/beans/spring-beans.xsd9 Http://www.springframework.org/schema/contextTen http://www.springframework.org/schema/context/spring-context.xsd "> one a - <!--The configuration of the IOC container, all objects to be created are configured here - - <BeanID= "user"class= "com.bie.po.User"></Bean> the - </Beans>
3: Create an entity class, such as User.java:
1 packagecom.bie.po;2 /** 3 * @authorBiehongli4 * @versioncreated: March 12, 2017 pm 5:45:065 * 6 */7 public classUser {8 9 Private intid;Ten PrivateString name; one public intgetId () { a returnid; - } - public voidSetId (intId) { the this. ID =id; - } - publicString getName () { - returnname; + } - public voidsetName (String Name) { + this. Name =name; a } at - -}
4: testing with junit, as shown below, can be done in either of two ways, and the second is recommended:
1 packagecom.bie.hello;2 3 Importorg.junit.Test;4 Importorg.springframework.beans.factory.BeanFactory;5 Importorg.springframework.beans.factory.xml.XmlBeanFactory;6 Importorg.springframework.context.ApplicationContext;7 Importorg.springframework.context.support.ClassPathXmlApplicationContext;8 Importorg.springframework.core.io.ClassPathResource;9 Importorg.springframework.core.io.Resource;Ten one Importcom.bie.po.User; a - /** - * @authorBiehongli the * @versioncreated: March 12, 2017 pm 5:46:23 - * - */ - public classUsertest { + - @Test + public voidSpringhello () { a //previous old ways to create objects at //user User=new User (); - - //now the object is created to the IOC container. - //first step: load the configuration file -Resource resource=NewClasspathresource ("applicationcontext.xml"); - //Step two: Create a Container object (bean factory), IOC container =bean factory +aplicationcontext.xml inBeanfactory factory=Newxmlbeanfactory (resource); - //step three: Get the object created by the container toUser user= (user) Factory.getbean ("user"); + - System.out.println (user); the } * $ @TestPanax Notoginseng public voidtest2 () { - //first step: Get IOC container object theApplicationContext AC =NewClasspathxmlapplicationcontext ("applicationcontext.xml"); + //step two: Get the bean from the container aUser user = (user) Ac.getbean ("user")); the + System.out.println (user); - } $}
Spring's most bull x frame, I'm coming ~ ~ ~
Spring's Hello World (spring Introduction)