工作中遇到的问题--实现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
一.模式解析 解释器模式是类的行为模式.给定一个语言之后,解释器模式可以定义出其文法的一种表示,并同时提供一个解释器.客户端可以使用这个解释器来解释这个语言中的句子. 以上是解释器模式的类图,事实上我 ...
随机推荐
- vim的编码设置
VIM的相关字符编码主要有三个参数 fencs: 它是一个编码格式的猜测列表.当用vim打开某个文件时,会依次取这里面的编码进行解码,如果某个编码格式从头至尾解码正确,那么就用那个编码 fenc:它是 ...
- SWPFILE实现(增加swap空间)
1.mkdir /var/swap chmod 700 /var/swap(可以不用设置) 2.dd if=/dev/zero of=/var/swap/file bs=1024 count=65 ...
- python常见错误总结
TypeError: 'module' object is not callable 模块未正确导入,层级关系没找对 缩进错误. IndentationError: unindent does not ...
- MySQL语句45道练习题及答案
一. 设有一数据库,包括四个表:学生表(Student).课程表(Course).成绩表(Score)以及教师信息表(Teacher).四个表的结构分别如表1-1的表(一)~表( ...
- JS 验证一组input框是否为空的方法
function checkInput() { var $tr = $("#tb_confirmed .scrollContent").find("tr"); ...
- SVG 2D入门4 - 笔画与填充
前面我们重点都在总结各类形状,文本和图片,接下来,我们还是和讨论canvas一样,总结一下颜色处理,也就是填充和边框效果:你会发现这里的内容与canvas基本上是一致的.这些属性既可以以属性的形式写在 ...
- mongodb 几个要注意的问题
1. moongo db 会尽量将 所有 索引和 热数据 放入内存中来进行比较,从而来获得更好的查询速度,同时,mongodb在写的时候,也是先写入内存,然后定期同步到磁盘上面去,这样可以达成顺序写的 ...
- Python mix-in 组合 ~ 将类组合起来 .
在一个池塘中有10个乌龟 1 条鱼 . class Turtle: def __init__(self,x): # 在生命对象的时候 说明对象的 数量 . (还是一个对象 . 数量只是该对象的一个属性 ...
- 用PHP语言刷OJ题
平常在学校都是用C,C++,Java来刷OJ题,把AC的题用不同的语言再AC一次,基本相当于翻译而已.看到学校的OJ支持提交PHP代码,于是尝试了一下. 首先,得会使用PHP,但是你如果在看这篇博客, ...
- HDU1166-敌兵布阵(线段树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1166 #include<cstdio> #include<string> #i ...