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è)計有所幫助。
相關(guān)文章
關(guān)于SpringBoot3.x中spring.factories功能被移除的解決方案
這篇文章主要介紹了SpringBoot3.x中spring.factories功能被移除的解決方案,在配置好相關(guān)依賴、最小啟動類和配置之后,發(fā)現(xiàn)項目無法啟動,于是根據(jù)啟動上下文日志和按行DEBUG找到原因并且在等待組件升級兼容之前進(jìn)行臨時性解決,需要的朋友可以參考下2022-12-12
java實(shí)現(xiàn)的漢字轉(zhuǎn)五筆功能實(shí)例
這篇文章主要介紹了java實(shí)現(xiàn)的漢字轉(zhuǎn)五筆功能,結(jié)合具體實(shí)例形式分析了java基于字符串遍歷與編碼轉(zhuǎn)換等操作實(shí)現(xiàn)五筆編碼獲取的相關(guān)操作技巧,需要的朋友可以參考下2017-06-06
將Arthas整合到Java業(yè)務(wù)鏡像中的流程步驟
在現(xiàn)代Java應(yīng)用開發(fā)中,診斷和調(diào)試是一個不可或缺的環(huán)節(jié),Arthas,作為阿里巴巴開源的一款Java診斷工具,提供了一種在不修改代碼的情況下,實(shí)時監(jiān)控、診斷和調(diào)試Java應(yīng)用程序的解決方案,本文將詳細(xì)介紹Arthas的基本概念,并逐步指導(dǎo)如何將其整合到Java業(yè)務(wù)鏡像中2025-02-02
elasticsearch構(gòu)造Client實(shí)現(xiàn)java客戶端調(diào)用接口示例分析
這篇文章主要為大家介紹了elasticsearch構(gòu)造Client實(shí)現(xiàn)java客戶端調(diào)用接口示例分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-04-04
Java擴(kuò)展庫RxJava的基本結(jié)構(gòu)與適用場景小結(jié)
RxJava(GitHub: https://github.com/ReactiveX/RxJava)能夠幫助Java進(jìn)行異步與事務(wù)驅(qū)動的程序編寫,這里我們來作一個Java擴(kuò)展庫RxJava的基本結(jié)構(gòu)與適用場景小結(jié),剛接觸RxJava的同學(xué)不妨看一下^^2016-06-06

