Spring annotations are introduced in the Spring2.5 version to simplify the XML configuration. The frequency with which annotations are used in the enterprise development process is very high, but the premise of learning annotations is that everyone must be familiar with spring-based XML configuration, which is my personal advice because the use of annotations is not present in the Spring2.0 version
1. Spring common annotations are as follows
- @Component
- @Autowired
- @Qualifier
- @Scope
- @Controller
- @Service
- @Repository
2. When using spring annotations, be sure to focus on the package that the spring framework needs to include "very important", and here we take the spring4.0.3 version as an example to introduce
- Common-logging.jar
- Spring-core-4.0.3.jar
- Spring-context-4.0.3.jar
- Spring-beans-4.0.3.jar
- Spring-expression-4.0.3.jar
- Spring-aop-4.0.3.jar,"This jar package is in use <context:component-scan/> need to import this package"
3. @Component annotations
- @Component is primarily used to inject a Java class into the spring framework, which is equivalent to the <bean id= "xxx" class= "xxx" in the XML configuration file/>
- When spring annotations are used, we need to add <context:component-scan/> in the configuration file to scan the class with annotations added, so that the class that declares the annotations will work
- The name of the bean instance is default to the first lowercase of the bean class, and the rest is unchanged.
<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xmlns:p= "http://www.springframework.org/schema/p"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 " > <Context:component-scanBase-package= "Com.gxa.spring.day02"></Context:component-scan> </Beans>
Package com.gxa.spring.day02; Import org.springframework.stereotype.Component; @Component Public class studentannotation {}
Packagecom.gxa.spring.test;Importorg.junit.Test;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;Importcom.gxa.spring.day02.StudentAnnotation; Public classTest02 {@Test Public voidM06 () {ApplicationContext context=NewClasspathxmlapplicationcontext ("Spring.xml"); Studentannotationstudentannotation= Context.getbean ("Studentannotation", studentannotation.class); System.out.println (Studentannotation.hashcode ()); } }
4. In addition to the @component annotations, the spring container provides 3 functions and @component annotation equivalents. They are used to annotate dao,service and the controller of the Web layer, respectively.
- @Repository: For implementing class annotations to DAO
- @Service: Annotations for Service Implementation classes
- @Controller: Used to implement class annotations on a Controller
[Liu Yang Java]_spring Common Note Introduction _ 6th Lecture