Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the Java-based @ Configuration and @ Bean uses in Spring

Shulou Source: shulou.com Published: 2022-06-02 13:06:18 10月01日 Update

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!

Tags: Configuration content code objects methods equivalence learning practicality deeper interest foundation practicality practicality simplicity of operation is in more friends comments results website Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Shulou Information Redmi Microsoft Huawei