环信SpringBoot自动装配及使用
1. 注册环信即时通讯云账号
2. 添加应用或者直接使用试用应用点击查看
3. 将开发者id写入yaml文件
4.导入pom文件
<dependency>
<groupId>com.easemob.im</groupId>
<artifactId>im-sdk-core</artifactId>
<version>0.2.5</version>
</dependency>
5. SpringBoot自动装配的原理:(自定义Springboot启动器)
SpringBoot启动的时候通过**@EnableAutoConfiguration**注解找到META-INF/spring.factories配置文件中所有的自动配置类,并对其进行加载,
TanhuaAutoConfiguration它实际上就是一个javaConfig形式的Spring容器配置类,它们都有一个**@EnableConfigurationPerperties**的注解,通过这个注解启动XXXProperties命名的类去加载全局配置中的属性,如server.port,而XXXProperties通过@ConfigurationProperties注解将全局配置文件中的配置与当前类的属性进行绑定,从而实现自动装配 1 在app-server中调用自动配置类,这个类的配置文件就需要 配置 系统需要的属性
2 在auto-config项目中,抽离公共的环境配置 3 通过@ConfigurationProperties读取公共的环境配置 4 编写xxTemplate,实现短信发送业务 5 在spring.factories中通过@EnableAutoConfiguration自动加载模板类
6. 书写HuanXinProperties
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix="tanhua.huanxin")
@Data
public class HuanXinProperties {
private String appKey;
private String clientId;
private String ClientSecret;
}
7. 书写HuanXinTemplate
package com.tanhua.autoconfig.template;
import cn.hutool.core.collection.CollUtil;
import com.easemob.im.server.EMProperties;
import com.easemob.im.server.EMService;
import com.easemob.im.server.model.EMTextMessage;
import com.tanhua.autoconfig.properties.HuanXinProperties;
import java.util.Set;
public class HuanXinTemplate {
private EMService emService;
public HuanXinTemplate(HuanXinProperties huanXinProperties){
EMProperties emProperties=EMProperties.builder()
.setAppkey(huanXinProperties.getAppKey())
.setClientId(huanXinProperties.getClientId())
.setClientSecret(huanXinProperties.getClientSecret())
.build();
emService = new EMService(emProperties);
}
public Boolean createUser(String hx_username,String hx_pwd){
System.out.println("username="+hx_username+" hx_pwd="+hx_pwd);
try{
emService.user().create(hx_username,hx_pwd).block();
return true;
}catch (Exception e){
System.out.println("创建用户失败!!");
e.printStackTrace();
}
return false;
}
public Boolean insertUser(String hx_user,String hx_user2){
try{
emService.contact().add(hx_user,hx_user2).block();
return true;
}catch (Exception e){
System.out.println("添加好友失败!!");
e.printStackTrace();
}
return false;
}
public Boolean sendMessage(String toUsername,String messgae){
try{
Set<String> set= CollUtil.newHashSet(toUsername);
emService.message().send("admin","users",set,new EMTextMessage().text(messgae),null).block();
return true;
}catch (Exception e){
e.printStackTrace();
System.out.println("环信发送信息失败!!");
}
return false;
}
}
8. 书写 HuanXinAutoConfiguration
import com.tanhua.autoconfig.properties.HuanXinProperties;
import com.tanhua.autoconfig.template.HuanXinTemplate;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
@EnableConfigurationProperties({
HuanXinProperties.class
})
public class HuanXinAutoConfiguration {
@Bean
public HuanXinTemplate huanXinTemplate(HuanXinProperties huanXinProperties){return new HuanXinTemplate(huanXinProperties);}
}
9. 测试应用
import com.easemob.im.server.EMProperties;
import com.easemob.im.server.EMService;
import com.tanhua.autoconfig.properties.HuanXinProperties;
public class HuanXinTemplate {
private EMService service;
public HuanXinTemplate(HuanXinProperties huanXinProperties){
EMProperties emProperties = new EMProperties.Builder()
.setAppkey(huanXinProperties.getAppKey())
.setClientId(huanXinProperties.getClientId())
.setClientSecret(huanXinProperties.getClientSecret())
.build();
service = new EMService(emProperties);
}
public boolean createUser(String hx_user,String hx_pwd){
try {
service.user().create(hx_user,hx_pwd).block();
return true;
} catch (Exception e) {
System.out.println("环信创建用户失败");
e.printStackTrace();
}
return false;
}
public boolean addContcat(String hx_username1,String hx_username2){
try {
service.contact().add(hx_username1,hx_username2).block();
return true;
} catch (Exception e) {
System.out.println("环信添加好友失败");
e.printStackTrace();
}
return false;
}
}
|