Spring annotation @resource Why doesn't it work?

Source: Internet
Author: User

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?

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.