Common notes in annotation:
@Componet describe a bean in spring
@Repository identify the class of the Data Access Layer (DAO layer) as a bean in spring
@Service identify the class of the business Layer (service layer) as a bean in spring
@Controller identify the class of the control layer as a bean in spring
@Resource has two properties, name and type, which resolves name to the bean instance name, and the Type property resolves to the Bean's instance type.
Use examples to illustrate how.
Userdao interface
Public interface Userdao {public
void Save ();
}
Userdaoimpl class, implementing Userdao interface
Import org.springframework.stereotype.Repository;
@Repository ("Userdao")//This is labeled as bean public
class Userdaoimpl implements Userdao {
@Override public
Void Save () {
//TODO auto-generated method stub
System.out.println ("Userdao...save ...");}
}
UserService interface
Public interface UserService {public
void Save ();
}
Userserviceimpl class, implementing UserService interface
Import Javax.annotation.Resource;
Import Org.springframework.stereotype.Service;
@Service ("UserService")//labeled as Bean public
class Userserviceimpl implements UserService {
@Resource (name= " Userdao ")//Assemble the private Userdao Userdao by the name of the Bean
;
public void Setuserdao (Userdao userdao) {
This.userdao=userdao;
}
@Override public
Void Save () {
//TODO auto-generated method Stub
this.userDao.save ();
System.out.println ("Userservice...save ...");}
}
Useraction class
Import Javax.annotation.Resource;
Import Org.springframework.stereotype.Controller;
@Controller ("useraction")//labeled as Bean public
class Useraction {
@Resource (name= "UserService")//assembled by Bean name
private UserService UserService;
public void Setuserservice (UserService userservice) {
this.userservice=userservice;
}
public void Save () {
this.userService.save ();
System.out.println ("Useraction...save ...");}
}
Configuration of XML
<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 "
xmlns:aop="/http/ WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP "
xmlns:tx=" Http://www.springframework.org/schema/tx "
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 ">
<!--Use the context namespace to notify spring to scan the specified path for annotation parsing-
<context: Component-scan base-package= "package name" ></context:component-scan>
</beans>
Experience: The individual in the run time, reported a mistake, Java.lang.noclassdeffounderror:org/springframework/aop/targetsource, just started to think that is not a jar package problem, Because the jar package is based on the teacher said the import, the results of the search for the next solution, is indeed a small jar package, spring's Spring-aop-4.x.x.release.jar