1. Create a new Java project
2. Add spring-related jar packages and add them to the reference
3. Add spring dependent jar packages and add them to the reference
4. Write Spring configuration file Applicationcontext.xmlCreate a new Applicationcontext.xml file, and then write the header file. For the spring header file, refer to the guide provided by spring. Unzip the downloaded spring file to finddocs\spring-framework-reference\html\index.htmland open, and then find in the open page there will be a header file related to the following is the written applicationcontext.xml file.
<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "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" > <BeanID= "User"class= "domain. User ">< Propertyname= "Name"value= "Fei"/></Bean></Beans>
5, write the test program run SpringFirst, write a test of the entity class user
Public classUser {PrivateString name; PublicString GetName () {returnname;} Public voidsetName (String name) { This. Name =name;} PublicUser (String name) { This. Name =name;} PublicUser () {} Public voidSayname () {System.out.println (name);}}Ii. writing the test file method one: Using the Spring test framework
@RunWith (Springjunit4classrunner. Class)// Use Spring's test framework @ContextConfiguration ("Classpath:applicationContext.xml")// Load the spring configuration file and load the configuration files placed in other folders:/config/applicationcontext.xmlpublicclass springtest { privatepublicvoid test1 () { User.sayname (); } }
Mode two: Load the spring configuration file with Classpathxmlapplicationcontext to implement ApplicationContext
Public class SpringTest02 { privatestaticstatic { new classpathxmlapplicationcontext ("Classpath:config/applicationcontext.xml" Public void test1 () { Ctx.getbean ("user", user. Class). Sayname (); }}
Spring Learning Note 1--spring Build