A detailed introduction to the spring example to explain the application

Source: Internet
Author: User
Tags configuration settings string version zip
First we have to get the relevant files for spring, and the spring files are on the SourceForge, the Web site is:
  
http://sourceforge.net/project/showfiles.php?group_id=73357
  
When writing this article, Spring's latest version is 1.1.1, with two downloads and one spring-framework-1.1.1-with- Dependencies.zip, one is spring-framework-1.1.1.zip,with-dependencies, including some ant, Jakarta-commons, struts, Velocity and so on other open source Java project Dependencies file, if you also need these related files, you can download this version, if you already have these related files, you only need to download spring-framework-1.1.1.zip this file.
  
After downloading the zip file and decompressing it, in the Dist directory is the relevant files that spring requires, and if you are downloading the with-dependencies version, then the dependent files that you might use in the Lib directory are available. In the Dist directory, Spring-core.jar is the core of spring, and for writing simple stand-alone programs, you can use this core, and if you later need to use the other child framework support of spring, add other jar files, such as Spring-aop.jar, Spring-webmvc.jar and so on. You can also use the Spring.jar file directly, which includes all the categories required by all of the spring-supported features, without the need to add individual jar files.
  
As far as our first spring program, as long as Spring-core.jar this file, it is the only dependencies of other project files, is commons- Logging.jar, you can find it in the Jakarta-commons directory of the Lib directory, add the location of these two files to classpath, and we can start writing the first spring program.
  
To compose our first component (component), it's just a simple javabean to greet new users:
  
Hellobean.java
  
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 a new greeting through the setter, but instead of writing the program to do it ourselves, we define the configuration file by spring, and we write Bean.xml:
Bean.xml
  
<?xml version= "1.0" encoding= "UTF-8"?>
<! DOCTYPE beans Public "-//spring/dtd bean/en" "Http://www.springframework.org/dtd/spring-beans.dtd" >
<beans>
<bean id= "Hellobean" class= "Onlyfun.caterpillar.HelloBean" >
<property name= "Helloword" ><value>hello! Justin!</value></property>
</bean>
</beans>
  
Bean.xml defines the JavaBean alias and source category,<property> tag that sets the string value we want to inject into the javabean. Bean.xml must be in the directory that your classpath can access, perhaps the current working directory, in the Web program can be in the classes directory, we use a stand-alone program, we will use the FileInputStream read bean.xml, so put it in the current work Directory, and then we write a simple test program:
Springtest.java
  
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 the use of spring's IOC container functionality from a relatively low-level perspective, with beanfactory to read the configuration file and complete dependency injection, what is the dependency here? Refers to the Hellobean dependent on the string object, and through the interface retained by the setter, we use the setter injection to complete this dependency injection instead of writing the greeting to death in Hellobean, Beanfactory is the focus of the whole spring, and the whole spring revolves around it, using xmlbeanfactory to read XML configuration files, and of course we can use properties files, which we'll introduce later.
  
After beanfactory reads the configuration settings of the bean and completes the relationship maintenance, we can take the example by Getbean () method and specifying the bean alias to see the effect after the actual run:
  
2004/10/21 Morning 10:28:00 Org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadbeandefinitions
  
Info: Loading XML Bean definitions from resource for InputStream
  
2004/10/21 Morning 10:28:00 Org.springframework.beans.factory.support.AbstractBeanFactory Getbean
  
Info: Creating shared instance of Singleton Bean ' Hellobean '
  
Hello! justin!

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.