How to assemble Spring base-bean (ii) annotation-based configuration

Source: Internet
Author: User
Tags aop stub xmlns

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

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.