Spring實(shí)戰(zhàn)之@Autowire注解用法詳解
本文實(shí)例講述了Spring實(shí)戰(zhàn)之@Autowire注解用法。分享給大家供大家參考,具體如下:
一 配置
<?xml version="1.0" encoding="GBK"?> <beans xmlns="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-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <context:component-scan base-package="org.crazyit.app.service,org.crazyit.app.dao"/> </beans>
二 dao接口
BaseDao
package org.crazyit.app.dao; public interface BaseDao<T> { void save(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> { }
三 dao實(shí)現(xiàn)類
BaseDaoImpl
package org.crazyit.app.dao.impl; import org.crazyit.app.dao.*; public class BaseDaoImpl<T> implements BaseDao<T> { public void save(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 { }
四 Bean
Item
package org.crazyit.app.domain; public class Item { }
User
package org.crazyit.app.domain; public class User { }
五 service接口
BaseService
package org.crazyit.app.service; import org.springframework.stereotype.*; import org.springframework.beans.factory.annotation.*; import org.crazyit.app.service.*; public interface BaseService<T> { void addEntity(T entity); }
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> { }
六 Service實(shí)現(xiàn)類
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<T> implements BaseService<T> { @Autowired private BaseDao<T> dao; public void addEntity(T entity) { System.out.println("調(diào)用" + dao + "保存實(shí)體:" + entity); } }
ItemServiceImpl
package org.crazyit.app.service.impl; import org.springframework.stereotype.*; import org.springframework.beans.factory.annotation.*; import org.crazyit.app.service.*; import org.crazyit.app.domain.*; @Component("itemService") public class ItemServiceImpl extends BaseServiceImpl<Item> implements ItemService { }
UserServiceImpl
package org.crazyit.app.service.impl; import org.springframework.stereotype.*; import org.springframework.beans.factory.annotation.*; import org.crazyit.app.service.*; import org.crazyit.app.domain.*; @Component("userService") public class UserServiceImpl extends BaseServiceImpl<User> implements UserService { }
七 測試類
package lee; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.crazyit.app.service.*; import org.crazyit.app.domain.*; public class BeanTest { public static void main(String[] args)throws Exception { // 創(chuàng)建Spring容器 ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); UserService us = ctx.getBean("userService", UserService.class); us.addEntity(new User()); ItemService is = ctx.getBean("itemService", ItemService.class); is.addEntity(new Item()); } }
八 測試
調(diào)用org.crazyit.app.dao.impl.UserDaoImpl@b7dd107保存實(shí)體:org.crazyit.app.domain.User@42eca56e調(diào)用org.crazyit.app.dao.impl.ItemDaoImpl@52f759d7保存實(shí)體:org.crazyit.app.domain.Item@7cbd213e
更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Spring框架入門與進(jìn)階教程》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對大家java程序設(shè)計(jì)有所幫助。
上一篇:Java List集合排序?qū)崿F(xiàn)方法解析
欄 目:Java
本文標(biāo)題:Spring實(shí)戰(zhàn)之@Autowire注解用法詳解
本文地址:http://mengdiqiu.com.cn/a1/Java/8838.html
您可能感興趣的文章
- 01-10Springboot中@Value的使用詳解
- 01-10springboot實(shí)現(xiàn)文件上傳步驟解析
- 01-10springboot jta atomikos實(shí)現(xiàn)分布式事物管理
- 01-10SpringBoot使用RabbitMQ延時(shí)隊(duì)列(小白必備)
- 01-10如何基于SpringBoot部署外部Tomcat過程解析
- 01-10SPRING BOOT啟動(dòng)命令參數(shù)及源碼詳析
- 01-10springboot集成fastDfs過程代碼實(shí)例
- 01-10springmvc級聯(lián)屬性處理無法轉(zhuǎn)換異常問題解決
- 01-10SPRINGBOOT讀取PROPERTIES配置文件數(shù)據(jù)過程詳解
- 01-10Spring注解和同步鎖不能同步問題解決


閱讀排行
本欄相關(guān)
- 01-10Java實(shí)現(xiàn)動(dòng)態(tài)模擬時(shí)鐘
- 01-10Springboot中@Value的使用詳解
- 01-10JavaWeb實(shí)現(xiàn)郵件發(fā)送功能
- 01-10利用Java實(shí)現(xiàn)復(fù)制Excel工作表功能
- 01-10Java實(shí)現(xiàn)動(dòng)態(tài)數(shù)字時(shí)鐘
- 01-10java基于poi導(dǎo)出excel透視表代碼實(shí)例
- 01-10java實(shí)現(xiàn)液晶數(shù)字字體顯示當(dāng)前時(shí)間
- 01-10基于Java驗(yàn)證jwt token代碼實(shí)例
- 01-10Java動(dòng)態(tài)顯示當(dāng)前日期和時(shí)間
- 01-10淺談Java中真的只有值傳遞么
隨機(jī)閱讀
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 04-02jquery與jsp,用jquery
- 01-10delphi制作wav文件的方法
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 08-05織夢dedecms什么時(shí)候用欄目交叉功能?
- 01-10C#中split用法實(shí)例總結(jié)