springcloud入门(1)

本文介绍如何使用SpringBoot创建一个简单的应用。从搭建Maven项目开始,配置必要的依赖,并编写启动类,最终实现了一个能返回“Hello World!”消息的小应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天用springboot创建一个小应用。

首先,创建一个简单的maven应用,如下图:


然后,配置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.test</groupId>
  <artifactId>springcloud_helloworld</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>springcloud_helloworld Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.1.RELEASE</version>
  </parent>
  <dependencies>
    <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
  </dependencies>
  <repositories>         
   <repository>           
    <snapshots>             
     <enabled>false</enabled>            
     </snapshots>            
     <id>central</id>            
     <name>Maven Repository Switchboard</name>            
     <url>http://repo2.maven.org/maven2</url>          
     </repository>        
     </repositories>
  <build>
    <finalName>springcloud_helloworld</finalName>
    
  </build>
  
</project>

接下来编写相应的启动类,代码如下:

package com.springcloud.hello;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@SpringBootApplication
public class Example {

	@RequestMapping("/")
	String home(){
		return "Hello World!";
		
	}
	 public static void main(String[] args) throws Exception {
		 SpringApplication.run(Example.class, args);
	 }
}
启动这个类,成功运行后,在浏览器输入http://localhost:8080/便可看到如下字样:

注意,启动类一定要在一个自己定义的包下,比如com.springcloud.hello,否则会出现如下异常:

org.springframework.beans.factory.BeanDefinitionStoreException: Failed to read candidate component class: URL [jar:file:/E:/repository/org/springframework/boot/spring-boot-autoconfigure/1.3.1.RELEASE/spring-boot-autoconfigure-1.3.1.RELEASE.jar!/org/springframework/boot/autoconfigure/jdbc/DataSourceAutoConfiguration$JdbcTemplateConfiguration.class]; nested exception is java.lang.IllegalStateException: Could not evaluate condition on org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration$JdbcTemplateConfiguration due to org/springframework/dao/DataAccessException not found. Make sure your own configuration does not rely on that class. This can also happen if you are @ComponentScanning a springframework package (e.g. if you put a @ComponentScan in the default package by mistake)


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值