"Spring Combat"--5 set Value injection

Source: Internet
Author: User

This article explains the most common features of spring-dependency injection.

The way to inject is to use Getter setter injection, usually most of the programming is also used this method.

To give a simple example, or a performer.

Performers have their own attributes, age or performances of the song and so on. It also requires complex properties, such as musical instruments, that each instrument makes a different sound.

Here's a look at the performer performer

Package Com.spring.test.action1;  Public Interface Performer {    void  perform () throws performanceexception;}

We define ourselves as a piano performer who has age and song, and an additional instrument attribute.

Package Com.spring.test.setter;import Com.spring.test.action1.performanceexception;import Com.spring.test.action1.Performer; Public classinstrumentalist implements performer{PrivateString Song; Private intAge ; Privateinstrument instrument;  Public intGetage () {returnAge ; }     Public voidSetage (intAge ) {         This. Age =Age ; }     PublicString Getsong () {returnSong; }     Public voidSetsong (String song) { This. Song =Song; }     PublicInstrument getinstrument () {returninstrument; }     Public voidSetinstrument (instrument instrument) { This. Instrument =instrument; }     Publicinstrumentalist () {} Public voidperform () throws Performanceexception {System. out. println ("instrumentalist Age:"+Age ); System. out. Print ("Playing"+song+":");    Instrument.play (); }}

The instrument is constructed as follows, still using the interface method:

Package Com.spring.test.setter;  Public Interface Instrument {    publicvoid  play ();}

SAX implements the Instrument interface

Package Com.spring.test.setter;  Public class Saxophone implements instrument {    public  saxophone () {}    publicvoid  Play () {        System. out. println ("Toot Toot toot");     }

The above is the construction of the basic class.

Here's a look at spring's configuration file:

<?xml version="1.0"encoding="UTF-8"? ><beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="Http://www.springframework.org/schema/beans"xsi:schemalocation="Http://www.springframework.org/schema/beanshttp//www.springframework.org/schema/beans/spring-beans-3.0.xsd "><bean id="Sax" class="Com.spring.test.setter.Saxophone"/> <bean id="Kenny" class="com.spring.test.setter.Instrumentalist"> <property name="Song"Value="Jingle Bells"/> <property name=" Age"Value=" -"/> <property name="Instrument" ref="Sax"/> </bean></beans>

In the configuration file, you can find that when setting a value injection, use name to specify which property to inject .

Name is named in terms of the variable name.

1 letters are not case-sensitive and the other parts are the same as variable names.

2 Injected property type, can be string, int, double, float, etc., when the property is a string or int, it can be automatically converted according to the type of the variable.

3 is injected with a bean, it is directly linked to another bean using ref.

Here is the code for the application context:

Package Com.spring.test.setter;import Org.springframework.context.applicationcontext;import Org.springframework.context.support.classpathxmlapplicationcontext;import com.spring.test.action1.PerformanceException; Public classTest { Public Static voidMain (string[] args) throws performanceexception {ApplicationContext ctx=NewClasspathxmlapplicationcontext ("Bean.xml"); Instrumentalist performer= (instrumentalist) Ctx.getbean ("Kenny");    Performer.perform (); }}

The results of the implementation are as follows:

Instrumentalist Age:Playing jingle Bells:toot Toot Toot

"Spring Combat"--5 set Value injection

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.