Spring Boot从原理到实战

    “回眸一笑百媚生”,说的就是Spring Boot。开始只是听说了这个名词,那它和spring有啥区别啊,名字有点类似哦。所以说:Spring Boot是spring的升级版,但并不是对spring功能上的增强,而是提供了一种快速使用spring的方式:开箱即用,没有代码生成,也无需XML配置。

    如何使用SpringBoot呢?首先创建一个一般的Maven项目,有一个pom.xml和基本的src/main/java结构。

1、pom文件配置

<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.nes.spring.boot</groupId>
    <artifactId>SpringBootDemo1</artifactId>
    <version>0.0.1-SNAPSHOT</version>  
    <!--spring-boot-starter-parent包含了大量配置好的依赖管理,在自己项目添加这些依赖的时候不需要写<version>版本号-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.1.RELEASE</version>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>
    
    <dependencies>
        <!--spring-boot-starter-web 提供了mvc、aop的依赖包-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <build>
    	<plugins>
    		<!-- Compile -->
    		<plugin>
    			<groupId>org.apache.maven.plugins</groupId>
    			<artifactId>maven-compiler-plugin</artifactId>
    			<version>2.5.1</version>
    			<configuration>
    				<source>1.7</source>
    				<target>1.7</target>
    			</configuration>
            </plugin>
            <!-- spring boot maven plugin -->
             <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <dependencies>
                    <dependency>
                        <groupId>org.springframework</groupId>
                        <artifactId>springloaded</artifactId>
                        <version>1.2.5.RELEASE</version>
                    </dependency>
                </dependencies>
            </plugin>
    	</plugins>
    </build>
</project>

    注意:Spring默认使用jdk1.6,如果你想使用jdk1.8,你需要在pom.xml的属性里面添加java.version;


2、新建HelloController测试类,pojo对象Demo类,很平常的code。

@RestController
public class HelloController {

    @RequestMapping("/hello")
    public String hello(){
        return "hello-2017";
    }

    @RequestMapping("/getDemo")
    public Demo getDemo(){
        Demo demo = new Demo();
        demo.setId(1);
        demo.setName("星星");
        demo.setCreateTime(new Date());
        demo.setRemarks("测试你会不会出现");
        return demo;
    }
}


3、新建启动类Application.java

@SpringBootApplication
public class Application extends WebMvcConfigurerAdapter {

    //在main方法中启动程序
    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }
}

    Spring Boot是使用SpringApplication引导项目启动,SpringApplication类为我们引导项目提供了一种便利的方式——通过main()方法直接启动。大多数情况下,我们可以把项目启动这个任务直接委托给SpringApplication.run方法。 右击 run 'Application.java'就可以启动,控制台也会出现相应的提示。

4、问题

    运行的时候报错:Caused by: org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name 'errorPageFilter' for bean class [org.springframework.boot.web.support.ErrorPageFilter] conflicts with existing;

org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [Application]; nested exception is org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name 'errorPageFilter' for bean class [org.springframework.boot.web.support.ErrorPageFilter] conflicts with existing, non-compatible bean definition of same name and class [org.springframework.boot.context.web.ErrorPageFilter]
	at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:187) ~[spring-context-4.3.3.RELEASE.jar:4.3.3.RELEASE]
	at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:324) ~[spring-context-4.3.3.RELEASE.jar:4.3.3.RELEASE]
	at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:246) ~[spring-context-4.3.3.RELEASE.jar:4.3.3.RELEASE]
	at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:273) ~[spring-context-4.3.3.RELEASE.jar:4.3.3.RELEASE]
	at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:98) ~[spring-context-4.3.3.RELEASE.jar:4.3.3.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:681) ~[spring-context-4.3.3.RELEASE.jar:4.3.3.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:523) ~[spring-context-4.3.3.RELEASE.jar:4.3.3.RELEASE]
	at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.4.1.RELEASE.jar:1.4.1.RELEASE]
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:761) [spring-boot-1.4.1.RELEASE.jar:1.4.1.RELEASE]
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:371) [spring-boot-1.4.1.RELEASE.jar:1.4.1.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-1.4.1.RELEASE.jar:1.4.1.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1186) [spring-boot-1.4.1.RELEASE.jar:1.4.1.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1175) [spring-boot-1.4.1.RELEASE.jar:1.4.1.RELEASE]
	at Application.main(Application.java:12) [classes/:na]
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_05]
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_05]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_05]
	at java.lang.reflect.Method.invoke(Method.java:483) ~[na:1.8.0_05]
	at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144) [idea_rt.jar:na]
Caused by: org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name 'errorPageFilter' for bean class [org.springframework.boot.web.support.ErrorPageFilter] conflicts with existing, non-compatible bean definition of same name and class [org.springframework.boot.context.web.ErrorPageFilter]
	at org.springframework.context.annotation.ClassPathBeanDefinitionScanner.checkCandidate(ClassPathBeanDefinitionScanner.java:320) ~[spring-context-4.3.3.RELEASE.jar:4.3.3.RELEASE]
	at org.springframework.context.annotation.ClassPathBeanDefinitionScanner.doScan(ClassPathBeanDefinitionScanner.java:259) ~[spring-context-4.3.3.RELEASE.jar:4.3.3.RELEASE]
	at org.springframework.context.annotation.ComponentScanAnnotationParser.parse(ComponentScanAnnotationParser.java:137) ~[spring-context-4.3.3.RELEASE.jar:4.3.3.RELEASE]
	at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:275) ~[spring-context-4.3.3.RELEASE.jar:4.3.3.RELEASE]
	at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:237) ~[spring-context-4.3.3.RELEASE.jar:4.3.3.RELEASE]
	at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:204) ~[spring-context-4.3.3.RELEASE.jar:4.3.3.RELEASE]
	at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:173) ~[spring-context-4.3.3.RELEASE.jar:4.3.3.RELEASE]
	... 18 common frames omitted

    spring boot 第一个测试项目报错,提示是errorPageFilter bean 冲突,但其实是配置类没有放到package下。只要随意放到一个package下就不会有问题了。Spring Boot是无需XML配置的,它会自动识别特定目录下的文件,如果没有放到package下,它识别不了,应该是这样子的。


  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
Spring Boot Spring Cloud实战》是由徐雪桥编著的一本介绍如何使用Spring BootSpring Cloud进行开发的实战教程。本书以实际案例为基础,详细讲解了Spring BootSpring Cloud的各项特性与用法,旨在帮助读者快速上手并掌握这两个开发框架。 首先,书中介绍了Spring Boot的基本概念和用法,包括如何使用Spring Initializer来创建一个Spring Boot项目,如何配置和启动Spring Boot应用程序等。此外,还介绍了Spring Boot中常用的注解和组件,如控制器、服务、仓库等,并详细讲解了如何使用Spring Boot进行数据库访问、日志管理、缓存等操作。 接着,书中着重介绍了Spring Cloud的各项核心技术和组件,包括服务注册与发现、负载均衡、断路器、配置中心等。通过实际案例,读者可以了解和掌握使用Eureka和Consul等服务注册中心进行服务治理的方法,使用Ribbon和Feign进行服务间通信的方法,以及使用Hystrix进行断路器的配置和管理等。此外,书中还介绍了如何使用Spring Cloud Config进行统一配置管理,以及如何使用Spring Cloud Stream进行消息驱动的开发等。 最后,书中还介绍了如何使用Spring BootSpring Cloud进行微服务架构开发,并通过实际案例演示了如何构建和部署一个基于Spring Cloud的微服务架构。通过学习本书,读者可以了解到微服务架构的基本概念和原理,并学会如何将其应用于实际项目开发中。 总之,《Spring Boot Spring Cloud实战》是一本涵盖了Spring BootSpring Cloud开发的实践性教程。无论是初学者还是有一定经验的开发者,都可以通过阅读本书来快速入门和提升自己的开发能力。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值