Spring Getting Started

xiaoxiao2021-03-06  52

Spring Getting Started

Java boutique books, learning must be available to Lag 2004-11-17 11: 09: 51708 View

First we have to get Spring's related files, and Spring's file is placed on the SourceForge, the URL is:

http://sourceforge.net/project/showfiles.php?group_id=73357

When you write this article, Spring has the latest version of 1.1.1, there are two download versions, one is Spring-framework-1.1.1-with- dependencies.zip, one is Spring-Framework-1.1.1.zip, with- Dependencies include some other Open Source Java projects for other open source Java projects, if you also need these related files, you can download this version, if you already have these related files, just download Spring- Framework-1.1.1.zip This file.

After downloading the zip file and decompressed, in the dist directory is the related files you need to use Spring. If you download the with-dependendencial version, you can use the dependent file you might be used in the lib directory. Under the Dist directory, Spring-core.jar is the core of Spring. For a simple stand-alone program, use this core, if you need to use Spring other sub-frame support, add other JAR files to However, for example, Spring-aop.jar, Spring- Webmvc.jar, etc. You can also use the Spring.jar file directly, which includes all the categories required for all Spring support, and no longer need to join individual JAR files.

As far as our first Spring program, as long as Spring-core.jar can, it is the only other project files, which is commons- logging.jar, you can find this in the jakarta-commons directory of the lib directory, The position of the two files is added to ClassPath, we can start writing the first Spring program.

To write our first component (Component), it is just a simple JavaBean, which is used to hear a new user:

Hellobean.java

Package online.

Public class hellobean {

Private string helloword = "Hello! World!";

Public void sethelloword (string helloword) {

THIS.HELLOWORD = HelloWord;

}

Public string getHelloword () {

Return HelloWord;

}

}

Hellobean has a preset "Hello! World!" String, we can also set new converbers through Setter, but we don't personally write programs to make these things, but in confirming file definition, from Spring. For our set action, we write bean.xml:

Bean.xml

Hello! Justin!

Bean.xml defines the JavaBean alias and source categories, the tab settings we want to inject to Javabean, bean.xml must be in the directory you can access in your classpath, perhaps it is current The working directory, can be in the web program, we use the single-machine program in the CLASSES directory, we will read bean.xml using FileInputStream, so put it in the current working directory, then we write one Simple test procedure:

Springtest.java

Package online.

Import java.io. *;

Import org.springframework.beans.Factory.BeanFactory;

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

Public class springtest {

Public static void main (string [] args) throws oException {

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

Beanfactory factory = new xmlbeanfactory (IS);

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

System.out.println (Hello.getHelloword ());

}

}

This is from the relatively low-level angle using Spring IOC container feature, and is to read the configuration file and complete the dependence on the dependencies. What is the dependence on this? It refers to the Hellobean phase, which is used by the String object. Through the interface reserved by Setter, we use setter injection to complete this dependency injection, not to write the words in Hellobean, beanfactory is the focus of the entire Spring, the core of the entire Spring Around it, use XMLBeanFactory here, responsible for reading XML configuration files, of course, we can also use the Properties file, which will be introduced later.

BeanFactory reads the configuration of the bean and completes the relationship maintenance, we can get an instance by getBeBean () method and specify the alias of the bean to see the effect after the actual operation:

2004/10/21 10:28:00 am Org.springframework.beans.Factory.xml.xmlbeandefinitionReader loadingBeandefinitions

Information: Loading XML Bean Definitions from Resource for InputStream2004 / 10/21 Amm 10:28:00 Org.SpringFramework.beans.Factory.support.AbstractBeanFactory GetBean

Information: Creating Shared Instance of Singleton Bean 'Hellobean'

Hello! Justin!

If you want to change the converense today, you can change bean.xml, you don't have to modify the main program, from a relatively general perspective, it means that if you want to change the dependencies between some objects, As long as the group state file is modified, no one of the rows of the component is modified.

Welcome to reprint in the case of retaining http://www.javajia.com!

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

New Post(0)