Build a spring development environment and write the first spring applet

Source: Internet
Author: User
Tags define abstract

First, I wrote a spring framework of the basic knowledge article, which does not describe how to configure the spring development environment, today, if you do not know how to download the spring package, you can read my article:

http://blog.csdn.net/u012561176/article/details/45971917, which describes 2 ways to get the spring package.

It is recommended that you understand the principles of IOC (inversion of control) before you configure the spring environment, and you can read the article I wrote: http://blog.csdn.net/u012561176/article/details/45974315, At the bottom of this article are an introduction to IOC main components and two ways of injecting.

Below, I will describe how to build the spring environment in two ways, the first of which is to use the MyEclipse tool to automatically configure spring, the second reason we manually configure, what is the difference between, there is no big difference, the first kind of slightly simple, the second kind of slightly complex, But the second way to configure a higher version of spring, we look at personal hobbies!




Two. The first way: automatic configuration mode.

(1). First, create a new Java project with the project name One_spring.


(2). Select this Java project, right-click, select MyEclipse under the Add Spring capabilites ... This option can also be selected from the menu bar, after selection, as shown in:

In order for us to create this Java project can be ported, so we can select the last arrow to point to, after selection, as shown in:

Click the Browse button, select the folder where you placed the jar package, and then click on it as shown below:

When you click Create New folder, a new one is called LIB, which specifically places the jar package:

Click OK, then we can click on the Next button:

After clicking Next, as shown:

Click Finish button to complete the automatic configuration of the Spring development environment, click the Finish button, the project file structure as shown:

Where ApplicationContext is the spring configuration file.


(3). Below we can write a relatively simple spring applet.

First, create a new interface animal, placed under the Com.inter package, that is, the Animal.java file, the code is as follows:

Package Com.inter;public interface Animal {void eat ();//define abstract eating method void La ();//define abstract excretion method}


Then create a new dog class, placed under the Com.bean package, the implementation of the animal interface, the implementation of the animal interface of the abstract method, that is, Dog.java file, the code is as follows:

Package Com.bean;import Com.inter.animal;public class Dog implements animal{@Overridepublic void Eat () {//TODO Auto-gene Rated Method StubSystem.out.println ("Dog Eats dog Food");} @Overridepublic void La () {//TODO auto-generated method StubSystem.out.println ("Dog pulls the dog poo");}}

Then create a new cat class, placed under the Com.bean package, implemented the animal interface, the implementation of the animal interface abstract method, that is, Cat.java file, the code is as follows:

Package Com.bean;import Com.inter.animal;public class Cat implements animal{@Overridepublic void Eat () {//TODO Auto-gene Rated method StubSystem.out.println ("Cat Eats cat food"); @Overridepublic void La () {//TODO auto-generated method StubSystem.out.println ("Cat pull Cat Poo");}


Next we can configure the two beans objects of dog and Cat in the Applicationcontext.xml file, with the following code:

<?xml version= "1.0" encoding= "UTF-8"? ><beansxmlns= "Http://www.springframework.org/schema/beans" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:p= "http://www.springframework.org/schema/p" xsi: schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-3.0.xsd "><bean id=" dog "class=" Com.bean.Dog "></bean><bean id=" Cat "class=" Com.bean.Cat "></bean></beans>


Where the ID in the bean tag is the ID for the object of the class, so that the following tags and code are available, class is the classpath, the package name + class name.


Finally, we create a new test class animaltest, placed under the Com.test package, that is, the Animaltest.java file, the code is as follows:

Package Com.test;import Org.springframework.context.applicationcontext;import Org.springframework.context.support.classpathxmlapplicationcontext;import Org.springframework.context.support.filesystemxmlapplicationcontext;import Com.bean.cat;import Com.bean.Dog; public class Animaltest {public static void main (string[] args) {ApplicationContext ac=new Classpathxmlapplicationcontext ("Applicationcontext.xml");//query to config file and assign to AC this ApplicationContext application context object Dog d= (dog Ac.getbean ("dog");//Get the ID in the config file for the Bean Object D.eat ();//Call the Eat Method d.la ();//Call the Excretion method cat c= (CAT) Ac.getbean ("cat");// Get the ID in the config file for cat this Bean Object c.eat ();//Call Eat Method c.la ();//Call the Excretion Method}}


The project file structure looks like this:


The post-operation effect is as follows:


(4). So the spring environment is configured successfully.





Three. The second way: Manually configure the spring environment mode.

(1). First, download the required spring packages, I downloaded the version of Spring4.1.6, after downloading a few folders, the configuration of spring required jar package is libs, as shown in:


(2). Next we will create a new Java project in the MyEclipse tool, with the project name One_spring1, as shown in:

Then select the project, right-click the build path->configure build Path ... This option, after the point is opened, is as follows:

Click on the right add External JARS ... button, which is the red arrow pointing, adds the jar package of the Libs folder under the software package we downloaded, and we can add the core jar package, as shown in:

Where the red box is spring's core jar package, but a jar package is missing, This jar package I download 4.1.6 version of the package can not find this jar package, this jar is Commons-logging-1.1.3.jar, this jar package I was in the STRUTS2 installation package to get, we also added, as shown in:

Click the OK button, and if it is a Web project, import these core jar packages into the Lib folder under Web-inf.


(3). Then we create a new spring applicationcontext.xml configuration file under the SRC directory under the project, in fact we can also build beans.xml, but often is the first new, this file we can find from the spring's official documents, here I directly attached template:

<?xml version= "1.0" encoding= "UTF-8"? ><beansxmlns= "Http://www.springframework.org/schema/beans" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:p= "http://www.springframework.org/schema/p" xsi: schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-3.0.xsd "></beans>

We can add the bean tag to the beans tag and so on!


(4). This will configure the spring environment, then write the test class!

First, write an interface person, placed under the Com.inter package, that is, the Person.java file, the code is as follows:

Package Com.inter;public interface Person {void eat ()///define abstract eating method void drink ();//define abstract drink Method}


Then the definition of two classes, respectively Northman class and Southman class, are placed under the Com.bean package, implements the person interface, but also implements the abstract method in the interface

The Northman.java file code is as follows:

Package Com.bean;import Com.inter.person;public class Northman implements person{@Overridepublic void Eat () {//TODO Auto -generated method StubSystem.out.println ("Northern people like to eat pasta");} @Overridepublic void Drink () {//TODO auto-generated method StubSystem.out.println ("Northern people like to drink");}}


The Southman.java file code is as follows:

Package Com.bean;import Com.inter.person;public class Southman implements person{@Overridepublic void Eat () {//TODO Auto -generated method StubSystem.out.println ("Southern people like to eat");} @Overridepublic void Drink () {//TODO auto-generated method StubSystem.out.println ("Southerners like Tea");}


Then in the Applicationcontext.xml configuration file configuration beans, namely Northman and Southman class, Applicationcontext.xml file code is as follows:

<?xml version= "1.0" encoding= "UTF-8"? ><beansxmlns= "Http://www.springframework.org/schema/beans" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:p= "http://www.springframework.org/schema/p" xsi: schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-3.0.xsd "><bean id=" Northman "class=" Com.bean.NorthMan "></bean><bean id=" SouthMan " class= "Com.bean.SouthMan" ></bean></beans>


Finally, write the test class tests, placed under the Com.test package, the code is as follows:

Package Com.test;import Org.springframework.context.applicationcontext;import Org.springframework.context.support.filesystemxmlapplicationcontext;import Com.bean.northman;import Com.bean.southman;public class Test {public static void main (string[] args) {ApplicationContext ac=new Filesystemxmlapplicationcontext ("Src/applicationcontext.xml");//Use the file system to query the Applicationcontext.xml configuration file Northman n= ( Northman) Ac.getbean ("Northman"); N.eat (); N.drink (); Southman s= (Southman) Ac.getbean ("Southman"); S.eat (); S.drink ();}}


The project structure file looks like this:


The post-operation effect is as follows:


(5). This allows the spring environment to be configured successfully.






Four. Summary: Whether it is manual configuration or automatic configuration of the spring environment, are very much the same, see what you like to use, above I configured the spring environment in 2 ways, also wrote 2 Java project to test whether the configuration is successful! One of the first items in the test class animaltest is the way to find the configuration file for the Classpath, the second item for the test class, and the way to find the configuration file for the file system. The configuration file can be placed at will, but this configuration file must be found! Note: Everyone still put the configuration file under the SRC directory, using classpath way to find! This is easier, and there is no error!




Five. The above content is only for your study reference, write not good, please forgive me, if there are errors, please point out, thank you!

Build a spring development environment and write the first spring applet

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.