60 seconds to spring

xiaoxiao2021-03-06  114

60 seconds to spring

Introduction

Spring is a layered Java / J2EE Application Framework, Based on-one J2EE DESIGN DEVEEROPMENT BY ROD JOHNSON (Wrox, 2002).

This A Series of Very Short Tutorial Giving You a Jump Start Unding of Spring Framework IN 60 Seconds (Per Tutorial). It Do IT, Read The Spring Reference Documentation for That.

Pre-Requisites

BEFORE Running Any of The Tutorial on your machine, you need to get the below feput.

Installed J2SE 1.4.2 Plus Environment Setting. Installed Ant 1.6.1 Plus Environment Setting and Some Simple Knowledge on Using Ant. Download Spring Framework 1.0.2 with dependencies.

Tutorials Source Code

60Sec2Spring.zip (Spring.jar Included)

Tutorials

Bean Tutorial 1 - Hello World Bean

Bean Tutorial 2 - To Singleton or Not To Singleton

The first example of Bean Tutorial 3 - setter-based and construction-based dependency Injection is to create a bean with an XML file, and you don't have to write Factory yourself. The second example is simpler Singleton: It is equivalent to the third example of Static. The first example solves the problem of object creation. Now it is to rely on bean call issues, depending on the bean has two ways to call, one is a constructor One is a set method, this is not special, I have used a lot of swings. But strangely, there is no call or constructor in the main method. Here is the use of XML, and the secret creation of relying on Bean (he has been found automatically), don't need us to call (don't look for him again), huh, not just effort, more embodying IOC. Reference refers to the form of three INJECTION, which is Interface INJECTION, SETTER INJECTION, and CONSTRUCTOR INJECTION, corresponding to Type 1, Type 2, Type 3, which is mentioned in IOC. In addition to Interface Injection due to strong aggressive, there is no difference in other forms. Spring and Pico have implemented these two forms of INJECTION, just Spring highlights SETTER INJECTION, while Pico tends to constructor Injection. When the two components need to communicate with each other, the setter INJECTION is obviously better than the constructor inJection.

60 sec to Spring [TOC] Bean Tutorial 1 - Hello World Bean This tutorial show you how to call a Hello World Bean using Spring IoC Step 1: Create a HelloBean.java in src org / jarchitect / spring / tutorial1 / HelloBean.javapackage. Org.jarchitect.spring.tutorial1;

Public class hellobean {

Public String Sayhelloworld () {

Return "Hello World";

}

} Step 2: Specify The Hellobean Class in The Bean.xml Bean.xml

"http://www.springframework.org/dtd/spring-beans.dtd">

Jarchitect Spring Tutorial 1

Step 3: Create a main.java in src org / jarchitect / Spring / Tutorial1 / Main.java

Package org.jarchitect.spring.tutorial1;

Import java.io.fileinputstream;

Import java.io.filenotfoundexception;

Import Java.io.InputStream;

Import org.springframework.beans.Factory.xml.xmlbeanfactory;

Public class main {

Public static void main (String [] args) {

Try {

// r r r configure

InputStream IS = New FileInputStream ("bean.xml");

XMLBeanfactory Factory = New XMLBeanFactory (IS);

// instantiate an Object

Hellobean Hellobean = (Hellobean) Factory.getBean ("Hellobean");

// Execute the public method of the hellobean

System.out.println (Hellobean.SayhelloWorld ());

} catch (filenotfoundexception e) {

E.PrintStackTrace ();

}

}

} Step 4: Use an ant script to build and execute the main class. Just Run Ant from The Command Prompt Will Do The Trick. Beelow Are The Output from Ant.

C: / 60sec2spring / springTutorial1> ANTBUILDFILE: Build.xml

Build:

[Javac] Compiling 2 Source Files To C: / 60sec2spring / SpringTutorial1 / BIN

Run:

[Java] May 18, 2004 2:25:14 Pm Org.SpringFramework.beans.Factory.xml.xmlbeandefinitionReader loadingBeandefinitions

[Java] Info: Loading XML Bean Definitions from (No Description)

[Java] May 18, 2004 2:25:15 Pm Org.SpringFramework.beans.Factory.support.AbstractBeanFactory GetBean

[java] info: CREANG Shared Instance of Singleton Bean 'Hellobean'

[java] Hello World Build Successful

Total Time: 3 Second Done. [TOC]

. Bean Tutorial 2 - To Singleton or Not To Singleton Beans are defined to be deployed in one of the two modes: Singleton and Non-Singleton (Prototype) When a bean is a singleton, only one shared instance of the bean will be managed. The prototype mode of a bean deployment results in the creation of a new bean instance every time a request for that specific bean is done. Tutorial 2 will demostrate the difference behaviour when defining a bean as Singleton and Prototype.

Note: by Default All Beans Are Deployed in Singleton Mode, Unless 1: Create a counterbean.java in src org / jarchitect / Spring / Tutorial2 / CounterBean.java

Package org.jarchitect.spring.tutorial2;

Public class counterbean {

PRIVATE INT Counter;

Public counterbean () {

System.out.println ("Construct Counterbean ...");

}

Public int getCount () {

Return Counter ;

}

}

Step 2: Specify The Counterbean Class in The Bean.xml Do Notice That I Have Defined One Counterbean As Singleton Bean and The Other as prototype bean.

"http://www.springframework.org/dtd/spring-beans.dtd">

Jarchitect Spring Tutorial 2

Step 3: Create a main.java in src org / jarchitect / Spring / Tutorial2 / Main.java

Package org.jarchitect.spring.tutorial2;

Import java.io.fileinputstream;

Import java.io.filenotfoundexception;

Import Java.io.InputStream;

Import org.springframework.beans.Factory.xml.xmlbeanfactory;

Public class main {

Public static void main (String [] args) {

Try {

// r r r configure

InputStream IS = New FileInputStream ("bean.xml");

XMLBeanfactory Factory = New XMLBeanFactory (IS);

// instantiate an startleton objecton Object

System.out.println ("---- Singleton Bean ----");

Counterbean singleletonbean =

Factory.getBean ("Singletonbean");

System.out.println ("First Call: -" SingletonBean.getCount ());

Singletonbean = (counterbean) Factory.getBean ("Singletonbean");

System.out.println ("Second Call: -" SingletonBean.getCount ());

System.out.println ("");

// instantiate an Object

System.out.println ("---- Prototype Bean ----");

Counterbean prototypebean =

("Prototypebean");

System.out.println ("First Call: -" PrototypeBean.getCount ());

Prototypebean = (counterbean) Factory.getBean ("prototypebean");

System.out.println ("Second Call: -" PrototypeBean.getCount ());} catch (filenotfoundexcection e) {

E.PrintStackTrace ();

}

}

} Step 4: Use an ant script to build and execute the main class. Just Run Ant from The Command Prompt Will Do The Trick. Beelow Are The Output from Ant.

C: / 60sec2spring / springTutorial2> Ant

Buildfile: build.xml

Build:

[Javac] Compiling 2 Source Files To C: / 60sec2spring / SpringTutorial2 / Bin

Run:

[Java] Jun 1, 2004 12:16:42 Pm org.springframework.beans.Factory.xml.xmlbeandefinitionReader loadingBeandefinitions

[Java] Info: Loading XML Bean Definitions from (No Description)

[Java] Jun 1, 2004 12:16:42 Pm Org.SpringFramework.beans.Factory.support.AbstractBeanFactory GetBean

[Java] Info: CREANG Shared Instance of Singleton Bean 'SingletonBean'

[java] ---- Singleton Bean ------------------------------------

[Java] Construct Counterbean ......

[Java] First Call: - 0

[Java] Second Call: - 1

[java] ---- Prototype Bean ----

[Java] Construct Counterbean ......

[Java] First Call: - 0

[Java] Construct Counterbean ......

[Java] SECOND CALL: - 0

Build Successful

Total Time: 2 Seconds Done. [TOC]

Bean Tutorial 3 - SETTER-BASED AND CONSTRUCTION-BASED DEPENDENCY INJECTION

Inversion of Control / Dependency Injection EXISTS in Two Major Variants:

Setter-based dependency injection is realized by calling setters on your beans after invoking a no-argument constructor to instantiate your bean. Beans defined in the BeanFactory that use setter-based dependency injection are true JavaBeans. Spring generally advocates usage of setter-based dependency Injection, Since A Large Number of Constructor Arguments Can Get Unwieldy, especially.

Constructor-based dependency injection is realized by invoking a constructor with a number of arguments, each representing a collaborator or property.Step 1: We will need two dummy classes for our tutorial, AnotherBean.java and YetAnotherBean.java.

ORG / JARCHITECT / SPRING / TUTORIAL3 / ANOTHERBEAN.JAVA

Package org.jarchitect.spring.tutorial3;

Public class annotherbean {

Public anotherbean () {

System.out.println ("Construct ANOTHERBEAN");

}

}

ORG / JARCHITECT / SPRING / TUTORIAL3 / YETANOTHERBEAN.JAVA

Package org.jarchitect.spring.tutorial3;

Public class yetanotherbean {

Public yetanotherbean () {

System.out.println ("Construct Yetanotherbean");

}

}

Step 2: We Will Create a ConstructorexampleBean and a setterexamplebean.

ORG / JARCHITECT / SPRING / TUTORIAL3 / ConstructorXampleBean.java

Package org.jarchitect.spring.tutorial3;

Public class constructorexamplebean {

PRIVATE ANOTHERBEAN Beanone;

Private Yetanotherbean Beantwo;

Private INT i;

Public ConstructorexampleBean

Anotherbean AnotherexampleBean,

Yetanotherbean YetAnotherbean,

INT i) {

THIS.Beanone = anotherexamplebean;

THIS.Beantwo = yetanotherbean;

THIS.I = I;

System.out.println ("Construction Done !!!);

}

Public constructorexamplebean () {

System.out.println ("Default Construction: Should Not Be Call !!!);

}

}

ORG / JARCHITECT / SPRING / TUTORIAL3 / SETTEREXAMPLEBEAN.JAVA

Package org.jarchitect.spring.tutorial3;

Public class setterexampleBean {

PRIVATE ANOTHERBEAN Beanone;

Private Yetanotherbean Beantwo;

Private INT i;

Public setterexamplebean () {

System.out.println ("Default Construction");

}

Public void setBeanone (anotherbean bean) {

THIS.BEANONE = bean;

System.out.println ("set beanone");

}

Public void setBeantwo (YETANOTHERBEAN bean) {

THIS.Beantwo = Bean;

System.out.println ("set beantwo");

}

Public void setintegerProperty (int i) {

THIS.I = I;

System.out.println ("Set I");

}

}

Step 3: Specify The HelloBean Class in the bean.xml

Bean.xml

"http://www.springframework.org/dtd/spring-beans.dtd">

Jarchitect Spring Tutorial 3

setter-based defendency inJection.

Constructor Dependency Injection

1

Step 4: Reate a main.java in SRC

ORG / JARCHITECT / SPRING / TUTORIAL3 / Main.java

Package org.jarchitect.spring.tutorial3;

Import java.io.fileinputstream;

Import java.io.filenotfoundexception;

Import Java.io.InputStream;

Import org.springframework.beans.Factory.xml.xmlbeanfactory;

Public class main {

Public static void main (String [] args) {

Try {

// r r r configure

InputStream IS = New FileInputStream ("bean.xml");

XMLBeanfactory Factory = New XMLBeanFactory (IS);

// instantiate setterexamplebean

Setterexamplebean setterbean =

Factory.getBean ("setterbean");

// instantiate constructorexamplebean

Constructorexamplebean constructorbean =

(Constructorexamplebean) Factory.getBean ("constructorbean");

} catch (filenotfoundexception e) {

E.PrintStackTrace ();

}

}

}

STEP 5: Use an ant script to build and execute the main class. Just Run Ant from the Command Prompt Will Do The Trick.

BELOW is The Output from Ant.

C: / 60sec2spring / springTutorial3> Ant

Buildfile: build.xml

Build:

[Javac] Compiling 5 Source Files To C: / 60sec2spring / SpringTutorial3 / Bin

Run:

[Java] Jun 1, 2004 12:36:51 Pm Org.SpringFramework.beans.Factory.xml.xmlbeandefinitionReader loadingBeandefinitions

[Java] Info: Loading XML Bean Definitions from (No Description)

[java] Jun 1, 2004 12:36:51 Pm Org.SpringFramework.beans.Factory.support.AbstractBeanFactory GetBean

[Java] Info: Creating Shared Instance of Singleton Bean 'SetterBean'

[Java] Default Constructionor

[java] construct anotherbean

[Java] Jun 1, 2004 12:36:51 Pm Org.springframework.beans.Factory.support.AbstractBeanFactory GetBean [Java] Info: Creating Shared Instance of Singleton Bean 'AnotherexampleBean'

[java] Construct Yetanotherbean

[java] set beanone

[java] set beantwo

[java] Jun 1, 2004 12:36:51 Pm Org.SpringFramework.beans.Factory.support.AbstractBeanFactory GetBean

[Java] Info: CREANG Shared Instance of Singleton Bean 'YetAnotherbean'

[java] set i

[java] Jun 1, 2004 12:36:51 Pm Org.SpringFramework.beans.Factory.support.AbstractBeanFactory GetBean

[Java] Info: CREANG SHARED Instance of Singleton Bean 'ConstructorBean'

[java] construction done !!!

[Java] Jun 1, 2004 12:36:51 Pm Org.SpringFramework.beans.Factory.support.AbstractAutowirecapableBeanFactory AutowireConstructor

[java] info: bean 'constructorbean' instantiated via constructor [public org.jarchitect.spring.tutorial3.

Constructorexamplebean (org.jarchitect.spring.tutorial3.anotherbean, org.jarchitect.spring.tutorial3.yetanotherbean, int)

Build Successful

Total Time: 2 Seconds

Overtime

From bean.xml

......

Constructor Dependency Injection

1

......

Notice the index = "2", TRY Removing it and run ant Ant, you will have the following error.

.......

[Java] Info: Creating Shared Instance of Singleton Bean 'ConstructorBean' [Java] Org.SpringFramework.beans.Factory.BeancreationException: Error Creating Bean with name

'ConstructorBean' Defined In (No Description): 3 Constructor Arguments Specified But no matching

Constructor Found in Bean 'ConstructorBean' (Hint: Specify Index Arguments for Simple Parameters to

Avoid Type Ambiguities)

[java] at Org.SpringFramework.beans.Factory.support.AbstractAutoWirecapableBeanFactory.

AutoWireconstructor (AbstractautautowirecapableBeanfactory.java: 287)

[java] at Org.SpringFramework.beans.Factory.support.AbstractAutoWirecapableBeanFactory.

CreateBean (AbstractautautowirecapableBeanfactory.java: 181)

[java] at Org.SpringFramework.beans.Factory.support.AbstractBeanFactory.getBean (AbstractBeanfactory.java: 159)

[java] at Org.jarchitect.Spring.Tutorial3.main.main (Unknown Source)

[java] Exception in thread "main"

[java] Java Result: 1

Build Successful

Total Time: 1 Second

Reason: The argument is an int, but BeanFactory take it as a String, so the mapping is not obvious to the matching algorithm In case of primitives, it's recommended to specify the argument index explicitly..

More Reason: for Example, Constructors Myclass (Bool Flag, String Toto, Int Moo, FLOAT COW). There Is No Way for the BeanFactory to Tell Which Is Which from the string representations of each.

Next, try swapping

1

And Run Ant.done.

[TOC]

转载请注明原文地址:https://www.9cbs.com/read-98023.html

New Post(0)