In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article focuses on "what is the use of @ Configuration and @ Bean based on Java in Spring". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what are the Java-based @ Configuration and @ Bean uses in Spring?"
First, a small amount of configuration in xml is required to start the Java configuration:
Define a configuration class
Annotating this class with @ Configuration is equivalent to configuring beans; in XML with @ Bean annotation and configuring bean in XML.
The code is as follows:
Package SpringStudy;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import SpringStudy.Model.Counter;import SpringStudy.Model.Piano;@Configurationpublic class SpringConfig {@ Beanpublic Piano piano () {return new Piano ();} @ Bean (name = "counter") public Counter counter () {return new Counter (12, "Shake it Off", piano ();}}
Third, basic class code
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;@Resourceprivate 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 class
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;}}
Fourth, call the test class
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"); / / read the contents of bean.xml ApplicationContext annotationContext = new AnnotationConfigApplicationContext ("SpringStudy"); Counter c = annotationContext.getBean ("counter", Counter.class); / / create bean's reference object System.out.println (c.getMultiplier ()) System.out.println (c.isEquals ()); System.out.println (c.getSong ()); System.out.println (c.getInstrument (). GetName ());}}
Note: if you configure beans and bean in xml, or if you use automatic scan calls, the code is
ApplicationContext ctx = new ClassPathXmlApplicationContext ("spring/bean.xml"); / / read the contents of bean.xml Counter c = ctx.getBean ("counter", Counter.class); / / create a reference object for bean
5. Operation results
12.0falseShake it OffPiano-
At this point, I believe you have a better understanding of "what is the use of @ Configuration and @ Bean based on Java in Spring". You might as well do it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.