Prechádzať zdrojové kódy

修复热部署转换问题

RuoYi 6 rokov pred
rodič
commit
5d0e3a7034

+ 141 - 0
src/main/java/com/ruoyi/common/utils/bean/BeanUtils.java

@@ -0,0 +1,141 @@
+package com.ruoyi.common.utils.bean;
+
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import com.ruoyi.project.system.dept.domain.Dept;
+import com.ruoyi.project.system.user.domain.User;
+
+/**
+ * Bean 工具类
+ * 
+ * @author ruoyi
+ */
+public class BeanUtils
+{
+    /** Bean方法名中属性名开始的下标 */
+    private static final int BEAN_METHOD_PROP_INDEX = 3;
+
+    /** * 匹配getter方法的正则表达式 */
+    private static final Pattern GET_PATTERN = Pattern.compile("get(\\p{javaUpperCase}\\w*)");
+
+    /** * 匹配setter方法的正则表达式 */
+    private static final Pattern SET_PATTERN = Pattern.compile("set(\\p{javaUpperCase}\\w*)");
+
+    /**
+     * Bean属性复制工具方法。
+     * 
+     * @param dest 目标对象
+     * @param src 源对象
+     */
+    public static void copyBeanProp(Object dest, Object src)
+    {
+        List<Method> destSetters = getSetterMethods(dest);
+        List<Method> srcGetters = getGetterMethods(src);
+        try
+        {
+            for (Method setter : destSetters)
+            {
+                for (Method getter : srcGetters)
+                {
+                    if (isMethodPropEquals(setter.getName(), getter.getName())
+                            && setter.getParameterTypes()[0].equals(getter.getReturnType()))
+                    {
+                        setter.invoke(dest, getter.invoke(src));
+                    }
+                }
+            }
+        }
+        catch (Exception e)
+        {
+            e.printStackTrace();
+        }
+    }
+
+    /**
+     * 获取对象的setter方法。
+     * 
+     * @param obj 对象
+     * @return 对象的setter方法列表
+     */
+    public static List<Method> getSetterMethods(Object obj)
+    {
+        // setter方法列表
+        List<Method> setterMethods = new ArrayList<Method>();
+
+        // 获取所有方法
+        Method[] methods = obj.getClass().getMethods();
+
+        // 查找setter方法
+
+        for (Method method : methods)
+        {
+            Matcher m = SET_PATTERN.matcher(method.getName());
+            if (m.matches() && (method.getParameterTypes().length == 1))
+            {
+                setterMethods.add(method);
+            }
+        }
+        // 返回setter方法列表
+        return setterMethods;
+    }
+
+    /**
+     * 获取对象的getter方法。
+     * 
+     * @param obj 对象
+     * @return 对象的getter方法列表
+     */
+
+    public static List<Method> getGetterMethods(Object obj)
+    {
+        // getter方法列表
+        List<Method> getterMethods = new ArrayList<Method>();
+        // 获取所有方法
+        Method[] methods = obj.getClass().getMethods();
+        // 查找getter方法
+        for (Method method : methods)
+        {
+            Matcher m = GET_PATTERN.matcher(method.getName());
+            if (m.matches() && (method.getParameterTypes().length == 0))
+            {
+                getterMethods.add(method);
+            }
+        }
+        // 返回getter方法列表
+        return getterMethods;
+    }
+
+    /**
+     * 检查Bean方法名中的属性名是否相等。<br>
+     * 如getName()和setName()属性名一样,getName()和setAge()属性名不一样。
+     * 
+     * @param m1 方法名1
+     * @param m2 方法名2
+     * @return 属性名一样返回true,否则返回false
+     */
+
+    public static boolean isMethodPropEquals(String m1, String m2)
+    {
+        return m1.substring(BEAN_METHOD_PROP_INDEX).equals(m2.substring(BEAN_METHOD_PROP_INDEX));
+    }
+
+    public static void main(String[] args)
+    {
+        User user = new User();
+
+        User zhen = new User();
+        zhen.setUserName("测试名称");
+        Dept dept = new Dept();
+        dept.setDeptId(11L);
+        dept.setDeptName("测试部门");
+        zhen.setDept(dept);
+
+        BeanUtils.copyBeanProp(user, zhen);
+
+        System.out.println(user.getDept().getDeptName());
+    }
+}

+ 5 - 1
src/main/java/com/ruoyi/common/utils/security/ShiroUtils.java

@@ -6,6 +6,8 @@ import org.apache.shiro.session.Session;
 import org.apache.shiro.subject.PrincipalCollection;
 import org.apache.shiro.subject.SimplePrincipalCollection;
 import org.apache.shiro.subject.Subject;
+
+import com.ruoyi.common.utils.bean.BeanUtils;
 import com.ruoyi.framework.shiro.realm.UserRealm;
 import com.ruoyi.project.system.user.domain.User;
 
@@ -34,7 +36,9 @@ public class ShiroUtils
 
     public static User getUser()
     {
-        return (User) getSubjct().getPrincipal();
+        User user = new User();
+        BeanUtils.copyBeanProp(user, getSubjct().getPrincipal());
+        return user;
     }
 
     public static void setUser(User user)

+ 2 - 12
src/main/java/com/ruoyi/project/monitor/job/util/ScheduleJob.java

@@ -4,8 +4,6 @@ import java.util.Date;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
 import java.util.concurrent.Future;
-import org.apache.commons.beanutils.PropertyUtils;
-import org.quartz.JobDataMap;
 import org.quartz.JobExecutionContext;
 import org.quartz.JobExecutionException;
 import org.slf4j.Logger;
@@ -13,6 +11,7 @@ import org.slf4j.LoggerFactory;
 import org.springframework.scheduling.quartz.QuartzJobBean;
 import com.ruoyi.common.constant.Constants;
 import com.ruoyi.common.constant.ScheduleConstants;
+import com.ruoyi.common.utils.bean.BeanUtils;
 import com.ruoyi.common.utils.spring.SpringUtils;
 import com.ruoyi.project.monitor.job.domain.Job;
 import com.ruoyi.project.monitor.job.domain.JobLog;
@@ -33,17 +32,8 @@ public class ScheduleJob extends QuartzJobBean
     @Override
     protected void executeInternal(JobExecutionContext context) throws JobExecutionException
     {
-        // Job job = (Job) context.getMergedJobDataMap().get(ScheduleConstants.JOB_PARAM_KEY);
-        JobDataMap jobDataMap = context.getMergedJobDataMap();
         Job job = new Job();
-        try
-        {
-            PropertyUtils.copyProperties(job, jobDataMap.get(ScheduleConstants.JOB_PARAM_KEY));
-        }
-        catch (Exception e)
-        {
-            log.error("copyProperties执行异常  - :", e);
-        }
+        BeanUtils.copyBeanProp(job, context.getMergedJobDataMap().get(ScheduleConstants.JOB_PARAM_KEY));
 
         IJobLogService jobLogService = (IJobLogService) SpringUtils.getBean(IJobLogService.class);
 

+ 2 - 1
src/main/resources/application.yml

@@ -42,7 +42,8 @@ spring:
       max-request-size: 30Mb
   devtools:
     restart:
-      enabled: true    
+      #禁用devtools模块的热部署功能
+      enabled: true
 # MyBatis
 mybatis:
     # 搜索指定包别名

+ 1 - 1
src/main/resources/templates/index.html

@@ -35,7 +35,7 @@
                          <img th:src="(${user.avatar} == '') ? '/img/profile.jpg' : '/profile/' + ${user.avatar}" alt="image" class="img-circle" height="60" width="60"/></span>
                          <a data-toggle="dropdown" class="dropdown-toggle" href="#">
                           <span class="clear"><span class="block m-t-xs"><strong class="font-bold">[[${user.userName}]]</strong></span>
-                         <span class="text-muted text-xs block"><span>[[${user.dept.deptName}]]</span> <b class="caret"></b></span> </span> </a>
+                         <span class="text-muted text-xs block"><span th:if="${not #strings.isEmpty(user.dept)}">[[${user.dept.deptName}]]</span> <b class="caret"></b></span> </span> </a>
                          <ul class="dropdown-menu animated fadeInRight m-t-xs">
                              <li><a class="menuItem" th:href="@{/system/user/profile}">个人信息</a></li>
                              <li class="divider"></li>