首先在项目运行时就初始化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的实时更新的更多相关文章

  1. 事务处理操作(COMMIT,ROLLBACK)。复制表。更新操作UPDATE实际工作中一般都会有WHERE子句,否则更新全表会影响系统性能引发死机。

    更新操作时两个会话(session)同时操作同一条记录时如果没有事务处理操作(COMMIT,ROLLBACK)则会导致死锁. 复制表:此方法Oracle特有

  2. (办公)工作中的编码不良习惯Java(不定时更新)

    1.别瞎写,方法里能用封装好的类,就别自己写HashMap. 2.方法名,整的方法名都是啥?退出close,用out. 3.git提交版本,自己写的代码,注释,提交版本的时候,一定要清理掉.每个判断能 ...

  3. 随机记录工作中常见的sql用法错误(一)

    没事开始写博客,留下以前工作中常用的笔记,内容不全或者需要补充的可以留言,我只写我常用的. 网上很多类似动软生成器的小工具,这类工具虽然在表关系复杂的时候没什么软用,但是在一些简单的表结构关系还是很方 ...

  4. 工作中常用的js、jquery自定义扩展函数代码片段

    仅记录一些我工作中常用的自定义js函数. 1.获取URL请求参数 //根据URL获取Id function GetQueryString(name) { var reg = new RegExp(&q ...

  5. 工作中那些提高你效率的神器(第二篇)_Listary

    引言 无论是工作还是科研,我们都希望工作既快又好,然而大多数时候却迷失在繁杂的重复劳动中,久久无法摆脱繁杂的事情. 你是不是曾有这样一种想法:如果我有哆啦A梦的口袋,只要拿出神奇道具就可解当下棘手的问 ...

  6. 工作中那些提高你效率的神器(第一篇)_Everything

    引言 无论是工作还是科研,我们都希望工作既快又好,然而大多数时候却迷失在繁杂的重复劳动中,久久无法摆脱繁杂的事情. 你是不是曾有这样一种想法:如果我有哆啦A梦的口袋,只要拿出神奇道具就可解当下棘手的问 ...

  7. Atitit 软件开发中 瓦哈比派的核心含义以及修行方法以及对我们生活与工作中的指导意义

    Atitit 软件开发中 瓦哈比派的核心含义以及修行方法以及对我们生活与工作中的指导意义 首先我们指明,任何一种行动以及教派修行方法都有他的多元化,只看到某一方面,就不能很好的评估利弊,适不适合自己使 ...

  8. C# 工作中遇到的几个问题

    C#  工作中遇到的几个问题 1.将VS2010中的代码编辑器的默认字体“新宋体”改为“微软雅黑”后,代码的注释,很难对齐,特别是用SandCastle Help File Builder生成帮助文档 ...

  9. [工作中的设计模式]解释器模式模式Interpreter

    一.模式解析 解释器模式是类的行为模式.给定一个语言之后,解释器模式可以定义出其文法的一种表示,并同时提供一个解释器.客户端可以使用这个解释器来解释这个语言中的句子. 以上是解释器模式的类图,事实上我 ...

随机推荐

  1. QuartZ的线程锁

                                                                                                ;        ...

  2. C语言基础--while循环

    while循环格式: while (条件表达式) { 语句; ... }   执行说明: while是对给定的条件进行判断, 如果条件满足, 就执行while后面大括号中的内容, 执行完毕之后会再次判 ...

  3. Repeater 合并单元格

    前途页面: <asp:Repeater ID="rptList" runat="server" OnPreRender="rptList_Pre ...

  4. JVM-类文件结构

    无关性的基石 I> "平台无关性"实现在操作系统的应用层上:sun公司以及其他虚拟机提供商发布了许多可以运行在各种不同平台上的虚拟机,这些虚拟机都可以载入和执行同一种平台无关 ...

  5. Tab的键的妙用

    vs2013输入“(”的时候自动加入了“)”,开始的时候感觉相当不方便,要按“End”才能继续输入“:”,后来发现按“Tab"也会自动跳出括号,于是满心喜欢.

  6. poj2250 最长上升子序列 + 输出

    //Accepted 208 KB 0 ms //最长公共上升子序列+输出 //dp //输出时用的递归输出,注意条件判断 #include <cstdio> #include <c ...

  7. EF 学习笔记

    1.EFcodeFirst如何使用存储过程!public string GetCoupon(int type) { using (var db=new ProbabilityContext()) { ...

  8. Android 对 properties文件的读写操作

    -. 放在res中的properties文件的读取,例如对放在assets目录中的setting.properties的读取:PS:之所以这里只是有读取操作,而没有写的操作,是因为我发现不能对res下 ...

  9. tip use view.isineditmode() in your custom views to skip code when shown in eclipse

    tip use view.isineditmode() in your custom views to skip code when shown in eclipse

  10. Hadoop MRUnit使用(一)

    之前在写MR job的时候,由于要在云梯,或者一淘的开发集群上运行:所以处理方法是,在本地打成jar包,然后scp到客户端网关机上,然后在提交job运行.这样的问题时,有时候如果遇到一些逻辑上的问题, ...