欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

深入理解java的spring-ioc的使用

spring-ioc的使用

創(chuàng)新互聯(lián)公司于2013年成立,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目成都網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢想脫穎而出為使命,1280元徐水做網(wǎng)站,已為上家服務(wù),為徐水各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:028-86922220

IOC容器在很多框架里都在使用,而在spring里它被應(yīng)用的最大廣泛,在框架層面上,很多功能都使用了ioc技術(shù),下面我們看一下ioc的使用方法。

  1. 把服務(wù)注冊到ioc容器
  2. 使用屬性注入反射對應(yīng)類型的實(shí)例
  3. 多態(tài)情況下,使用名稱反射類型的實(shí)例

把服務(wù)注冊到ioc容器

@Bean注冊組件
使用@Bean注解進(jìn)行類型的注冊,默認(rèn)你的ioc容器里類型為bean的返回值,名稱為bean所有的方法名,與你的包名稱沒有直接關(guān)系,如果你的接口有多種實(shí)現(xiàn),在注冊時(shí)可以使用@Bean("lind")這種方式來聲明。

@Component,@Configuration,Service,Repository注冊組件

這幾個(gè)注解都是在類上面聲明的,而@Bean是聲明在方法上的,這一點(diǎn)要注意,這幾個(gè)注解一般是指對一個(gè)接口的實(shí)現(xiàn),在實(shí)現(xiàn)類上加這些注解,例如,一個(gè)數(shù)據(jù)倉儲接口UserRepository,它可以有多種數(shù)據(jù)持久化的方式,如SqlUserRepositoryImpl和MongoUserRepositoryImpl,那么在注冊時(shí)你需要為他們起一個(gè)別名,如@Repository("Sql-UserRepositoryImpl) qlUserRepositoryImpl,默認(rèn)的名稱是類名,但注意類名首字母為小寫

public interface EmailLogService {
 void send(String email, String message);
}

@Component()
public class EmailLogServiceHttpImpl implements EmailLogService {
 private static final Logger logger = LoggerFactory.getLogger(EmailLogServiceHttpImpl.class);

 @Override
 public void send(String email, String message) {
 Assert.notNull(email, "email must not be null!");
 logger.info("send email:{},message:{}", email, message);
 }
}
@Component("email-socket")
public class EmailLogServiceSocketImpl implements EmailLogService {
 private static final Logger logger = LoggerFactory.getLogger(EmailLogServiceSocketImpl.class);

 @Override
 public void send(String email, String message) {
 Assert.notNull(email, "email must not be null!");
 logger.info("send email2:{},message:{}", email, message);
 }
}
// 看一下調(diào)用時(shí)的測試代碼
 @Resource(name = "email-socket")
 EmailLogService socketEmail;
 @Autowired
 @Qualifier( "emailLogServiceHttpImpl")
 EmailLogService httpEmail;

 @Test
 public void testIoc2() {
 socketEmail.send("ok", "ok");
 }


 @Test
 public void testIoc1() {
 httpEmail.send("ok", "ok");
 }

在程序中使用bean對象

1.使用Resource裝配bean對象
在通過別名調(diào)用bean時(shí),你可以使用@Resource注解來裝配對象

2.使用@Autowired裝配bean對象
也可以使用 @Autowired
@Qualifier( "emailLogServiceHttpImpl")兩個(gè)注解去實(shí)現(xiàn)程序中的多態(tài)

使用場景

在我們有些相同行為而實(shí)現(xiàn)方式不同的場景中,如版本1接口與版本2接口,在get方法實(shí)現(xiàn)有所不同,而這
兩個(gè)版本都要同時(shí)保留,這時(shí)我們需要遵守開閉原則,擴(kuò)展一個(gè)新的接口,而在業(yè)務(wù)上對代碼進(jìn)行重構(gòu),
提取兩個(gè)版本相同的方法到基類,自己維護(hù)各自獨(dú)有的方法,在為它們的bean起個(gè)名字,在裝配時(shí),通過
bean的名稱進(jìn)行裝配即可。

寫個(gè)偽代碼:

class Api_version1(){
@Autowired 
@Qualifier("print-version1")
PrintService printService;
}

class Api_version2(){
@Autowired 
@Qualifier("print-version2")
PrintService printService;
}

class BasePrintService{}

interface PrintService{}

@Service("print-version1")
class PrintServiceImplVersion1 extends BasePrintService implements PrintService{}

@Service("print-version2")
class PrintServiceImplVersion2 extends BasePrintService implements PrintService{}

以上所述是小編給大家介紹的java的spring-ioc的使用詳解整合,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時(shí)回復(fù)大家的。在此也非常感謝大家對創(chuàng)新互聯(lián)網(wǎng)站的支持!

當(dāng)前文章:深入理解java的spring-ioc的使用
本文網(wǎng)址:http://chinadenli.net/article22/jpchjc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃移動網(wǎng)站建設(shè)品牌網(wǎng)站設(shè)計(jì)手機(jī)網(wǎng)站建設(shè)響應(yīng)式網(wǎng)站Google

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

搜索引擎優(yōu)化