小编Sot*_*lis的帖子

Git无法识别重命名和修改的包文件

我有一个名为的java文件package/old/myfile.java.我通过git提交了这个文件.然后我重命名我的包,new所以我的文件在package/new/myfile.java.

我现在想要将此文件重命名(和内容更改)提交给git.

我什么时候git status得到

# Changes not staged for commit:
#   (use "git add/rm <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       deleted:    package/old/myfile.java
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       package/new/myfile.java
Run Code Online (Sandbox Code Playgroud)

我试过add新的和rm旧的,反之亦然,我一直在努力

$ git status
# On branch develop
# Changes to be …
Run Code Online (Sandbox Code Playgroud)

java git

13
推荐指数
2
解决办法
1万
查看次数

Spring - 拦截bean创建和注入自定义代理

我有一个@Controller带有@Autowired字段和处理程序方法,我想用自定义注释进行注释.

例如,

@Controller
public class MyController{
    @Autowired
    public MyDao myDao;

    @RequestMapping("/home")
    @OnlyIfXYZ
    public String onlyForXYZ() {
        // do something
        return "xyz";
    }
}
Run Code Online (Sandbox Code Playgroud)

@OnlyIfXYZ自定义注释的示例在哪里.我以为我会拦截Controller bean的创建,传递我自己的CGLIB代理,Spring可以在其上设置属性,比如autowired字段.

我尝试使用InstantiationAwareBeanPostProcessor但是该解决方案不起作用,因为postProcessBeforeInstantiation()短路过程的其余部分.我尝试过postProcessAfterInitialization(),如下所示

public class MyProcessor implements BeanPostProcessor {

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        // Here the bean autowired fields are already set
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object aBean, String aBeanName) throws BeansException {
        Class<?> clazz = aBean.getClass();
        // …
Run Code Online (Sandbox Code Playgroud)

java spring spring-mvc spring-aop

13
推荐指数
2
解决办法
1万
查看次数

为什么嵌套类的私有成员可以通过封闭类的方法访问?

谁能告诉我私人会员的访问级别?很长一段时间我一直对这段代码感到困惑:为什么私有成员,Line类的k,可以在outter类的"print"方法中访问?

public class myClass {
    public static class Line{
        private double k;
        private double b;
        private boolean isVertical;

        public Line(double k, double b, boolean isVertical){
            this.k = k;
            this.b = b;
            this.isVertical = isVertical;
        }

    }

    public static boolean print(Line line){
        System.out.println(line.k);
    }
}
Run Code Online (Sandbox Code Playgroud)

java

13
推荐指数
1
解决办法
8019
查看次数

日期格式化java中kk和HH之间的区别

我是java的新手,我正在尝试使用24小时格式来格式化时间.我有两种格式化小时的方法--HH和kk:

SimpleDateFormat format1 new SimpleDateFormat("HH:mm");
SimpleDateFormat format2 new SimpleDateFormat("kk:mm");
Date date = new Date();
System.out.println(format1.format(date));
System.out.println(format2.format(date));
Run Code Online (Sandbox Code Playgroud)

这些都产生类似11:21的东西.他们之间有什么区别?我错过了什么吗?

java datetime date-format simpledateformat

13
推荐指数
2
解决办法
1万
查看次数

如何翻转一个字母?

在这里,我试图将英文字母颠倒翻转为我的邮件模板.

好吧,我可以手动获取它.我正在做的是现在手动

就像

 content.append("?"); //actual h letter.
 content.append("?")// actual e letter.
Run Code Online (Sandbox Code Playgroud)

所以我的问题是,是否有任何技巧/方法/任何线索以程序化的方式做到这一点?

我现在正在做的是

switch(letter) {

case 'e':
   return '?';
}
Run Code Online (Sandbox Code Playgroud)

这看起来很奇怪,并寻找提示.谢谢你的帮助.

java char character-encoding

13
推荐指数
3
解决办法
3990
查看次数

LambdaMetafactory的Java访问bean方法

我的问题与 该线程中显式使用LambdaMetafactory密切相关,提供了一些非常好的例子来使用LambdaMetafactory访问类的静态方法; 但是,我想知道访问现有bean实例的非静态字段的等效代码是什么.似乎很难找到一个例子,我所执行的每一次尝试都以非工作代码结束.

这是bean代码:

class SimpleBean {
    private Object obj= "myCustomObject";
    private static Object STATIC_OBJECT = "myCustomStaticObject";
    public Object getObj() {
        return obj;
    }
    public void setObj(final Object obj) {
        this.obj = obj;
    }
    public static Object getStaticObj() {
        return STATIC_OBJECT;
    }
    public static void setStaticObj(final Object obj) {
        STATIC_OBJECT = obj;
    }
}
Run Code Online (Sandbox Code Playgroud)

这里是一个成功访问静态方法"getStaticObj()"的工作单元测试:

    @Test
public void accessStaticMethod() throws Throwable
{
    MethodHandles.Lookup caller = MethodHandles.lookup();
    Method reflected = SimpleBean.class.getDeclaredMethod("getStaticObj");
    MethodHandle methodHandle = caller.unreflect(reflected);
    CallSite site = LambdaMetafactory.metafactory(caller, …
Run Code Online (Sandbox Code Playgroud)

java lambda java-8

13
推荐指数
1
解决办法
4277
查看次数

为什么Gson fromJson抛出一个JsonSyntaxException:预期BEGIN_OBJECT但是BEGIN_ARRAY?

(这篇文章是一个规范性的问题,下面提供了一个示例答案.)


我正在尝试将一些JSON内容反序列化为自定义POJO类型Gson#fromJson(String, Class).

这段代码

import com.google.gson.Gson;

public class Sample {
    public static void main(String[] args) {
        String json = "{\"nestedPojo\":[{\"name\":null, \"value\":42}]}";
        Gson gson = new Gson();
        gson.fromJson(json, Pojo.class);
    }
}

class Pojo {
    NestedPojo nestedPojo;
}

class NestedPojo {
    String name;
    int value;
}
Run Code Online (Sandbox Code Playgroud)

抛出以下异常

Exception in thread "main" com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 16 path $.nestedPojo
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:200)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:103)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:196)
    at com.google.gson.Gson.fromJson(Gson.java:810)
    at com.google.gson.Gson.fromJson(Gson.java:775)
    at com.google.gson.Gson.fromJson(Gson.java:724)
    at …
Run Code Online (Sandbox Code Playgroud)

java json gson

13
推荐指数
2
解决办法
4万
查看次数

为什么Collections.swap将目标列表分配给原始类型的变量?

在JDK 1.6的源代码中,Collections类的swap方法如下所示:

public static void swap(List<?> list, int i, int j) {
    final List l = list;
    l.set(i, l.set(j, l.get(i)));
}
Run Code Online (Sandbox Code Playgroud)

创建传递列表的最终副本的原因是什么?他们为什么不直接修改传递的列表?在这种情况下,您还会获得原始类型警告.

java

12
推荐指数
1
解决办法
833
查看次数

在Jersey webapp启动时初始化数据库

我读过这个,但我不太明白它是如何工作的.我想在我的Web应用程序启动时加载属性文件并设置我的连接池.显然,我只想在一个地方做一次,所以如果需要我可以改变它.使用常规servlet,我只需将初始化代码放在servlet的init()方法中,但您无法使用Jersey servlet访问它.那我该怎么办?上面链接中的听众如何工作?

java web-applications initialization jersey

12
推荐指数
1
解决办法
2万
查看次数

如何在Spring中定义String数组的bean(使用XML配置)

我正在尝试定义类型的Spring bean,String[]现在能够找到一种方法.示例程序如下所示:

@Component("sampleClass")
public class SampleClass {
    @Value("#{someArrayId}")
    private String[] someArray;

    public void doWithArray() {
        System.out.println(Arrays.toString(someArray));
    }
}
Run Code Online (Sandbox Code Playgroud)

Spring XML配置

<context:annotation-config />
<context:component-scan base-package="com.demo.spring" />

<util:list id="someArrayId">
    <array>
        <value>Tiger</value>
        <value>Lion</value>
    </array>
</util:list>
Run Code Online (Sandbox Code Playgroud)

当我运行该程序时,我得到以下异常:

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sampleClass': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.lang.String[] com.demo.spring.SampleClass.someArray; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert value of type 'java.util.ArrayList' to required type 'java.lang.String[]'; nested exception is …
Run Code Online (Sandbox Code Playgroud)

java spring

12
推荐指数
3
解决办法
3万
查看次数