您当前的位置: 首页 >  spring

55分布式电商项目 - SpringBoot例子

杨林伟 发布时间:2019-07-15 15:07:48 ,浏览量:3

代码已上传至Github 地址:https://github.com/ylw-github/Spring-Boot-Demo.git

下面讲解Springboot的入门例子

SpringBoot HelloWorld 步骤1. 创建工程Spring-Boot-Demo,添加依赖,内容如下:

	4.0.0
	com.pinyougou
	pyg-springboot-demo
	0.0.1-SNAPSHOT
	
		org.springframework.boot
		spring-boot-starter-parent
		1.4.0.RELEASE
	
	
		
			org.springframework.boot
			spring-boot-starter-web
		
		
		
			org.springframework.boot
			spring-boot-devtools
		
	

可以发现发现,我们的工程自动添加了好多好多 jar 包 在这里插入图片描述 而这些 jar 包正式我们做开发时需要导入的 jar 包。因为这些 jar 包被我们刚才引入的spring-boot-starter-web 所引用了,所以我们引用 spring-boot-starter-web 后会自动把依赖传递过来。

步骤二: 创建引导类

只需要创建一个引导类 .

package com.pyg.manager;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MyApplication {
	
	//main
	// java -jar pyg-springboot-demo.jar
	public static void main(String[] args) {
		//启动入口
		//1,自动加载内置tomcat服务器环境
		//2,开启项目
		SpringApplication.run(MyApplication.class, args);
	}

}

简单解释一下,@SpringBootApplication 其实就是以下三个注解的总和

  • @Configuration: 用于定义一个配置类
  • @EnableAutoConfiguration : Spring Boot 会自动根据你 jar 包的依赖来自动配置项目。
  • @ComponentScan: 告诉 Spring 哪个 packages 的用注解标识的类 会被 spring 自动扫描并且装入 bean 容器。

我们直接执行这个引导类,会发现控制台出现的这个标识 在这里插入图片描述

步骤三: 实现 Hello World 输出

我们现在开始使用 spring MVC 框架,实现 json 数据的输出。如果按照我们原来的做法,需要在 web.xml 中添加一个 DispatcherServlet 的配置,再添加一个 spring 的配置文件。

但是我们用 SpringBoot,这一切都省了。我们直接写 Controller 类。

@RestController
public class PygController {

	@RequestMapping("/hello")
	public String showHello() {
		return "hello ,springboot";
	}
}

我们运行启动类来运行程序 在浏览器地址栏输入 http://localhost:8080/hello 即可看到运行结果 在这里插入图片描述

步骤四: 修改 tomcat 启动端口

src/main/resources 下创建application.properties:

server.port=8088

重新运行引导类。地址栏输入http://localhost:8088/hello 即可看到运行结果

步骤五: 读取配置文件信息

在 src/main/resources 下的 application.properties 增加配置

url=http://www.itcast.cn

我要在类中读取这个配置信息,修改 Controller:

@Autowired
private Environment env;

@RequestMapping("/info")
public String info(){
	return "HelloWorld~~"+env.getProperty("url");
}
步骤六: 热部署

我们在开发中反复修改类、页面等资源,每次修改后都是需要重新启动才生效,这样每次启动都很麻烦,浪费了大量的时间,能不能在我修改代码后不重启就能生效呢?可以,在pom.xml 中添加如下配置就可以实现这样的功能,我们称之为热部署。

 
	org.springframework.boot 
	spring-boot-devtools

SpringBoot 整合ActiveMQ 1.使用内嵌服务器

1)在 pom.xml 中引入 ActiveMQ 起步依赖


 	org.springframework.boot 
 	spring-boot-starter-activemq

2)创建消息生产者

@RestController
public class QueueController {
	@Autowired
	private JmsMessagingTemplate jmsMessagingTemplate;
	@RequestMapping("/send")
	public void send(String text){
		jmsMessagingTemplate.convertAndSend("itcast", text);
	}
 }

3)创建消息消费者

@Component
public class Consumer {
@JmsListener(destination="itcast")
public void readMessage(String text){
	System.out.println("接收到消息:"+text);
}

测试:启动服务后,在浏览器执行 http://localhost:8088/send.do?text=aaaaa即可看到控制台输出消息提示。Spring Boot 内置了 ActiveMQ 的服务,所以我们不用单独启动也可以执行应用程序。

2.使用外部服务

在 src/main/resources 下的 application.properties 增加配置, 指定 ActiveMQ 的地址

spring.activemq.broker-url=tcp://192.168.25.135:61616

运行后,会在 activeMQ 中看到发送的 queue 在这里插入图片描述

3.发送Map信息

1)修改 QueueController.java

@RequestMapping("/sendmap")
public void sendMap(){
	Map map=new HashMap();
	map.put("mobile", "13900001111");
	map.put("content", "恭喜获得 10 元代金券");
	jmsMessagingTemplate.convertAndSend("itcast_map",map);
}

2)修改 Consumer.java

@JmsListener(destination="itcast_map")
public void readMap(Map map){
	System.out.println(map);
}
关注
打赏
1688896170
查看更多评论

杨林伟

暂无认证

  • 3浏览

    0关注

    3183博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文
立即登录/注册

微信扫码登录

0.1030s