I want to use annotations, in the service layer calls the DAO Layer query database method, the query card number "1" exists, but error:
This should be annotated using unsuccessful meaning, how should spring annotations be used?
Here is a complete example of the project structure as follows
Testannotation.java
import Org.springframework.context.ApplicationContext; Import Org.springframework.context.support.ClassPathXmlApplicationContext; Public classtestannotation { Public Static voidMain (string[] args) {ApplicationContext context=NewClasspathxmlapplicationcontext ("Applicationcontext.xml"); Myaction Action= (myaction) Context.getbean ("myaction"); Action.testannotation (); } }
Applicationcontext.xml
<?xml version="1.0"encoding="UTF-8"? ><beans xmlns="Http://www.springframework.org/schema/beans"Xmlns:context="Http://www.springframework.org/schema/context"Xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"xsi:schemalocation="Http://www.springframework.org/schema/beanshttp//www.springframework.org/schema/beans/spring-beans-3.1.xsdhttp//Www.springframework.org/schema/contexthttp//www.springframework.org/schema/context/spring-context-3.1.xsd "><context:annotation-config/> <!--action is not annotated-<bean id="myaction" class="com.demo.annotation.MyAction"Scope="prototype"/> <!--annotation test--<context:component-scanBase-package="com.demo.annotation"/> </beans>
Myaction.java
import Javax.annotation.Resource; Public classmyaction{@Resource (name="Testservice") PrivateTestservice Testservice; PublicString testannotation () {if(NULL==Testservice) {System. out. println ("Service is null!!"); The String result=testservice.gettestannotation (); System. out. println (Result); return "Success"; } PublicTestservice Gettestservice () {returnTestservice; } Public voidSettestservice (Testservice testservice) { This. Testservice =Testservice; } }
Testservice.java
Public Interface Testservice { public String gettestannotation (); }
Testserviceimpl.java
import Org.springframework.stereotype.Service; Import org.springframework.beans.factory.annotation.Autowired; Import Org.springframework.beans.factory.annotation.Qualifier; /** * * * NOTE Test **/@Service ("Testservice") Public classTestserviceimp implements Testservice {/** * Automatic Assembly*/@Autowired @Qualifier ("Testdao") //The @Resource (name= "Testdao"), equivalent to <property ... ref= "Testdao"/> PrivateTestdao Testdao; PublicTestdao Gettestdao () {returnTestdao; } Public voidSettestdao (Testdao Testdao) { This. Testdao =Testdao; } @Override PublicString gettestannotation () {//TODO auto-generated Method Stub returntestdao.gettestdaoannotation (); } }
Testdao.java
Public Interface Testdao { /* * * * get DAO layer Annotations * @return */public String gettestdaoannotation (); }
Testdaoimpl.java
import org.springframework.stereotype.Repository; /** * Test annotation **/@Repository ("Testdao") Public classTestdaoimpl implements Testdao {@Override PublicString gettestdaoannotation () {//TODO auto-generated Method Stub return "This is Testdao Annotation ..."; } }
Operation Result:
Spring annotation @resource Why doesn't it work?