@Autowire注解怎么在春天中使用

  介绍

@Autowire注解怎么在春天中使用?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

<强>一配置

& lt; ? xml  version=?.0“,编码=癎BK" ?比;   http://www.springframework.org/schema/beans" & lt; beans  xmlns=?;   ,,xmlns: xsi=癶ttp://www.w3.org/2001/XMLSchema-instance"   ,,xmlns:上下文=癶ttp://www.springframework.org/schema/context"   ,,xsi: schemaLocation=? http://www.springframework.org/schema/beans   ,,http://www.springframework.org/schema/beans/spring-beans-4.0.xsd   ,,http://www.springframework.org/schema/context   ,,,http://www.springframework.org/schema/context/spring-context-4.0.xsd"的在   ,,& lt;环境:component-scan   ,,,,,基本包=皁rg.crazyit.app.service, org.crazyit.app.dao"/祝辞,,,   & lt;/beans>

<强>二刀接口扩展

BaseDao

package  org.crazyit.app.dao;   public  interface  BaseDao   {   ,,void 拯救(T  e);   }

ItemDao

package  org.crazyit.app.dao;   import  org.crazyit.app.domain。*;   public  interface  ItemDao  extends  BaseDao< Item>   {   }

UserDao

package  org.crazyit.app.dao;   import  org.crazyit.app.domain。*;   public  interface  UserDao  extends  BaseDao< User>   {   }

<强>三刀实现类

BaseDaoImpl

package  org.crazyit.app.dao.impl;   import  org.crazyit.app.dao。*;   public  class  BaseDaoImpl   {   ,,public  void 保存(T  e)   ,,{   ,,,,,System.out.println(“程序保存对象:“,+,e);   ,,}   }

ItemDaoImpl

package  org.crazyit.app.dao.impl;   import  org.springframework.stereotype。*;   import  org.crazyit.app.dao。*;   import  org.crazyit.app.domain。*;   @ component (“itemDao")   public  class  ItemDaoImpl  extends  BaseDaoImpl< Item>   implements  ItemDao才能   {   }

UserDaoImpl

package  org.crazyit.app.dao.impl;   import  org.springframework.stereotype。*;   import  org.crazyit.app.dao。*;   import  org.crazyit.app.domain。*;   @ component (“userDao")   public  class  UserDaoImpl  extends  BaseDaoImpl< User>   implements  UserDao才能   {   }

<强>四豆

项目 package  org.crazyit.app.domain;   public  class 项目   {   用户}

 package  org.crazyit.app.domain;
  public  class 用户
  {
  }

<强>五服务接口

BaseService

package  org.crazyit.app.service;   import  org.springframework.stereotype。*;   import  org.springframework.beans.factory.annotation。*;   import  org.crazyit.app.service。*;   public  interface  BaseService   {   void 才能addEntity (T 实体);   }

ItemService

package  org.crazyit.app.service;   import  org.springframework.stereotype。*;   import  org.springframework.beans.factory.annotation。*;   import  org.crazyit.app.service。*;   import  org.crazyit.app.domain。*;   @ component   public  interface  ItemService  extends  BaseService< Item>   {   }

UserService

package  org.crazyit.app.service;   import  org.springframework.stereotype。*;   import  org.springframework.beans.factory.annotation。*;   import  org.crazyit.app.service。*;   import  org.crazyit.app.domain。*;   @ component   public  interface  UserService  extends  BaseService< User>   {   }

<>强六服务实现类

BaseServiceImpl

package  org.crazyit.app.service.impl;   import  org.springframework.stereotype。*;   import  org.springframework.beans.factory.annotation。*;   import  org.crazyit.app.dao。*;   import  org.crazyit.app.service。*;   public  class  BaseServiceImpl   {   @ autowired才能   private 才能;BaseDao< T>,刀;   public 才能;void  addEntity (T 实体)   {才能   ,,,System.out.println(“调用“,+,刀   ,,,,,+,“保存实体:“,+,实体);   ,,}   }

@Autowire注解怎么在春天中使用