Spring 2 for beginners of Java ssh, sshspring

Source: Internet
Author: User

Spring 2 for beginners of Java ssh, sshspring

In the previous article, we successfully set up the spring development environment. Next, let's take a look at spring's amazing things (I am also a newbie, and I am not quite right about it. I hope that it can be easily modified, thank you ).

I have heard that spring has many applications for xml files, but how to use it has always been in the fog of the cloud. When I was learning it at the beginning, I did not directly create a web project, applicationContext is not automatically generated. xml, I don't know how important this configuration file is, so after I write the following code, I always report errors during runtime, and I was so silly that the file will be automatically generated, it's ridiculous to think about it now. However, if the spring rack package is automatically imported into myeclipes, it will be generated automatically. We would like to remind you that you must first confirm the existence of applicationContext. xml before proceeding with code development. (For this problem, you don't have to read it carefully)

ApplicationContext. xml code I have given in the previous blog, I am stored in the src directory, here I give the link: http://www.cnblogs.com/speedwade/p/3968101.html

Next, I wrote a test applet to check whether spring has been successfully configured. The path is stored in the src directory com. sep. under the basic Package, create a new class named SpringTest. Because the file already exists, the prompt is not displayed when the file is created for the first time,

Then the code is

1 package com. sep. basic; 2 3 import org. springframework. context. applicationContext; 4 import org. springframework. context. support. classPathXmlApplicationContext; 5 6 public class SpringTest {7 public static void main (String args []) 8 {9 // create applicationcontext10 ApplicationContext ctx = new ClassPathXmlApplicationContext ("applicationContext. xml "); 11 // output spring container 12 System. out. println (ctx); 13} 14}

Right-click the new file and choose Run As/Java Application. The following code appears on the console:

1 org. springframework. context. support. ClassPathXmlApplicationContext @ b749757: startup date [Mon Sep 15 17:21:18 CST 2014]; root of context hierarchy

Next, let's explain that in SpringTest. java, we only created the ApplicationContext instance, and it is the Spring container. Only after obtaining the container can we successfully access the Bean in the container. The output is exactly the ApplicationContext instance we created. Most of the functions in Spring are implemented through this container. So we can see that it is powerful.

Next we will create a simple bean to see how Spring works.

First, create a simple java class file named FirstBeanTest. The path is under the com. sep. basic. service package. The code is:

1 package com. sep. basic. service; 2 3 public class FirstBeanTest {4 private String name; 5 // setter Method 6 public void setName (String name) {7 this. name = name; 8} 9 // The info method of the test class 10 public void Info () 11 {12 System. out. println ("FirstBean's Name is" + name); 13} 14}

The above shows a member variable and Its set method.

Then add the following code in applicationContext. xml:

1 <! -- Deploy the FirstBeanTest class to the bean in Spring --> 2 <bean id = "firstBeanTest" class = "com. sep. basic. service. firstBeanTest "> 3 <property name =" name "value =" success "> </property> 4 </bean>

Next, return to the SpringTest file and add the following code:

1 FirstBeanTest fbt = ctx.getBean("firstBeanTest",FirstBeanTest.class);2 3 fbt.Info();

Well, run it now. You can see the output statement FirstBean's Name is success.

Is it amazing? We have not manually created a FirstBeanTest instance. Why is it still assigned a value? This is the responsibility of Spring. We only need to use spring containers to obtain instance objects and assign values. This is the credit of spring. spring will collect the configuration information, obtain the corresponding instance and set the corresponding attribute value.

This is what we often hear about control inversion (IOC ).

  

 


How to test Java SSH2 Spring transactions

Let's take a look at the data in the database and compare it. Rollback is not performed.
 
A simple java ssh framework source code

Do you still have no idea where the source code is and how to configure it? I suggest you find some videos on the Internet and try it with the video, record all the steps and re-configure them after reading them. If there is something unclear in the middle, you can take the note and mark it and read it several times, when the configuration is too many, you will be skilled.

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.