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