怎么在Spring中利用Java配置@Configuration和@Bean?針對(duì)這個(gè)問(wèn)題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問(wèn)題的小伙伴找到更簡(jiǎn)單易行的方法。
創(chuàng)新互聯(lián)建站專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于網(wǎng)站設(shè)計(jì)、成都做網(wǎng)站、祿豐網(wǎng)絡(luò)推廣、成都小程序開(kāi)發(fā)、祿豐網(wǎng)絡(luò)營(yíng)銷、祿豐企業(yè)策劃、祿豐品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營(yíng)等,從售前售中售后,我們都將竭誠(chéng)為您服務(wù),您的肯定,是我們最大的嘉獎(jiǎng);創(chuàng)新互聯(lián)建站為所有大學(xué)生創(chuàng)業(yè)者提供祿豐建站搭建服務(wù),24小時(shí)服務(wù)熱線:028-86922220,官方網(wǎng)址:www.rwnh.cn
一、首先,需要xml中進(jìn)行少量的配置來(lái)啟動(dòng)Java配置:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd"> <context:component-scan base-package="SpringStudy.Model"> </context:component-scan> </beans>
二、定義一個(gè)配置類
用@Configuration注解該類,等價(jià) 與XML中配置beans;用@Bean標(biāo)注方法等價(jià)于XML中配置bean。
代碼如下:
package SpringStudy; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import SpringStudy.Model.Counter; import SpringStudy.Model.Piano; @Configuration public class SpringConfig { @Bean public Piano piano(){ return new Piano(); } @Bean(name = "counter") public Counter counter(){ return new Counter(12,"Shake it Off",piano()); } }
三、基礎(chǔ)類代碼
Counter:
package SpringStudy.Model; public class Counter { public Counter() { } public Counter(double multiplier, String song,Instrument instrument) { this.multiplier = multiplier; this.song = song; this.instrument=instrument; } private double multiplier; private String song; @Resource private Instrument instrument; public double getMultiplier() { return multiplier; } public void setMultiplier(double multiplier) { this.multiplier = multiplier; } public String getSong() { return song; } public void setSong(String song) { this.song = song; } public Instrument getInstrument() { return instrument; } public void setInstrument(Instrument instrument) { this.instrument = instrument; } }
Piano類
package SpringStudy.Model; public class Piano { private String name="Piano"; private String sound; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSound() { return sound; } public void setSound(String sound) { this.sound = sound; } }
四、調(diào)用測(cè)試類
package webMyBatis; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import SpringStudy.Model.Counter; public class SpringTest { public static void main(String[] args) { //ApplicationContext ctx = new ClassPathXmlApplicationContext("spring/bean.xml");// 讀取bean.xml中的內(nèi)容 ApplicationContext annotationContext = new AnnotationConfigApplicationContext("SpringStudy"); Counter c = annotationContext.getBean("counter", Counter.class);// 創(chuàng)建bean的引用對(duì)象 System.out.println(c.getMultiplier()); System.out.println(c.isEquals()); System.out.println(c.getSong()); System.out.println(c.getInstrument().getName()); } }
注意:如果是在xml中配置beans和bean的話,或者使用自動(dòng)掃描調(diào)用的話,代碼為
ApplicationContext ctx = new ClassPathXmlApplicationContext("spring/bean.xml");// 讀取bean.xml中的內(nèi)容 Counter c = ctx.getBean("counter", Counter.class);// 創(chuàng)建bean的引用對(duì)象
五、運(yùn)行結(jié)果
12.0 false Shake it Off Piano ---------------------
關(guān)于怎么在Spring中利用Java配置@Configuration和@Bean問(wèn)題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒(méi)有解開(kāi),可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識(shí)。
分享文章:怎么在Spring中利用Java配置@Configuration和@Bean
網(wǎng)站網(wǎng)址:http://www.rwnh.cn/article30/gcgsso.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信公眾號(hào)、網(wǎng)站建設(shè)、ChatGPT、移動(dòng)網(wǎng)站建設(shè)、網(wǎng)站維護(hù)、定制開(kāi)發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)