I. Development of annotations
<context:component-scan base-package= "Com.itheima"/>
/* 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
- 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