Spring hello world

11.02.2019
0
18



Spring 3. MVC Hello world

, MVC Spring framework, Spring MVC.

MVC (Model-view-controller, --, --) , , , .

pom.xml dependency:

<properties> <spring.version>3.0.5.RELEASE</spring.version> </properties> <dependencies> <!-- Spring 3 dependencies --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> </dependencies>

Controller & Mapping

Spring 3, .@RequestMapping , 2.5, URL- REST Spring MVC.

package com.devcolibri.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller @RequestMapping("/welcome") public class HelloController { @RequestMapping(method = RequestMethod.GET) public String printWelcome(ModelMap model) { model.addAttribute("message", "Spring 3 MVC - Hello World"); return "hello"; } }

JSP

JSP .

<html> <body> <h1>Message : ${message}</h1> </body> </html>

Spring Configuration

Spring 3, auto component scanning ( ) view resolver .

: mvc-dispatcher-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" 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-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:component-scan base-package="com.devcolibri.controller" /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix"> <value>/WEB-INF/pages/</value> </property> <property name="suffix"> <value>.jsp</value> </property> </bean> </beans>

<context:component-scan base-package="com.devcolibri.common.controller" /> spring, .
<value>/WEB-INF/pages/</value> spring, views() WEB-INF/page
<value>.jsp</value> views jsp. , view.

web- Spring

, Spring 2.5, Spring "ContextLoaderListener" "DispatcherServlet".

: web.xml

<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>Spring MVC Application</display-name> <servlet> <servlet-name>mvc-dispatcher</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>mvc-dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> </web-app>

Demo

URL :http://localhost:8080/[name project]/welcome

: http://www.mkyong.com


spring hello world
Spring - Hello world example

In this post, we will learn how to create a simple Spring application using Eclipse IDE and Maven build tool.

Technologies used for this application are -

  • Spring 4.3.4.RELEASE
  • Eclipse Mars.2 (4.5.2)
  • Maven 3.3.9
  • JavaSE 1.8

So let us proceed to write a simple Spring application which will printHello Spring.

Step 1 -Create a Java Project

Go to File menu ?New ? Other ? Java ? Java Project

In New Java Project wizard, just add Project Name as SpringHelloWorldand click on Finish.

Step 2 - Convert Java Project to Maven

Right click on SpringHelloWorld project ? Configure ? Convert to Maven Project.

Please refer the below link to learn more about conversion of Java project to maven project.

http://www.boraji.com/how-to-convert-java-project-to-maven-project-in-eclipse

Step 3 Add Spring dependency to Maven pom.xml

Open pom.xml file and add required spring dependency to it. Here is the pom.xml file.

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>SpringHelloWorld</groupId> <artifactId>SpringHelloWorld</artifactId> <version>0.0.1-SNAPSHOT</version> <name>SpringHelloWorld</name> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.4.RELEASE</version> </dependency> </dependencies> <build> <sourceDirectory>src</sourceDirectory> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.5.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build>
</project>

Step 4 Create a Spring bean class

Let's create our first spring bean class under com.boraji.tutorial.spring package and write the following code in it.

HelloWorld.java

package com.boraji.tutorial.spring; /** * @author imssbora */
public class HelloWorld { public void sayHello() { System.out.println("Hello Spring!!"); }
}

Step 5 Create a bean configuration file

Create an xml file, named as beans.xml, under resources folder and write the following code in it.

beans.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"> <bean id="helloWorld" class="com.boraji.tutorial.spring.HelloWorld" /> </beans>

Now create a main class under com.boraji.tutorial.spring package as shown below.

MainApp.java

package com.boraji.tutorial.spring; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /** * @author imssbora */
public class MainApp { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); HelloWorld helloWorldBean = context.getBean("helloWorld", HelloWorld.class); helloWorldBean.sayHello(); }
}

Step 7 Review the Project structure

Here is the project structure of SpringHelloWorld application.

spring-hello-world.png

Step 8 Run application

Right click on MainApp.java ? Run as ? Java Application.

Output

Hello Spring!!

Download Sources

SpringHelloWorld.zip10.07 KB


Spring 3 hello world example

This tutorial shows you how to create a simple hello world example in Spring 3.0.

Technologies used in this article :

Spring 3.0.5.RELEASE Maven 3.0.3 Eclipse 3.6 JDK 1.6.0.13

P.S Spring 3.0, at least JDK 1.5 is required to work.

Spring 3.0 dependencies
In Spring 2.5.x, almost the entire Spring modules are grouped into a single spring.jar file. Since Spring 3.0, every modules are split into an individual jar file, for example, spring-core, spring-expression, spring-context, spring-aop and etc, for detail , please refer to this article Obtaining Spring 3 Artifacts with Maven.

1. Generate project structure with Maven

Issue below Maven command to create a standard Java project structure.

 mvn archetype:generate -DgroupId=com.mkyong.core -DartifactId=Spring3Example -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false 

2. Convert to Eclipse project

Convert Maven style project to Eclipses style project, and import into Eclipse IDE.

 mvn eclipse:eclipse 

2. Add Spring 3.0 dependency

Add the Spring 3.0 dependencies listed below in Mavens pom.xml file. The Spring dependencies are available for download via Maven central repository.

File : pom.xml

 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.mkyong.core</groupId> <artifactId>Spring3Example</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name>Spring3Example</name> <url>http://maven.apache.org</url> <properties> <spring.version>3.0.5.RELEASE</spring.version> </properties> <dependencies> <!-- Spring 3 dependencies --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> </dependencies> </project> 

3. Spring bean

A simple Spring bean.

 package com.mkyong.core; /** * Spring bean * */ public class HelloWorld { private String name; public void setName(String name) { this.name = name; } public void printHello() { System.out.println("Spring 3 : Hello ! " + name); } } 

4. Spring bean configuration file

Create a Spring configuration file, and declare all the available Spring beans.

File : SpringBeans.xml

 <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-3.0.xsd"> <bean id="helloBean" class="com.mkyong.core.HelloWorld"> <property name="name" value="Mkyong" /> </bean> </beans> 

5. Review project structure

Review directory structure as follows

spring3 hello world example

6. Run It

Run it.

 package com.mkyong.core; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class App { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext( "SpringBeans.xml"); HelloWorld obj = (HelloWorld) context.getBean("helloBean"); obj.printHello(); } } 

7. Output

 Spring 3 : Hello ! Mkyong 

Download Source Code

Download it Spring3-hello-world-example.zip (5KB)

References

Spring 3 artifacts with Maven/
? !
:
:
   2017
01.01.2019
- - 2017. Victoria Beckham
20.12.2018
Melanie Thierry Photos Photos - Melanie Thierry attends the Christian Dior show as part of the Paris Fashion Week Womenswear Spring
23.08.2018
Spring Pink- 12/14. / .
Kaia gerber
01.03.2018
Anais mali
26.02.2018
23.02.2018
Mira looked gorgeous in a blush pink embroidered tissue dress from Anita Dongre's Spring 2017 collection. She let the
19.02.2018
Sybil stallone vk
17.02.2018
13.02.2018
19.01.2018
07.01.2018
If you're new, Subscribe! > http://bit.ly/Subscribe-to-Nicki-Swift. From starring along inside Jim Carrey in the
05.01.2018