牛客网项目-第一章-笔记

牛客网项目-第一章

环境配置

java
maven
idea

Spring Intializr

搜索jar包的网站:https://mvnrepository.com/
https://start.spring.io/
在这里插入图片描述
缺少的aop包,手动在pom.xml中加入依赖

    	<dependency>
      		<groupId>org.springframework.boot</groupId>
      		<artifactId>spring-boot-starter-aop</artifactId>
    	</dependency>

另外评论区中有人提到JPA底层也是aop实现的,可以导入这个用来代替aop。

下载解压后用idea打开

Spring boot 入门示例

在这里插入图片描述
在这里插入图片描述
一开始运行时有报错:

Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled.
2023-09-09T15:40:08.281+08:00 ERROR 34354 --- [  restartedMain] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

Reason: Failed to determine a suitable driver class


Action:

Consider the following:
	If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
	If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).


Process finished with exit code 0

原因

这是因为spring boot 会默认加载org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration 这个类DataSourceAutoConfiguration类使用了@Configuration注解向spring注入了dataSource bean。因为工程中没有关于dataSource相关的配置信息,当spring创建dataSource bean因缺少相关的信息就会报错。

解决方法

@SpringBootApplication注解后面加上(exclude = {DataSourceAutoConfiguration.class})

在这里插入图片描述

增加功能

现在要在服务端提供一个功能,使其可以被浏览器访问到,能够给浏览器返回一个简短的问候。
新建一个包controller
controller下新建一个类
需要加两个springMVC的注解

@Controller
@RequestMapping("/alpha") //给这个类提供一个访问名称

还需要写一个能够给浏览器提供服务的方法。

    @RequestMapping(/hello) //给方法也取一个访问路径,才能使其被访问到
    @ResponseBody
    //因为返回的不是网页,只是返回的普通字符串,还需要加一个注解进行声明
    public String sayHello()
    {
        return "Hello String Boot.";
    }

在这里插入图片描述
加了新的代码后需要重新编译重新启动。
点击Rerun,重新启动前会重新编译;或者也可以Build Project. 编译后自动重启是devtools的作用

运行后:
在这里插入图片描述

如何改端口和项目地址

在 .properties中修改配置信息
修改访问端口为8088
项目地址/community

server.port=8088
server.servlet.context-path=/community

更改后显示如下:
在这里插入图片描述

Spring

在这里插入图片描述
Spring Cloud 微服务
Spring Cloud Data Flow数据集成

在这里插入图片描述

IOC

在这里插入图片描述
减少Bean之间的耦合度

在这里插入图片描述

SpringApplication.run(CommunityApplication.class, args);
开启TomCat;
自动创建了Spring容器,Sping容器会自动扫描某些包下某些bean,将这些Bean装到容器里。
会扫描配置类所在包和子包带有注解的内容,
CommunityApplication.class是配置文件,因为上面有注解@SpringBootApplication。这个注解是由其他注解组成的:
@SpringBootConfiguration表示这个类是配置文件
@EnableAutoConfiguration表示要启用自动配置
@ComponentScan是组件扫码,会自动扫描1.配置类所在的包以及子包的Bean;2.Bean上需要有像Controller这样的注解才能被扫描,装配到容器
在这里插入图片描述
Controller功能等价的注解(功能上一样,语义上有区别):

  • ControllerController是由Component实现的;处理请求的组件
  • ServiceService是由Component实现的;业务组件
  • RepositoryRepository也是由Component实现的;数据库访问的组件
  • Component:在任何地方都能用

在测试代码中启用正式类的配置类

在这里插入图片描述
在测试类前加上注解@ContextConfiguration(classes = CommunityApplication.class)运行的测试代码就以CommunityApplication为配置类了。
`

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值