SSH spring and sshspring
It has been more than a year since I heard of spring for the first time. For this unfamiliar framework, I can only say, love, and hate. Love is because it is too powerful, I have never understood what spring is. Let's start with it today:
I. Description
In a word, spring is a lightweight container framework centered on control inversion and aspect orientation.
However, there is a large amount of information in this sentence. As for the detailed explanation, I don't think Baidu understands it, but it can be said that: spring is created to solve the complexity of enterprise application development. One of its advantages is that it is a layered architecture that allows you to choose to use any component, in addition, it does not aim to replace any existing framework, but to integrate well.
Ii. Simple Application
Let's start with the application. In fact, spring applications are simpler than struts applications. What's different from struts is that spring can be used not only in web projects but also in java background projects, this small instance will be explained later:
Step 1: Create a java project in eclipse:
Step 2: add necessary references:
(Note: The created java project may not have a lib folder, and you can choose to create it yourself) copy the necessary jar package to lib:
Right-click springdemo2-properties-Java build path-addlibrary-user library-new library-addjars to add
Step 3: Create a test java class:
Package com. tgb. spring; public class HelloWord {/*** defines the attribute for Obtaining values */private String name; public String getName () {return name;} public void setName (String name) {this. name = name;} public void hello () {System. out. println ("hello:" + name );}}
Step 4: Create the spring configuration file applicationcontext. xml:
<? Xml version = "1.0" encoding = "UTF-8"?> <Beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi: schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <! -- Configure bean --> <bean id = "helloworld" class = "com. tgb. spring. helloWord "> <property name =" name "value =" Chen Lina "> </property> </bean> </beans>
Step 5: test the running result:
package com.tgb.spring;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Main {public static void main(String []args){ ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationcontext.xml"); HelloWord helloworld=(HelloWord) ctx.getBean("helloworld"); helloworld.hello();}}
The code here is mainly to get the helloworld instance through the spring container instead of directly creating the helloworld instance, which is exactly the role of the spring container.
Running result:
Is it amazing that the spring container not only creates an instance, but also has a value for the name attribute in the instance, all because the spring container, this method of setting attributes for the object is also called control inversion.
Spring is amazing, but I only know a little bit about it!