60 Seconds to Spring (1)

zhaozj2021-02-12  146

Original address: http://gabriel.jarchitect.org/spring/index.html

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.JAVA

Package org.jarchitect.spring.tutorial1; public class hellobean {public string Sayhelloworld () {return world;}}

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

Bean.xml

Version = "1.0" Encoding = "UTF-8"?> beans public "- // Spring // DTD bean // en" http://www.springframework.org/dtd/spring-beans.dtd ">

Jarchitect

Spring

TUTORIAL

1

ID =

"Hellobean"

Class =

"Org.jarchitect.Spring.Tutorial1.Hellobean"

/>

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 {// Read the configuration file 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 (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.

BELOW is The Output from Ant.

C: / 60sec2Spring / SpringTutorial1> ant Buildfile: 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 loadBeanDefinitions [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: Creating Shared Instance of Singleton Bean 'HelloBean' [Java] Hello World Build Successful Total Time: 3 Seconddone.

[TOC]

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

New Post(0)