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
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
public class Springtest {
public static void Main (string[] args) throws IOException {
InputStream is = new FileInputStream ("Bean.xml");
Beanfactory factory = new Xmlbeanfactory (IS);
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:
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.