spring3.2.5 Learning (ii)--IOC annotation Configuration

Source: Internet
Author: User

First, preface

This essay mainly records the process of IOC's full use of annotation.

Second, package and resources

The introduction of the rack package, as well as the project structure Reference spring3.2.5 Learning (i)--spring environment configuration and IOC introduction;

Third, the configuration environment

1. Configure Applicationcontext.xml

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "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/context Http://www.springframework.org/schema/context/spring-context.xs D ">             <Context:annotation-config/>    <!--Configuring annotation Scans -    <Context:component-scanBase-package= "Com.cyrus.platform" />    </Beans>

Where base-package is the specified scan path and is currently configured as a scan path: class under Com.cyrus.paltform.

2. Service class Tcyrususerservice comments

 PackageCom.cyrus.platform.service;ImportJavax.annotation.Resource;Importorg.springframework.stereotype.Component;ImportCom.cyrus.platform.dao.TCyrusUserDAO;ImportCom.cyrus.platform.model.TCyrusUser; @Component ("UserService") Public classTcyrususerservice {PrivateTcyrususerdao Userdao;  Public voidSave (Tcyrususer user) { This. Userdao.save (user); }     PublicTcyrususerdao Getuserdao () {returnUserdao; } @Resource (Name= "Userdao")     Public voidSetuserdao (Tcyrususerdao Userdao) { This. Userdao =Userdao; }}

where @Component ("UserService") is similar to the bean tag in the spring configuration file, the UserService in parentheses is the name attribute in the bean label; Resource is equivalent to the property configuration under the bean tag, @Resource The default search rule is query type first if no name is specified, and the name is queried if the type cannot match. Add the Name property in @resource to specify the related bean.

3. Implementing Class Tcyrususerdaoimpl annotations

 package   Com.cyrus.platform.dao.impl;  import   org.springframework.stereotype.Component;  import   Com.cyrus.platform.dao.TCyrusUserDAO;  import   Com.cyrus.platform.model.TCyrusUser; @Component (" Userdao ")  public  class  Tcyrususerdaoimpl implements   Tcyrususerdao {@ Override  public  void   Save (tcyrususer user) {System.out.println ( "The user is saved!"    ); }}

Third, testing

 PackageCom.cyrus.platform.service;ImportOrg.junit.Before;Importorg.junit.Test;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;ImportCom.cyrus.platform.model.TCyrusUser; Public classtcyrususerservicetest {@Before Public voidSetUp ()throwsException {} @Test Public voidTest () {ApplicationContext context=NewClasspathxmlapplicationcontext ("Classpath*:resources/spring/**/application*.xml"); Tcyrususerservice Service= Context.getbean ("UserService", Tcyrususerservice.class); Tcyrususer User=NewTcyrususer ();    Service.save (user); }}

spring3.2.5 Learning (ii)--IOC annotation Configuration

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.