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)