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)類(lèi)
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("程序保存對(duì)象:" + 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)類(lèi)
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 { }
七 測(cè)試類(lèi)
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()); } }
八 測(cè)試
調(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)容感興趣的讀者可查看本站專(zhuān)題:《Spring框架入門(mén)與進(jìn)階教程》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。
相關(guān)文章
Spring?Cloud?OAuth2實(shí)現(xiàn)自定義token返回格式
Spring?Security?OAuth的token返回格式都是默認(rèn)的,但是往往這個(gè)格式是不適配系統(tǒng)。本文將用一個(gè)接口優(yōu)雅的實(shí)現(xiàn)?Spring?Cloud?OAuth2?自定義token返回格式,需要的可以參考一下2022-06-06Java中管理資源的引用隊(duì)列相關(guān)原理解析
這篇文章主要介紹了Java中管理資源的引用隊(duì)列相關(guān)原理解析,涉及到Java的垃圾回收機(jī)制方面的知識(shí),需要的朋友可以參考下2015-12-12一篇文章帶你了解spring事務(wù)失效的多種場(chǎng)景
在日常編碼過(guò)程中常常涉及到事務(wù),在前兩天看到一篇文章提到了Spring事務(wù),那么在此總結(jié)下在Spring環(huán)境下事務(wù)失效的幾種原因.2021-09-09Java實(shí)現(xiàn)鼠標(biāo)隨機(jī)移動(dòng)效果的示例代碼
有的時(shí)候我們需要鼠標(biāo)一直滑動(dòng)的情況,為了節(jié)省時(shí)間,本文用Java語(yǔ)言寫(xiě)了一個(gè)腳本,可以實(shí)現(xiàn)鼠標(biāo)隨機(jī)移動(dòng),感興趣的小伙伴可以了解一下2022-05-05淺談SpringCloud的微服務(wù)架構(gòu)組件
這篇文章主要介紹了淺談SpringCloud的微服務(wù)架構(gòu)組件,Spring Cloud根據(jù)分布式服務(wù)協(xié)調(diào)治理的需求成立了許多子項(xiàng)目,每個(gè)項(xiàng)目通過(guò)特定的組件去實(shí)現(xiàn),需要的朋友可以參考下2023-04-04SpringBoot替換默認(rèn)的tomcat服務(wù)器的方法
Tomcat是Apache基金下的一個(gè)輕量級(jí)的Servlet容器,支持Servlet和JSP,Tomcat具有Web服務(wù)器特有的功能,在SpringBoot框架中,我們使用最多的是Tomcat,這是SpringBoot默認(rèn)的容器技術(shù),本文給大家介紹了Spring?Boot如何替換默認(rèn)的tomcat服務(wù)器,需要的朋友可以參考下2024-08-08利用Java獲取文件名、類(lèi)名、方法名和行號(hào)的方法小結(jié)
這篇文章運(yùn)用實(shí)例代碼給大家介紹了利用Java怎樣獲取文件名、類(lèi)名、方法名和行號(hào),有需要的可以參考借鑒,下面一起來(lái)看看吧。2016-08-08解決RestTemplate第一次請(qǐng)求響應(yīng)速度較慢的問(wèn)題
這篇文章主要介紹了解決RestTemplate第一次請(qǐng)求響應(yīng)速度較慢的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10Java項(xiàng)目中防止SQL注入的四種方案總結(jié)
SQL注入是一種代碼注入技術(shù),通過(guò)把SQL命令插入到Web表單遞交或輸入域名或頁(yè)面請(qǐng)求的查詢(xún)字符串,最終達(dá)到欺騙服務(wù)器執(zhí)行惡意的SQL命令,下面我們就來(lái)看看如何在項(xiàng)目中防止SQL注入吧2023-10-10