工作中遇到的问题--实现CustomerSetting的实时更新
首先在项目运行时就初始化CustomerSettings的值,采用@Bean,默认是singtone模式,只会加载一次。
@Configuration
@Order(3)
@EnableWebMvcSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class MFGWebSecurityConfigurerAdapter extends
AWebSecurityConfigurerAdapter {
@Autowired
private UserRepository userRepository;
@Autowired
private CustomerSettingsRepository customerSettingsRepository;
@Override
protected void configure(HttpSecurity httpSecurity) throws Exception {
httpSecurity
.formLogin()
.successHandler(
new SavedRequestAwareAuthenticationSuccessHandler())
.loginPage("/login").permitAll().failureUrl("/login-error")
.defaultSuccessUrl("/").and().logout()
.logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
.logoutSuccessUrl("/logged-out").permitAll().and().rememberMe()
.key(SECURITY_TOKEN)
.tokenRepository(persistentTokenRepository())
.tokenValiditySeconds(mfgSettings.getRememberMeTokenValidity())
.and().sessionManagement().maximumSessions(1)
.sessionRegistry(sessionRegistry).and().sessionFixation()
.migrateSession().and().authorizeRequests().anyRequest()
.authenticated();
}
@Bean
public PersistentTokenRepository persistentTokenRepository() {
JdbcTokenRepositoryImpl tokenRepository = new JdbcTokenRepositoryImpl();
tokenRepository.setDataSource(dataSource);
return tokenRepository;
}
@Bean
@LoggedInUser
@Scope(value = WebApplicationContext.SCOPE_SESSION, proxyMode = ScopedProxyMode.TARGET_CLASS)
@Transactional(readOnly = true)
public User getLoggedInUser() {
Authentication authentication = SecurityContextHolder.getContext()
.getAuthentication();
if (authentication != null
&& !(authentication instanceof AnonymousAuthenticationToken)
&& authentication.isAuthenticated())
return userRepository.findByLogin(authentication.getName());
return null;
}
@Bean
@SystemUser
@Scope(value = WebApplicationContext.SCOPE_APPLICATION, proxyMode = ScopedProxyMode.NO)
@Transactional(readOnly = true)
public User getSystemUser() {
return userRepository.findByLogin(Constants.SYSTEM_USER);
}
@Bean
public CustomerSettings customerSettings() {
return customerSettingsRepository.findAll().get(0);
}
}
为了能够在CustomerSettings发生改变时,对相关的值进行实时更新,在CustomerService中添加更新后的操作方法:
@Override
protected void afterUpdate(CustomerSettings t) {
customerSettings.editFrom(t);
}
@Override
protected void afterEdit(CustomerSettings t) {
customerSettings.editFrom(t);
}
另外:enitForm方法是写在CustomerSettings.java这个实体类中的,这样在更新时就能实现对其中元素的部分更新,避免以前实训中有些元素没更新就会被置空,需要每次都先查询再手动部分更新的情况。
以下是这个实体中使用enitForm的案例:
package com.sim.mfg.data.domain;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.EntityListeners;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.MappedSuperclass;
import javax.persistence.Transient;
import javax.persistence.Version;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.sim.mfg.data.dto.Action;
/**
* AMfgObject, based of all MFG data classes
* @author system-in-motion
*
*/
@MappedSuperclass
@EntityListeners({AuditingEntityListener.class})
public abstract class AMfgObject {
@Version
@Column(name = "VERSION")
private int version;
@CreatedDate
@Column(name = "CREATION_DATE", nullable = false)
private Date creationDate;
@LastModifiedDate
@Column(name = "UPDATE_DATE")
private Date updateDate;
@JsonIgnore
@CreatedBy
@JoinColumn(name = "CREATED_BY", nullable = false)
@ManyToOne(fetch = FetchType.LAZY)
private User createdBy;
@JsonIgnore
@LastModifiedBy
@JoinColumn(name = "UPDATED_BY")
@ManyToOne(optional = true, fetch = FetchType.LAZY)
private User updatedBy;
@Column(name = "ENABLED", columnDefinition = "INT", nullable = false)
private boolean enabled = true;
@Transient
private Set<Action> actions;
/**
* Empty constructor
*/
public AMfgObject() {}
/**
* Override this method to provide object edition from another object
* @param <T>
* @param dto
* @return
*/
public abstract void editFrom(AMfgObject object);
public int getVersion() {
return version;
}
public void setVersion(int version) {
this.version = version;
}
public Date getCreationDate() {
return creationDate;
}
public void setCreationDate(Date creationDate) {
this.creationDate = creationDate;
}
public Date getUpdateDate() {
return updateDate;
}
public void setUpdateDate(Date updateDate) {
this.updateDate = updateDate;
}
public User getCreatedBy() {
return createdBy;
}
public void setCreatedBy(User createdBy) {
this.createdBy = createdBy;
}
public User getUpdatedBy() {
return updatedBy;
}
public void setUpdatedBy(User updatedBy) {
this.updatedBy = updatedBy;
}
public boolean isEnabled() {
return enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
@JsonProperty
public Set<Action> getActions() {
return actions;
}
public void setActions(Set<Action> actions) {
this.actions = actions;
}
/**
* Return a list of arrays of action data to be displayed by DataTables
* @return
*/
public List<String[]> getActionArrays() {
List<String[]> actionArrays = new ArrayList<String[]>();
if (actions != null) {
for (Action action : actions) {
actionArrays.add(new String[] {
action.getBase(),
action.getAction(),
action.getStyle(),
action.getIcon(),
action.getAltText(),
action.getObjectId() == null ? "" : String.valueOf(action.getObjectId())});
}
}
// Sort by base + action to make sure the order is the same on each row
Collections.sort(actionArrays, new Comparator<String[]>() {
private static final int BASE = 0;
private static final int ACTION = 1;
@Override
public int compare(String[] arg0, String[] arg1) {
return (arg0[BASE]+arg0[ACTION]).compareTo(arg1[BASE]+arg1[ACTION]);
}
});
return actionArrays;
}
}
package com.sim.mfg.data.domain;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import org.hibernate.annotations.Where;
import org.hibernate.validator.constraints.NotBlank;
import com.sim.mfg.data.domain.type.DistributionOperationType;
@Entity
@Table(name = "CUSTOMERSETTINGS")
@Where(clause = "enabled=1")
public class CustomerSettings extends AMfgObject implements Serializable{
/**
*
*/
private static final long serialVersionUID = 559748589762162346L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@NotBlank
@Column(name = "NAME", nullable = false)
private String name;
@Column(name = "PREFIXLOWER", nullable = false)
private String prefixLower;
@Column(name = "PREFIXUPPER", nullable = false)
private String prefixUpper;
@Column(name = "CODEURL", nullable = false)
private String codeUrl;
@Column(name = "BATCHEXPIRYTIMEOUT", nullable = false)
private long batchExpiryTimeout;
@Column(name = "PRODUCTIONDATEENABLED", nullable = false)
private boolean productionDateEnabled = true;
@Column(name = "EXPIRYDATEENABLED", nullable = false)
private boolean expiryDateEnabled = true;
@Column(name = "FILLLOCATIONWIDGETENABLED", nullable = false)
private boolean fillLocationWidgetEnabled = true;
@Column(name = "PRODUCTSHELFLIFEENABLED", nullable = false)
private boolean productShelfLifeEnabled = true;
@Column(name = "PRODUCTBARCODEENABLED", nullable = false)
private boolean productBarcodeEnabled = true;
@Column(name = "WEIXINTOKEN", nullable = false)
private String weixinToken;
@Column(name = "WEIXINCORPID", nullable = false)
private String weixinCorpId;
@Column(name = "WEIXINENCODINGAESKEY", nullable = false)
private String weixinEncodingAesKey;
@Column(name = "WEIXINCORPSECRET", nullable = false)
private String weixinCorpSecret;
@Column(name = "WEIXINCODEAPPLICATIONAPPID", nullable = false)
private Integer weixinCodeApplicationAppId;
@Column(name = "WEIXINSHIPMENTOPERATIONAPPID", nullable = false)
private Integer weixinShipmentOperationAppId;
@Column(name = "WEIXINDELIVERYOPERATIONAPPID", nullable = false)
private Integer weixinDeliveryOperationAppId;
@Column(name = "WEIXINRECEPTIONOPERATIONAPPID", nullable = false)
private Integer weixinReceptionOperationAppId;
@Column(name = "WEIXINSIMPLERECEPTIONOPERATIONAPPID", nullable = false)
private Integer weixinSimpleReceptionOperationAppId;
@Column(name = "WEIXINRETURNOPERATIONAPPID", nullable = false)
private Integer weixinReturnOperationAppId;
@Column(name = "WEIXININVENTORYOPERATIONAPPID", nullable = false)
private Integer weixinInventoryOperationAppId;
@Column(name = "WEIXINMOVEMENTOPERATIONAPPID", nullable = false)
private Integer weixinMovementOperationAppId;
@Column(name = "WEIXINFILLINGOPERATIONAPPID", nullable = false)
private Integer weixinFillingOperationAppId;
/**
* Empty constructor
*/
public CustomerSettings() { }
/**
* Get Weixin app id for given distribution operation type
* @param operationType
* @return
*/
public Integer getWeixinDistributionOperationAppId(DistributionOperationType operationType) {
switch (operationType) {
case SHIPMENT:
return weixinShipmentOperationAppId;
case DELIVERY:
return weixinDeliveryOperationAppId;
case RECEPTION:
return weixinReceptionOperationAppId;
case RETURN:
return weixinReturnOperationAppId;
case INVENTORY:
return weixinInventoryOperationAppId;
case SIMPLERECEPTION:
return weixinSimpleReceptionOperationAppId;
default:
return null;
}
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPrefixLower() {
return prefixLower;
}
public void setPrefixLower(String prefixLower) {
this.prefixLower = prefixLower;
}
public String getPrefixUpper() {
return prefixUpper;
}
public void setPrefixUpper(String prefixUpper) {
this.prefixUpper = prefixUpper;
}
public String getCodeUrl() {
return codeUrl;
}
public void setCodeUrl(String codeUrl) {
this.codeUrl = codeUrl;
}
public long getBatchExpiryTimeout() {
return batchExpiryTimeout;
}
public void setBatchExpiryTimeout(long batchExpiryTimeout) {
this.batchExpiryTimeout = batchExpiryTimeout;
}
public boolean isProductionDateEnabled() {
return productionDateEnabled;
}
public void setProductionDateEnabled(boolean productionDateEnabled) {
this.productionDateEnabled = productionDateEnabled;
}
public boolean isExpiryDateEnabled() {
return expiryDateEnabled;
}
public void setExpiryDateEnabled(boolean expiryDateEnabled) {
this.expiryDateEnabled = expiryDateEnabled;
}
public boolean isFillLocationWidgetEnabled() {
return fillLocationWidgetEnabled;
}
public void setFillLocationWidgetEnabled(boolean fillLocationWidgetEnabled) {
this.fillLocationWidgetEnabled = fillLocationWidgetEnabled;
}
public boolean isProductShelfLifeEnabled() {
return productShelfLifeEnabled;
}
public void setProductShelfLifeEnabled(boolean productShelfLifeEnabled) {
this.productShelfLifeEnabled = productShelfLifeEnabled;
}
public boolean isProductBarcodeEnabled() {
return productBarcodeEnabled;
}
public void setProductBarcodeEnabled(boolean productBarcodeEnabled) {
this.productBarcodeEnabled = productBarcodeEnabled;
}
public String getWeixinToken() {
return weixinToken;
}
public void setWeixinToken(String weixinToken) {
this.weixinToken = weixinToken;
}
public String getWeixinCorpId() {
return weixinCorpId;
}
public void setWeixinCorpId(String weixinCorpId) {
this.weixinCorpId = weixinCorpId;
}
public String getWeixinEncodingAesKey() {
return weixinEncodingAesKey;
}
public void setWeixinEncodingAesKey(String weixinEncodingAesKey) {
this.weixinEncodingAesKey = weixinEncodingAesKey;
}
public String getWeixinCorpSecret() {
return weixinCorpSecret;
}
public void setWeixinCorpSecret(String weixinCorpSecret) {
this.weixinCorpSecret = weixinCorpSecret;
}
public Integer getWeixinCodeApplicationAppId() {
return weixinCodeApplicationAppId;
}
public void setWeixinCodeApplicationAppId(Integer weixinCodeApplicationAppId) {
this.weixinCodeApplicationAppId = weixinCodeApplicationAppId;
}
public Integer getWeixinShipmentOperationAppId() {
return weixinShipmentOperationAppId;
}
public void setWeixinShipmentOperationAppId(Integer weixinShipmentOperationAppId) {
this.weixinShipmentOperationAppId = weixinShipmentOperationAppId;
}
public Integer getWeixinDeliveryOperationAppId() {
return weixinDeliveryOperationAppId;
}
public void setWeixinDeliveryOperationAppId(Integer weixinDeliveryOperationAppId) {
this.weixinDeliveryOperationAppId = weixinDeliveryOperationAppId;
}
public Integer getWeixinReceptionOperationAppId() {
return weixinReceptionOperationAppId;
}
public void setWeixinReceptionOperationAppId(
Integer weixinReceptionOperationAppId) {
this.weixinReceptionOperationAppId = weixinReceptionOperationAppId;
}
public Integer getWeixinSimpleReceptionOperationAppId() {
return weixinSimpleReceptionOperationAppId;
}
public void setWeixinSimpleReceptionOperationAppId(
Integer weixinSimpleReceptionOperationAppId) {
this.weixinSimpleReceptionOperationAppId = weixinSimpleReceptionOperationAppId;
}
public Integer getWeixinReturnOperationAppId() {
return weixinReturnOperationAppId;
}
public void setWeixinReturnOperationAppId(Integer weixinReturnOperationAppId) {
this.weixinReturnOperationAppId = weixinReturnOperationAppId;
}
public Integer getWeixinInventoryOperationAppId() {
return weixinInventoryOperationAppId;
}
public void setWeixinInventoryOperationAppId(
Integer weixinInventoryOperationAppId) {
this.weixinInventoryOperationAppId = weixinInventoryOperationAppId;
}
public Integer getWeixinMovementOperationAppId() {
return weixinMovementOperationAppId;
}
public void setWeixinMovementOperationAppId(Integer weixinMovementOperationAppId) {
this.weixinMovementOperationAppId = weixinMovementOperationAppId;
}
public Integer getWeixinFillingOperationAppId() {
return weixinFillingOperationAppId;
}
public void setWeixinFillingOperationAppId(Integer weixinFillingOperationAppId) {
this.weixinFillingOperationAppId = weixinFillingOperationAppId;
}
@Override
public void editFrom(AMfgObject object) {
if(object==null)
return ;
CustomerSettings custSettings=(CustomerSettings)object;
this.name=custSettings.getName();
this.codeUrl=custSettings.getCodeUrl();
this.batchExpiryTimeout=custSettings.getBatchExpiryTimeout();
this.expiryDateEnabled=custSettings.isExpiryDateEnabled();
this.fillLocationWidgetEnabled=custSettings.isFillLocationWidgetEnabled();
this.prefixLower=custSettings.getPrefixLower();
this.prefixUpper=custSettings.getPrefixUpper();
this.productBarcodeEnabled=custSettings.isProductBarcodeEnabled();
this.productionDateEnabled=custSettings.isProductionDateEnabled();
this.productShelfLifeEnabled=custSettings.isProductShelfLifeEnabled();
this.weixinCodeApplicationAppId=custSettings.getWeixinCodeApplicationAppId();
this.weixinCorpId=custSettings.getWeixinCorpId();
this.weixinCorpSecret=custSettings.getWeixinCorpSecret();
this.weixinDeliveryOperationAppId=custSettings.getWeixinDeliveryOperationAppId();
this.weixinEncodingAesKey=custSettings.getWeixinEncodingAesKey();
this.weixinFillingOperationAppId=custSettings.getWeixinFillingOperationAppId();
this.weixinInventoryOperationAppId=custSettings.getWeixinInventoryOperationAppId();
this.weixinMovementOperationAppId=custSettings.getWeixinMovementOperationAppId();
this.weixinReceptionOperationAppId=custSettings.getWeixinReceptionOperationAppId();
this.weixinReturnOperationAppId=custSettings.getWeixinReturnOperationAppId();
this.weixinShipmentOperationAppId=custSettings.getWeixinShipmentOperationAppId();
this.weixinSimpleReceptionOperationAppId=custSettings.getWeixinSimpleReceptionOperationAppId();
this.weixinToken=custSettings.getWeixinToken();
}
@Override
public String toString() {
return "CustSettings [id=" + id + ", name=" + name + ", codeUrl="
+ codeUrl + "]";
}
}
工作中遇到的问题--实现CustomerSetting的实时更新的更多相关文章
- 事务处理操作(COMMIT,ROLLBACK)。复制表。更新操作UPDATE实际工作中一般都会有WHERE子句,否则更新全表会影响系统性能引发死机。
更新操作时两个会话(session)同时操作同一条记录时如果没有事务处理操作(COMMIT,ROLLBACK)则会导致死锁. 复制表:此方法Oracle特有
- (办公)工作中的编码不良习惯Java(不定时更新)
1.别瞎写,方法里能用封装好的类,就别自己写HashMap. 2.方法名,整的方法名都是啥?退出close,用out. 3.git提交版本,自己写的代码,注释,提交版本的时候,一定要清理掉.每个判断能 ...
- 随机记录工作中常见的sql用法错误(一)
没事开始写博客,留下以前工作中常用的笔记,内容不全或者需要补充的可以留言,我只写我常用的. 网上很多类似动软生成器的小工具,这类工具虽然在表关系复杂的时候没什么软用,但是在一些简单的表结构关系还是很方 ...
- 工作中常用的js、jquery自定义扩展函数代码片段
仅记录一些我工作中常用的自定义js函数. 1.获取URL请求参数 //根据URL获取Id function GetQueryString(name) { var reg = new RegExp(&q ...
- 工作中那些提高你效率的神器(第二篇)_Listary
引言 无论是工作还是科研,我们都希望工作既快又好,然而大多数时候却迷失在繁杂的重复劳动中,久久无法摆脱繁杂的事情. 你是不是曾有这样一种想法:如果我有哆啦A梦的口袋,只要拿出神奇道具就可解当下棘手的问 ...
- 工作中那些提高你效率的神器(第一篇)_Everything
引言 无论是工作还是科研,我们都希望工作既快又好,然而大多数时候却迷失在繁杂的重复劳动中,久久无法摆脱繁杂的事情. 你是不是曾有这样一种想法:如果我有哆啦A梦的口袋,只要拿出神奇道具就可解当下棘手的问 ...
- Atitit 软件开发中 瓦哈比派的核心含义以及修行方法以及对我们生活与工作中的指导意义
Atitit 软件开发中 瓦哈比派的核心含义以及修行方法以及对我们生活与工作中的指导意义 首先我们指明,任何一种行动以及教派修行方法都有他的多元化,只看到某一方面,就不能很好的评估利弊,适不适合自己使 ...
- C# 工作中遇到的几个问题
C# 工作中遇到的几个问题 1.将VS2010中的代码编辑器的默认字体“新宋体”改为“微软雅黑”后,代码的注释,很难对齐,特别是用SandCastle Help File Builder生成帮助文档 ...
- [工作中的设计模式]解释器模式模式Interpreter
一.模式解析 解释器模式是类的行为模式.给定一个语言之后,解释器模式可以定义出其文法的一种表示,并同时提供一个解释器.客户端可以使用这个解释器来解释这个语言中的句子. 以上是解释器模式的类图,事实上我 ...
随机推荐
- 【NOIP模拟_54测试】【并查集】【二进制】【搜索】【区间序列类】
第一题 Mushroom的序列 大意: 给一个序列,求一段连续最长区间满足:最多改变一个数,使得区间是严格的上升子序列. 解: 直接扫描一遍,记一个最长上升子序列编号.然后从每一个编号为1 的点来判断 ...
- java基础之 集合
一.ArrayList和Linkedlist的区别 1.ArrayList是基于数组,LinkedList基于链表实现. 对ArrayList和LinkedList而言,在列表末尾增加一个元素所花的开 ...
- 类似github的框架
github是程序员经常上的网站,但如果是在一家苦逼不能访问外网的公司,那不能把自己的代码托管在github上绝对是一件非常痛苦的事情.如果想要在公司内网也可以用github托管自己的代码,那就要自己 ...
- 速度!!!抢KIS英文版(多设备版)3年激活码
活动地址 http://promo.kaspersky.com/wvu直接打不开,需要用 US 的 在 线 代 理,暂时提供1个(204.12.228.235)可以绕过第一步邮箱验证注意:3年版多设备 ...
- HTML5实战教程———开发一个简单漂亮的登录页面
最近看过几个基于HTML5开发的移动应用,比如臭名昭著的12036移动客户端就是主要使用HTML5来实现的,虽然还是有点反应迟钝,但已经比较流畅了,相信随着智能手机的配置越来越高性能越来越好,会越来越 ...
- PHP time() 函数
定义和用法 time() 函数返回当前时间的 Unix 时间戳. 语法 time(void) 参数 描述 void 可选. 说明 返回自从 Unix 纪元(格林威治时间 1970 年 1 月 1 日 ...
- 0125 多线程 继承Thread 练习
//定义一个继承Thread类的类,并覆盖run()方法,在run()方法中每隔100毫秒打印一句话public class Csh extends Thread{ public void run() ...
- Apparmor——Linux内核中的强制访问控制系统
AppArmor 因为最近在研究OJ(oline judge)后台的安全模块的实现,所以一直在研究Linux下沙箱的东西,同时发现了Apparmor可以提供访问控制. AppArmor(Appli ...
- Bochs使用说明
简介 关于Bochs的介绍:http://en.wikipedia.org/wiki/Bochs Bochs的官网:http://bochs.sourceforge.net/ 这里记录如何在Windo ...
- iOS APP上线流程
前言:作为一名IOS开发者,把开发出来的App上传到App Store是必须的.下面就来详细介绍下具体流程. 1.打开苹果开发者中心:https://developer.apple.com 打开后点击 ...