第三章 快速搭建Spring4.x项目
来源:互联网 发布:玄空排盘软件下载 编辑:程序博客网 时间:2024/06/10 19:29
以下操作使用Spring Tool Suite(简称STS,Spring官方推出的基于Eclipse的开发工具)进行操作。
一、新建一个Maven项目
二、修改pom文件,添加对Spring的依赖
<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>com.example</groupId> <artifactId>demo2</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <java.version>1.8</java.version> <spring.version>4.1.6.RELEASE</spring.version> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>${java.version}</source> <target>${java.version}</target> </configuration> </plugin> </plugins> </build></project>
三、编写Service类
package demo2.service;import org.springframework.stereotype.Service;@Servicepublic class DemoService { public void say(String word) { System.out.println("Hello " + word); } }
四、编写配置类
package demo2.config;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;@Configuration@ComponentScan("demo2")public class DIConfig {}
五、创建Spring容器并运行
package demo2;import org.springframework.context.annotation.AnnotationConfigApplicationContext;import demo2.config.DIConfig;import demo2.service.DemoService;public class Main { public static void main(String[] args) { AnnotationConfigApplicationContext cxt = new AnnotationConfigApplicationContext(DIConfig.class); DemoService ds = cxt.getBean(DemoService.class); ds.say("Spring"); cxt.close(); }}
六、查看运行结果
Hello Spring
0 0
- 第三章 快速搭建Spring4.x项目
- springMvc+spring4+hibernate4快速搭建项目
- 使用Spring4.x 搭建注解式 Spring MVC + JPA 项目
- 如何搭建Spring4项目
- Spring4快速入门第三章配置bean(下)
- 搭建Spring4.x.x开发环境
- 2.5 maven+spring4.X基础上搭建mybatis
- 基于Spring4.x 搭建 Spring MVC + MyBatis
- 搭建SpringMVC4.x+Spring4.x+Hibernate4.x(非Maven版)
- SpringMvc4.x基础(一):项目快速搭建
- Spring4+SpringMVC+Mybatis3+MySQL5项目搭建
- Spring4+SpringMVC+Hibernate4+MySQL5项目搭建
- 搭建Spring4+Spring4MVC+SpringData+JPA+Hibernate4项目
- 最简单SpringMvc项目搭建——Java1.7+Eclipse luna4.4.2+Maven3.2.x+spring4.1.1
- MyBatis学习总结(八)——Mybatis3.x与Spring4.x整合 一、搭建开发环境 1.1、使用Maven创建Web项目 执行如下命令: mvn archetype:create
- spring4.x 零配置搭建重要的配置类
- vue2.x 项目搭建
- 第十五章 SpringMVC项目快速搭建
- struct usb_interface
- 理解js的事件冒泡
- Android singleTask调用顺序u
- 最大连续子序列和:动态规划经典题目
- 2016年暑假于北京某家创业公司的python实习面经
- 第三章 快速搭建Spring4.x项目
- SQL datediff (时间差)
- Eclipse 安装最新SVN插件
- 梳理
- JAVA设计模式之享元模式Flyweight
- 设计模式之工厂模式
- c++ 之类的前置声明
- Struts2系列笔记(7)---Struts2类型转换
- 【Android应用开发】Instrumentation简单的模拟按键发送消息