Spring-----Annotation Development and Spring test unit

Source: Internet
Author: User

I. Development of annotations
    • import jar package; Spring-aop-xxx.jar
    • Import constraint: (available in Official document Xsd-configuration.html)
       <  beans  xmlns  = "Http://www.springframework.org/schema/beans"   Xmlns:xsi  = "Http://www.w3.org/2001/XMLSchema-instance"   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-context.xsd " >  

       

    • Configuring the scan switch in XML:

<context:component-scan base-package= "Com.itheima"/>

    • 1. IOC annotations
/* 1. @Component component, which indicates that you want to host this class, let spring create an instance of this class. The US in parentheses is actually the ID identifier  2. @Component is a generic annotation that can be used for any managed class, but spring is designed to cater to three-tier architectures, so specific annotations are given for each layer.          Action  ---@Controller         Service---@Service         Dao---@Repository: Warehouse,      recommended: If you later host classes in layer three, use the corresponding annotations. If you are hosting a normal other class. @Component  3. The default generated instance is a singleton, and if you want to make more than one example, you have to add an annotation         @Scope ("prototype")  4.  @PostConstruct  //Initialize the instance when calling @PreDestroy//  destroy the instance before calling  5. If you use annotations to host a class without writing property values, Then the default ID identifier is the name of the class (the first letter is  lowercase) userserviceimpl* /

DI annotations

use annotations to complete dependency injection. General annotation injection, the point it targets is the injection of an object. Spring provides two annotations  @Resource  and  @Autowired for object injection-common annotations are two @Resource  &  @ Autowired@resource (Name= "UD")  finds the corresponding class based on the given tag, creates the object, and injects it in. @Autowired  Automatic assembly, the corresponding implementation class creation object will be found and injected in. But if there are multiple implementations, an exception is thrown

Second, Spring test unit
    1. Step: Import Jar---managed business logic class--annotate on test class, inject value to member variable of test class
//Spring expands JUnit's operating environment, and in addition to the test functionality, it defines the code that creates the factory .@RunWith (Springjunit4classrunner.class)//tell spring's test environment where the configuration file is@ContextConfiguration ("Classpath:applicationContext.xml") Public classTestuserservice {//test the annotations that appear inside the class without opening the scan switch. Because of this test environment, it will parse the annotations of this test class. @AutowiredPrivateUserService UserService; @Test Public voidTestsave () {userservice.save (); } }

Spring-----Annotation Development and Spring test unit

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.