Spring|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 dependencies1. Generate project structure with Maven Issue below Maven command to create a standard Java project structure.
In Spring 2.5.x, almost the entire Spring modules are grouped into a singlespring.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.
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 Eclipse’s 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 Maven’s pom.xml file. The Spring dependencies are available for download via Maven central repository.
File : pom.xml
4.0.0
com.mkyong.core
Spring3Examplejar
1.0-SNAPSHOT
Spring3Example
http://maven.apache.org 3.0.5.RELEASE
org.springframework
spring-core
${spring.version}
org.springframework
spring-context
${spring.version}
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
5. Review project structure Review directory structure as follows
文章图片
Spring3-hello-world-example.png 6. Run It 【Spring|Spring 3 hello world example】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
推荐阅读
- opencv|opencv C++入门
- java|springboot+nodejs+Vue影视作品网站java
- 统一配置管理java_Spring Cloud Alibaba(三)(使用 Nacos config 实现统一配置管理)
- Nacos|二、SpringCloud框架搭建之Nacos配置中心
- Spring|Spring-IOC配置-依赖注入
- Java|霸占GitHub热榜的《Spring Cloud Alibaba源码笔记》果然“威力极大”
- 大数据|80岁还在写代码!Hello World发明人、UNIX命名者项目登上GitHub热榜
- springboot|企业微信通过群聊机器人用springboot发送信息
- mysql一级缓存和二级缓存_SpringBoot+Mybatis一级缓存和二级缓存详解
- Spring详细解读事务管理