工作中遇到的问题--实现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
一.模式解析 解释器模式是类的行为模式.给定一个语言之后,解释器模式可以定义出其文法的一种表示,并同时提供一个解释器.客户端可以使用这个解释器来解释这个语言中的句子. 以上是解释器模式的类图,事实上我 ...
随机推荐
- php变量的判空和类型判断
(1)var_dump(); 判断一个变量是否已经声明并且赋值,并且打印类型和值 <?php $a; var_dump($a);//输出null <?php var_dump($a);// ...
- C# SVN检出的代码,F12显示从元数据
解决办法: 删除项目中的引用(同时也要删除bin文件夹中的dll文件,否则不能重新添加),并重新添加本地引用即可. 原因: 项目中的dll文件不是本机编译出来的,所以找不到元数据.如果当前关联的项目里 ...
- SDK(SoftWare Development Kit)介绍
ctrl+alt+shift+s进入项目设置页面: SKDs的界面可以设置SDK. 点击到project 可以为project选择sdk 如上图标注 1 所示,IntelliJ IDEA 支持 6 种 ...
- Vijos 1243 生产产品 (单调队列优化的动态规划)
题意:中文题.不说了. 注意一些地方,机器的执行过程是没有顺序的,而且每个机器可以用多次.第一次执行的机器不消耗转移时间K. 用dp[i][j]表示第i个机器完成第j个步骤的最短时间,sum[j][i ...
- 算法导论 第六章 思考题6-3 Young氏矩阵
这题利用二叉堆维持堆性质的办法来维持Young氏矩阵的性质,题目提示中写得很清楚,不过确实容易转不过弯来. a,b两问很简单.直接看c小问: 按照Young氏矩阵的性质,最小值肯定在左上角取得,问题在 ...
- 国产单机RPG游戏的情怀
最近在玩儿仙剑奇侠传5,这个游戏从小时候玩儿到现在,也算是见证了一代人的成长,小时候没少玩盗版,现在自己工作了,有了固定的收入,也能体会到游戏开发者的不容易,尤其是单机游戏这个圈子,现在国内几乎没有人 ...
- Android.mk中添加宏定义
在Boardconfig.mk 中添加一个 IS_FLAG := true 由于Boardconfig.mk和各目录的Android.mk是相互关联的 所以我们可以在Android.mk 中添加 一个 ...
- STL源码分析《4》----Traits技术
在 STL 源码中,到处可见 Traits 的身影,其实 Traits 不是一种语法,更确切地说是一种技术. STL库中,有一个函数叫做 advance, 用来将某个迭代器(具有指针行为的一种 cla ...
- 开源软件项目管理系统招设计/开发。。。。。Zend Framework2架构 svn://735.ikwb.com/pms
开源软件项目管理系统招设计/开发.....Zend Framework2架构svn://735.ikwb.com/pms
- Magento后台Grid删除Add New按钮
开发过包含后台Grid及表等Magento完整模块的朋友应该知道,默认的,在Magento后台Grid右上方都会包含一个Add New按钮,用来添加新的item.但有些情况我们也可能不需要这个Add ...