Build a Spring development environment and write the first Spring Applet

Source: Internet
Author: User

Build a Spring development environment and write the first Spring Applet

I. I wrote an article on the basic knowledge of the Spring framework, which does not cover how to configure the Spring development environment. Today, let's talk about it. If you don't know how to download the Spring software package

Next, I will use two methods to describe how to build a Spring environment. The first is to use the built-in MyEclipse tool to automatically configure Spring. The second is to manually configure the environment, is there any difference? There is no big difference. The first is a little simple, and the second is a little complicated, but the second method can be used to configure a higher version of Spring!

 

 

 

2. Method 1: automatic configuration.

(1) first, create a Java project named one_spring.

 

(2) select this Java project, right-click it, and select Add Spring Capabilites... under MyEclipse. You can also select it from the menu bar, as shown in:

To facilitate the migration of the Java project we created, we can select the point pointed by the last arrow and select it, as shown in:

Click the Browse button, select the folder for storing the jar package, and click it, as shown in:

Click Create New Folder to Create a Folder named lib, which is used to place the jar package:

Click OK, and then click the Next button:

Click Next, as shown in:

Click Finish to automatically configure the Spring development environment. After you click Finish, the project file structure is shown in:

ApplicationContext is the Spring configuration file.

 

(3) Below we can write a simple Spring applet.

First, create an Animal interface and place it in the com. inter package, that is, the Animal. java file. The Code is as follows:

Package com. inter; public interface Animal {void eat (); // define the abstract eating method void la (); // define the abstract excretion method}

 

Create a new Dog class and place it in the com. bean package to implement the Animal interface and implement the abstract method of the Animal interface, that is, the 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-generated method stubSystem. out. println (dog food);} @ Overridepublic void la () {// TODO Auto-generated method stubSystem. out. println (dog shit );}}

 

 

 

 

Create a new Cat class and place it in the com. bean package to implement the Animal interface and implement the abstract method of the Animal interface, that is, the 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-generated method stubSystem. out. println (cat food);} @ Overridepublic void la () {// TODO Auto-generated method stubSystem. out. println (modemy );}}


Next, we can configure the Dog and Cat beans in the applicationContext. xml file. The specific code is as follows:

 
 
  
  
 


The id in the bean tag is the id of the class object, so that the following labels and code can be used. class is the class path, that is, the package name + class name.

 

Finally, we create a test class AnimalTest and place it in 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 the configuration file and assign it to the application context object Dog d = (Dog) ac. getBean (dog); // get the bean object d whose id is dog in the configuration file. eat (); // call the eat method d. la (); // call the excretion method Cat c = (Cat) ac. getBean (cat); // obtain the bean Object c. eat (); // call the eat method c. la (); // call the excretion method }}

 

Shows the project file structure:


The effect after running is as follows:

 

(4). In this way, the configuration of the Spring environment is successful.

 

 

 

 

Iii. Method 2: manually configure the Spring environment.

(1 ). first, download the required Spring software package. I downloaded the Spring4.1.6 version. After the download, I got several folders. The jar package required for Spring configuration is under libs, as shown in:

 

(2). Next, create a Java project named one_spring1 in MyEclipse, as shown in:

Select this project, right-click Build Path-> configure Build Path..., and click this option, as shown in:

Click Add External JARS... button, that is, the red arrow points to, and adds the jar package in the libs folder under the downloaded package. We can add the core jar package, as shown in:

The core jar package of Spring is highlighted in red, but a jar package is missing. This jar package cannot be found when I download the software package of version 4.1.6, this jar package is commons-logging-1.1.3.jar, this jar package I got in the Struts2 installation package, we also add in, as shown in:

Click the OK button, if it is a Web project, the core jar package is imported into the lib folder under the WEB-INF.

 

(3 ). next, create a Spring applicationContext under the src directory under the project. the xml configuration file can also be used to create new beans. xml, but it is often created as the first type. We can find this file from the official Spring documentation. Here I directly attach the template:

 
 

We can add bean labels in beans labels!

 

(4). Now we have configured the Spring environment, and the next step is to compile the test class!

First, write an interface Person under the com. inter package, that is, the Person. java file. The Code is as follows:

Package com. inter; public interface Person {void eat (); // defines the abstract eating method void drink (); // defines the abstract drinking method}


Define two classes, namely the NorthMan class and the SouthMan class, and place them in the com. bean package to implement the Person interface and abstract methods in the interface.

The code of the NorthMan. java file 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 pasta);} @ Overridepublic void drink () {// TODO Auto-generated method stubSystem. out. println (The northerner prefers to drink );}}


The code of the SouthMan. java file 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 (Southerners like to eat);} @ Overridepublic void drink () {// TODO Auto-generated method stubSystem. out. println (Southerners prefer tea );}}


Configure the beans in the applicationContext. xml configuration file, that is, the NorthMan and SouthMan classes. The applicationContext. xml file code is as follows:

 
 
  
  
 


Finally, write the Test class and place it in 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 applicationContext. xml configuration file NorthMan n = (NorthMan) ac. getBean (northMan); n. eat (); n. drink (); SouthMan s = (SouthMan) ac. getBean (southMan); s. eat (); s. drink ();}}

 

Shows the project structure file:

 

The effect after running is as follows:

 

(5). In this way, the configuration of the Spring environment is successful.

 





Iv. Conclusion: whether it is manual or automatic configuration ?? The Spring environment is similar. See how you like it. I configured the Spring environment in two ways and wrote two Java projects to test whether the configuration was successful! In the test class AnimalTest of the first project, the path of the configuration file is the class path, and the method of searching the configuration file for the test class of the second project is the file system. The configuration file can be placed at will, but you must find this configuration file! Note: You can still store the configuration file under the src directory and use the class path to find it! This is relatively simple and there will be no errors!



 

Related Article

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.