The construction of spring and the use of JUnit

Source: Internet
Author: User

Friends who need spring jar plus QQ2393469413

1. Download the jar package, create a Web project with MyEclipse, and then copy and paste the jar package into Lib in the project's Web-inf directory, and the new IDE will automatically load the jar.

2, then create a org.util.test package (name can be arbitrary), and then create a class in the package Unittestbase (using JUNIT4 to test the base class, the other test classes will inherit the class). This class is used to load spring-related configuration information and is not created without using the JUnit tool for testing

3, the specific code of the Unittestbase class

 Packageorg.util.test;Importorg.springframework.beans.BeansException;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;Importorg.springframework.util.StringUtils;Importorg.junit.*; Public classUnittestbase {PrivateClasspathxmlapplicationcontext context; PrivateString Springxmlpath;//is the path string that configures the spring bean container, which is obtained through the constructor         Publicunittestbase () {} Publicunittestbase (String springxmlpath) { This. Springxmlpath =Springxmlpath; } @Before Public voidbefore () {if(Stringutils.isempty (Springxmlpath)) {/*** classpath* is the operation under all jar packages under the current jar directory, * such as scanning, etc., classpath is only the operation of the current single jar package.             For example, in the scanner, * classpath only scan the current package of class,classpath* is scanned in the current package directory of all the packages in the class. */Springxmlpath= "Classpath*:spring-*.xml"/*spring-* represents spring's bean configuration file, can be arbitrarily named, * denotes any matching character */}Try{            /*** Str.split (String regex) function: the string str, * is divided into a string array according to the regular expression regex.             "[, \\s]+" is a regular expression, \\s represents a variety of whitespace characters, + means matching more than one.)             * StringUtils is a tool class for manipulating Java.lang.String under Org.apache.commons.lang, * Use may need to be imported manually Commons-lang-xx.jar * Split the Springxmlpath path, * because the Springxmlpath path may be a concatenation of multiple paths, the XML under each path will be scanned for recognition after splitting*/Context=NewClasspathxmlapplicationcontext (Springxmlpath.split ("[, \\s]+")); Context.start ();//get and start a container by scanning an XML file}Catch(beansexception e) {e.printstacktrace (); }} @After Public voidAfter () {Context.destroy (); } @SuppressWarnings ("Unchecked")    protected<textendsObject>T Getbean (String beanid) {/*** T This is generic, when you are not sure what type parameters to use, generics can represent any type of parameters, more flexible and convenient*/        return(T) Context.getbean (Beanid); }    protected<textendsObject> T Getbean (class<t>clazz) {        returnContext.getbean (Clazz); }}

4. Create a org.bean.example package, and then create the following Oneinterface interface

1  Package org.bean.example; 2 3  Public Interface oneinterface {4     5      Public string Save (string s); 6 }

5, create the Oneinterface interface implementation class Oneinterfaceimp class

 Package org.bean.example;  Public class Implements oneinterface{    @Override    public  String Save (string s) {        System.out.println ( "Hello Oneinterfaceimp");         return s;    }}

6. Select src Right-click to create a Spring-ioc.xml file, which is a spring bean configuration file, and the file name can be created by itself. The header information of the XML file, MyEclipse will not be automatically generated need to be added manually, you can add the following code

<?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:p="/http/ www.springframework.org/schema/p "    xsi:schemalocation=" Http://www.springframework.org/schema/beans Http://www.springframework.org/schema/beans/spring-beans-3.1.xsd Http://www.springframework.org/schema/tx http:/ /www.springframework.org/schema/tx/spring-tx.xsd "xmlns:tx=" Http://www.springframework.org/schema/tx "><!- -Above is the header information of XML--! >
<!--The following is the configuration information for bean Properties--! >
    class= "Org.bean.example.OneInterfaceImp" > </bean></beans>

7, finally create a test class Testoneinterface.java, the class must inherit the beginning of the Unittestbase.java

 Packageorg.bean.example;Importorg.junit.Test;ImportOrg.junit.runner.RunWith;ImportOrg.junit.runners.BlockJUnit4ClassRunner;Importorg.util.test.UnitTestBase; the @RunWith (Blockjunit4classrunner.class) Public classTestoneinterfaceextendsunittestbase{ PublicTestoneinterface () {Super("Classpath*:spring-ioc.xml"); }//@Test annotations and annotations on the class must be written, which is a way for JUnit to test @Test Public voidTesthello () {oneinterface oneinterface=Super. Getbean ("Oneinterface"); System.out.println (Oneinterface.save ("Hello Spring")); }    }

8, in the Testoneinterface.java

If you have the following results, you can build a successful

The construction of spring and the use of JUnit

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.