Spring Learning in the Java series--Annotations (iii)

Source: Internet
Author: User

First, annotations

To turn on the automatic scanning feature before using annotations

<?xml version= "1.0" encoding= "UTF-8"? ><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 "    xsi:schemalocation=" Http://www.springframework.org/schema/beans/       http Www.springframework.org/schema/beans/spring-beans.xsd       Http://www.springframework.org/schema/mvc        http:/ /www.springframework.org/schema/mvc/spring-mvc.xsd        http://www.springframework.org/schema/context        http ://www.springframework.org/schema/context/spring-context.xsd        HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP        http://www.springframework.org/schema/aop/spring-aop.xsd        Http://www.springframework.org/schema/tx        http://www.springframework.org/schema/tx/spring-tx.xsd       ">     <context:component-scan Base-package= "Springannotationbll,springannotationdal"/></beans>
Second, structure code

    

Iii. Common annotations

@Service for labeling business layer components,
@Controller for labeling control-layer components, such as action in struts
@Repository is used to label data access Components, or DAO components.
@Component refer to components, we can use this annotation when the component is poorly categorized.
@Scope used to specify scope scope (used on a class)

Iv. example 4.1, DAO layer Code
Package Springannotationdal;public interface Springannotationdal {public abstract void Add ();}
Package Springannotationdal;import org.springframework.stereotype.Repository; @Repositorypublic class Springannotationdalbean implements Springannotationdal {/* (non-javadoc) * @see Springannotationdal.springannotationdal#add () */public void Add () {System.out.print ("add ...");}}
4.2. Service Layer Code
Package Springannotationbll;public interface SPRINGANNOTATIONBLL {public abstract void Add ();}
Package Springannotationbll;import Org.springframework.stereotype.service;import Springannotationdal.springannotationdal, @Servicepublic class Springannotaionbllbean implements SPRINGANNOTATIONBLL {Private Springannotationdal springannotationdal;public springannotationdal Getspringannotationdal () {return Springannotationdal;} public void Setspringannotationdal (Springannotationdal springannotationdal) {this.springannotationdal = Springannotationdal;} public void Add () {System.out.print ("add ...");}}
4.3. Call
Package Test;import Java.io.ioexception;import Java.io.printwriter;import javax.servlet.servletexception;import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;import Org.springframework.context.applicationcontext;import Org.springframework.context.support.classpathxmlapplicationcontext;import Springannotationbll.springannotationbll;import Spring.testservicebean;public class Cservlet extends HttpServlet { public void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException { ApplicationContext context=new classpathxmlapplicationcontext ("Applicationcontext.xml"); SPRINGANNOTATIONBLL services= (SPRINGANNOTATIONBLL) Context.getbean ("Springannotaionbllbean"); Services.add ();}}
V. Results

 

Spring Learning in the Java series--Annotations (iii)

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.