First Spring
First we have to get Spring's related files. Spring's file is placed on SourceForge, the URL is: http://sourceforge.net/project/showfiles.php? Group_id = 73357
When you write this article, Spring's latest version is
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-dependengies include some Ant, Jakarta-Commons, Struts, Velocity Wait other open source Java project dependent files, if you also need these related files, you can download this version. If you already have these files, you only need to download 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 single-machine 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 soon as the first Spring program, as long as Spring-core.jar can, it is the only other task file, 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:
package onlyfun.caterpillar; 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 converbeas through SETER, but we don't have to write programs to make these things, but in confirming file definition, from Spring to us For the set action, we write bean.xml:
xml version = "1.0" encoding = "UTF-8"?>
package onlyfun.caterpillar; 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 IOException {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? Refers to the Hellobean variance of String objects, through the interface reserved by Setter, we use setter injection to complete this dependent injection, not to write the words in Hellobean, beanfactory is the focus of the whole spring, the core of the entire Spring Holding it, using XMLBeanFactory here, responsible for reading XML configuration files, of course we can also use the Properties file.
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:00ORG.SPRINGFRAMEWORK.Beans.Factory.xml.xmlbeandefinitionReader loadBeandefinitions Information: loading xml bean definitions from resource for InputStream2004 / 10/21
10: 28: 00org.springframework.beans.Factory.support.AbstractBeanFactory GetBean Information: CREANGDISTANCE 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 acomer point of view, it means that if you want to change the dependencies between some items, Just modify the group state file without modifying any row of components.