A quick introduction to the annotated approach to the IOC of the spring framework
1. Step One: Import annotations develop all the required jar packages
* 6 jar Packages required for the introduction of an IOC container
* Multiple introduction of one: Spring Framework AOP jar package, SPRING-AOP jar Package
2. Step two: Create the corresponding package structure, write the Java class
* UserService--Interface
* Userserviceimpl--Specific implementation class
3. Step three: In the SRC directory, create the Applicationcontext.xml configuration file, and then introduce the constraints. Note: Because of the way you want to use annotations now, the constraints introduced have changed
* need to introduce context constraints, the specific constraints are as follows
<Beansxmlns= "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-b Eans.xsd Http://www.springframework.org/schema/context Http://www.springframework.org/schema/context/sp Ring-context.xsd "> <!--Bean Definitions here - </Beans>
4. Step four: Turn on component scanning in the Applicationcontext.xml configuration file
* Spring Annotation Development: Component Scan <Context:component-scan base-package= "Com.itheima.demo1" />
* Note: You can use the following configuration
<base-package= "Com.itheima"/>// So it's scanning all the content under the Com.itheima package.
5. Step Five: Add annotations on the Userserviceimpl implementation class
* @Component (value= "UserService") --equivalent to <bean id= "UserService" class= "in the way XML is configured" >
6. Step Six: Write the test code
Public class SpringDemo1 { @Test publicvoid run1 () { new Classpathxmlapplicationcontext ("Applicationcontext.xml"); = (UserService) ac.getbean ("UserService"); Us.save (); } }
common annotations for bean Management in spring framework
1.@ComponentComponents. (Effect on the class)
2. Spring provides three derivative annotations of @component: (functionality is now consistent)
*@Controller--acting on the web layer
* @Service --role in the business layer
*@Repository--acting on the persistence layer
* Description: These three annotations are intended to make the label class itself clear, and spring will enhance it in subsequent versions
3. Annotation of attribute injection (note: The use of annotation injection, can not provide a set method)
* If the generic type is injected, you can use the value annotation
* @Value --For injecting common types
* If the object type is injected, use the following annotation
* @Autowired--automatic assembly by type by default
* If you want to inject by name
*@Qualifier--forcing the use of name injection
* @Resource --equivalent to @autowired and @qualifier used together
* Emphasis: annotations provided by Java
* Attribute using the Name property
annotations of the bean's scope and life cycle
1. Notes on the scope of the bean
* Annotated as@Scope (value= "prototype"), acting on the class. The values are as follows:
* Singleton-single case, default value
* Prototype--Multiple cases
2. Configuration of the Bean's life cycle (understanding)
* Notes are as follows:
* @PostConstruct--equivalent to Init-method
* @PreDestroy--equivalent to Destroy-method
Spring Framework consolidates JUnit unit tests
1. To simplify JUnit testing, the spring framework can also be used to integrate testing
2. Specific steps
* Requirements: There must be a junit environment first (that is, the development environment has been imported into the JUNIT4)!!
* Step one: Introduce in the program:Spring-test.jar
* Step Two: Add annotations on specific test classes
@RunWith (Springjunit4classrunner. Class) @ContextConfiguration ("Classpath:applicationContext.xml") public class SpringDemo1 { @Resource (name= "UserService") Private UserService UserService; @Test publicvoid Demo2 () { userservice.save (); } }
Spring Framework IOC annotations