Spring Learning Note 1--spring Build

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.