继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

java项目之路 一、Spring boot项目搭建

慕容8515858
关注TA
已关注
手记 1
粉丝 0
获赞 0

1:可能发生的erorr --pom.xml文件中,引入springboot父依赖时,文件报错:
–解决方案:清空.m2/repository下的所有依赖文件,重新下载即可
2:springboot集成mybatis,建议使用xml文件管理sql语句。
dependencies如下:
mysql:

<dependency>
	<groupId>mysql</groupId>
	<artifactId>mysql-connector-java</artifactId>
</dependency>
mybatis:
<dependency>
	<groupId>org.mybatis.spring.boot</groupId>
	<artifactId>mybatis-spring-boot-starter</artifactId>
	<version>1.2.0</version>
</dependency>

编写applicastion.yml文件:

	spring:
		datasource:
			type: com.alibaba.druid.pool.DruidDataSource
			url: jdbc:mysql://127.0.0.1:3306/person?useUnicode=true&characterEncoding=utf-8
			username: root
			password:
			driver-class-name: com.mysql.jdbc.Driver
	mybatis:
		mapper-locations: classpath:mapper/*.xml
		type-aliases-package: com.example.demo.model
		configuration:
			cache-enabled: true
			lazy-loading-enabled: false
			aggressive-lazy-loading: true
			multiple-result-sets-enabled: true
			map-underscore-to-camel-case: true
			auto-mapping-behavior: full
			use-column-label: true
			use-generated-keys: false
			default-executor-type: simple
			default-statement-timeout: 25000
	server:
		port: 8080

3:mapper接口可使用注解@Mapper,也可以采用自动扫描机制在入口类使用@MapperScan(“com.example.demo.dao”)来注解(推荐使用)
自动扫描mapper接口可以不添加注解(idea中,mapper接口不使用注解的话,使用@Autowired注解是飘红,并不影响使用,
有强迫症的童鞋可以在mapper接口使用@Repository或者@Resouse注解)

4:springboot热部署:两种方式(可以避免如论代码改动多少都需要频繁重启服务)
1):添加dependency:

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-devtools</artifactId>
	<optional>true</optional>
</dependency>
2):使用视图渲染(例如:freemarker,Thymeleaf)
	freemarker 设置方式
spring:
  freemarker:
	cache: false
	Thymeleaf 设置方式:
spring: 
  thymeleaf:
	cache: false 

5:springboot整合freemarker:
添加dependency:

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-devtools</artifactId>
	<optional>true</optional>
</dependency>

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

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
编写application.yml文件:
spring:
  freemarker:
	cache: false  

6:springboot整合Thymeleaf:
添加dependency:

<dependency>
	<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-thymeleaf</artifactId>
	</dependency>
编写application.yml配置文件
spring:
  thymeleaf:
	cache: false
	mode: HTML5

7:springboot整合redis:
添加dependency:

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
	<groupId>org.springframework.session</groupId>
	<artifactId>spring-session</artifactId>
</dependency>
配置文件application.yml编写
spring:
 redis:
	host: 192.168.44.129
	password: admin
  session:
	store-type: none
可能报错:JedisDataException
原因:未设置redis密码
相关命令:
	config get requirepass: 这是查询redis是否配置密码,如果返回为空,则表明未配置密码。 
	config set requirepass “admin”这是将redis的密码设置为“admin”

8:SpringBoot集成Redis消息订阅发布

1)创建一个Redis消息接收器
package cn.tyrone.springboot.redis.message;

import java.util.concurrent.CountDownLatch;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

public class Receiver {
	private static final Logger LOGGER = LoggerFactory.getLogger(Receiver.class);

	private CountDownLatch latch;

	@Autowired
	public Receiver(CountDownLatch latch) {
		this.latch = latch;
	}

	public void receiveMessage(String message) {
		LOGGER.info("Received <" + message + ">");
		latch.countDown();
	}
}
2)注册一个监听器并发送消息
package cn.tyrone.springboot.redis.message;

import java.util.concurrent.CountDownLatch;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;

//https://spring.io/guides/gs/messaging-redis/

@SpringBootApplication
public class Application {
	public static final Logger LOGGER = LoggerFactory.getLogger(Application.class);

	/*
	 * Redis消息监听器容器
	 * 这个容器加载了RedisConnectionFactory和消息监听器
	 */
	@Bean
	RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory, 
			MessageListenerAdapter listenerAdapter){
		RedisMessageListenerContainer container = new RedisMessageListenerContainer();
		container.setConnectionFactory(connectionFactory);
		container.addMessageListener(listenerAdapter, new PatternTopic("sprinboot-redis-messaage"));
		return container;
	}

	/*
	 * 将Receiver注册为一个消息监听器,并指定消息接收的方法(receiveMessage)
	 * 如果不指定消息接收的方法,消息监听器会默认的寻找Receiver中的handleMessage这个方法作为消息接收的方法
	 */
	@Bean
	MessageListenerAdapter listenerAdapter(Receiver receiver){
		return new MessageListenerAdapter(receiver, "receiveMessage");
	}

	/*
	 * Receiver实例
	 */
	@Bean
	Receiver receiver(CountDownLatch latch){
		return new Receiver(latch);
	}

	@Bean
	CountDownLatch latch(){
		return new CountDownLatch(1);
	}

	/*
	 * Redis Template 用来发送消息
	 */
	@Bean
	StringRedisTemplate template(RedisConnectionFactory connectionFactory){
		return new StringRedisTemplate(connectionFactory);
	}

	/*
	 * 测试用例
	 */
	public static void main(String[] args) throws Exception {
		ApplicationContext ctx = SpringApplication.run(Application.class, args);

		StringRedisTemplate template = ctx.getBean(StringRedisTemplate.class);
//      CountDownLatch latch = ctx.getBean(CountDownLatch.class);

		LOGGER.info("Sending message......");
		template.convertAndSend("sprinboot-redis-messaage", "Hello, SpringBoot redis message!!!!");
//      latch.wait();

		System.exit(0);

	}

}
	对于本例并不十分清楚CountDownLatch latch这个的目的,在测试的过程中,加上这句代码,会抛一个异常,但是发送和接收消息都是成功的。
	如果将此代码注释掉,该异常也将消失。同时,也并不影响消息的发布与接收。
	CountDownLatch只是一个同步的辅助类,测试过程中,并没有发现这个类对测试结果的有什么帮助。

9:SpringBoot 整合 Apache ActiveMQ
安装ActiveMQ:
直接去官网下载最新版本即可,由于这是免安装的,只需要解压就行了。
安装完之后进入bin目录,双击 activemq.bat文件(linux下在bin目录下执行 activemq start)
在浏览器输入:http://ip:8161/admin/,出现如下界面表示启动成功
61616为对外服务端口号
8161为控制器端口号
当端口号冲突时,可以修改这两个端口号。cd conf ,修改activemq.xml 修改里面的61616端口。修改jetty.xml,修改里面的8161端口。

1)添加dependency:
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
2)编写application:
spring:
  activemq:
	broker-url: tcp://localhost:61616
	user: admin
	password: admin
删除不活动队列:
一般情况下,ActiveMQ的queue或者topic在不使用之后,可以通过web控制台来删除掉。当然,也可以通过配置,
使得broker可以自动探测到无用的队列(一定时间内为空的队列)并删除掉,回收响应资源。 
activemq.xml
<broker xmlns="http://activemq.apache.org/schema/core" brokerName="localhost" dataDirectory="${activemq.base}/data" destroyApplicationContextOnStop="true" schedulePeriodForDestinationPurge="10000">
	<destinationPolicy>
		<policyMap>
		  <policyEntries>
			<policyEntry topic=">" gcInactiveDestinations="true" inactiveTimoutBeforeGC="100000" memoryLimit="1mb">
			  <pendingSubscriberPolicy>
				<vmCursor />
			  </pendingSubscriberPolicy>
			</policyEntry>
			<policyEntry queue=">" gcInactiveDestinations="true" inactiveTimoutBeforeGC="100000" memoryLimit="1mb">
			</policyEntry>
		  </policyEntries>
		</policyMap>
	</destinationPolicy>
</broker>
打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP