您好,登錄后才能下訂單哦!
怎么在Spring中利用Java配置@Configuration和@Bean?針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
一、首先,需要xml中進行少量的配置來啟動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>
二、定義一個配置類
用@Configuration注解該類,等價 與XML中配置beans;用@Bean標注方法等價于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()); } }
三、基礎類代碼
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; } }
四、調用測試類
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中的內容 ApplicationContext annotationContext = new AnnotationConfigApplicationContext("SpringStudy"); Counter c = annotationContext.getBean("counter", Counter.class);// 創建bean的引用對象 System.out.println(c.getMultiplier()); System.out.println(c.isEquals()); System.out.println(c.getSong()); System.out.println(c.getInstrument().getName()); } }
注意:如果是在xml中配置beans和bean的話,或者使用自動掃描調用的話,代碼為
ApplicationContext ctx = new ClassPathXmlApplicationContext("spring/bean.xml");// 讀取bean.xml中的內容 Counter c = ctx.getBean("counter", Counter.class);// 創建bean的引用對象
五、運行結果
12.0 false Shake it Off Piano ---------------------
關于怎么在Spring中利用Java配置@Configuration和@Bean問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業資訊頻道了解更多相關知識。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。