小编dur*_*597的帖子

在消息源 Spring 中不处理此类消息异常

我正在使用 Spring Message 源来加载我的属性文件。

String placeDetails = messageSource.getMessage(code,
                        null, new Locale(locale.toLowerCase()));
Run Code Online (Sandbox Code Playgroud)

属性文件中的示例条目:

BNA=Nashville:Nashville:USA
Run Code Online (Sandbox Code Playgroud)

属性文件名:

placeDetails_locale.properties
Run Code Online (Sandbox Code Playgroud)

messagesource.getMessage 方法返回分配的值 ex If code "BNA" String "placeDetails" will hold "Nashville:Nashville:USA".If the code not found in the property file it throw "No such message found exception"。但我需要处理这种情况,例如如果在该场景中找不到消息,我必须将默认值设置为 placeDetails。

我尝试的方法: 1.我需要先检查如果该属性文件中的代码可用,那么我只需要调用 get messagesource 方法。但我不知道如何通过消息源检查值的存在 2.我需要定义在 catch 块中查找默认值。

我尝试了第二次但没有按预期工作。帮助我了解如何通过消息源检查属性文件中代码的可用性(第一个方法)

java spring message

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

在非构造函数方法中使用Guice注入依赖项

我正在研究一个动态创建HTTP请求的组件,我希望能够模拟这些单元测试请求.

目前实现看起来像这样:

class ModelClass {
     public void populate() {
          HTTPRequest request = new HTTPRequest();
          //configure request...
          request.send();
     }
}
Run Code Online (Sandbox Code Playgroud)

有没有办法使用Guice实例化,request所以我可以用模拟类的实例替换它进行测试?我能想出的最近的是添加一个注入器作为ModelClass的实例变量:

class ModelClass {
     private final Injector injector;

     ModelClass(Injector injector){
          this.injector = injector;
     }

     public void populate() {
          HTTPRequest request = injector.getInstance(HTTPRequest.class);
          //configure request...
          request.send();
     }
}
Run Code Online (Sandbox Code Playgroud)

但这基本上就像使用工厂一样,完全错过了Guice的观点.

java tdd mocking guice roboguice

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

Guice @Singleton必须遵循Singleton设计模式吗?

注释的类@Singleton必须遵循Singleton设计模式吗?

我的猜测是他们没有:没有必要拥有私有构造函数和static .instance()方法,而是Guice确保只实例化该类的一个实例.

java singleton design-patterns guice

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

将TFS签入策略升级到VS2015的问题

我有两个适用于VS2013的策略.我想让这些策略适用于VS2015,但找不到VS2015的TFS API库.我尝试添加这个

"C:\ Program Files(x86)\ Microsoft Visual Studio 12.0\Common7\IDE\ReferenceAssemblies\v2.0\Microsoft.TeamFoundation.VersionControl.Client.dll"

作为我的政策项目的参考,它编译得很好.当我尝试注册它们时,其中一个工作,另一个没有.我得到以下例外:

[MyPolicy]中的内部错误.加载[MyPolicy]策略时出错(策略程序集"MyPolicy,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null"未注册.).安装说明:请参阅基于Web的安装说明.

为什么第一个政策得到了良好的审判,而不是第二个?有人知道这个问题吗?

c# tfs visual-studio-2015

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

为什么我在课堂上遇到了捕获适用性错误?

我写了以下代码:

public class HashMapImpl<Key,Value>{
    Key key;
    Value value;

    List<? extends Key> keylist;
    List<? extends Value> valuelist;

    public HashMapImpl(Key k,Value v){
        this.key = k;
        this.value = v;

        keylist = new ArrayList<Key>();
        valuelist = new ArrayList<Value>();
    }

    public Value put(Key k, Value v){
        this.keylist.add(k);
    }
}
Run Code Online (Sandbox Code Playgroud)

我收到此错误add():

add(capture#3-of ? extends Key)类型中的方法List<capture#3-of ? extends Key>不适用于参数(Key)

为什么会这样?我该如何解决?

java generics list

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

How do I substitute Guice modules with fake test modules for unit tests?

Here's how we are using Guice in a new application:

public class ObjectFactory {
  private static final ObjectFactory instance = new ObjectFactory();
  private final Injector injector;

  private ObjectFactory() throws RuntimeException {
    this.injector = Guice.createInjector(new Module1());
  }

  public static final ObjectFactory getInstance() {
    return instance;
  }

  public TaskExecutor getTaskExecutor() {
    return injector.getInstance(TaskExecutor.class);
  }
}
Run Code Online (Sandbox Code Playgroud)

Module1 defines how the TaskExecutor needs to be constructed.

In the code we use ObjectFactory.getInstance().getTaskExecutor() to obtain and the instance of TaskExecutor.

In unit tests we …

java spring unit-testing guice

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

为什么我会收到未经检查的分配警告?

使用 Gson 将 JSON 字符串转换为 Java 对象时,我收到此警告。为什么我得到它,我该如何解决它?

这是我在代码中收到的警告:

Unchecked assignment: 
    'net.brawtasports.brawtasportsgps.model.JSONKeys' to 
    'net.brawtasports.brawtasportsgps.model.JSONKeys<net.brawtasports.brawtasportsgps.model.team.Object>'
Run Code Online (Sandbox Code Playgroud)

这是我在运行时得到的错误:

java.lang.ClassCastException:
    com.google.gson.internal.LinkedTreeMap cannot be cast to
    net.brawtasports.brawtasportsgps.model.team.Object
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

  String jsonString = Preferences.readFromPreferences(ApplicationConstants.team_data,"");
    Gson gson = new Gson();
    JSONKeys<Object> teamMembers = gson.fromJson(jsonString, JSONKeys.class); //converts json string to java object
    Object players = teamMembers.getObject();//object is my custom class
    //ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_spinner_dropdown_item,players);
    ArrayAdapter arrayAdapter = new ArrayAdapter(getActivity(),android.R.layout.simple_spinner_dropdown_item,players.getPlayersSummary());
    player1.setAdapter(arrayAdapter);
Run Code Online (Sandbox Code Playgroud)

这是我的 JSONKeys POJO 的代码:

   public class JSONKeys<T> {

private boolean Success;
private String Message;
private int ObjectIdentifier;
private T …
Run Code Online (Sandbox Code Playgroud)

java android json classcastexception gson

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

将List设置为常量是否合理有效?

Google的Oauth2库方法需要一个针对scopes参数的Java列表.将参数编码为List文字是否合理,如下所示:

public  static  final List<String> YOUTUBE_SCOPES = Lists.newArrayList(
      "https://www.googleapis.com/auth/youtube.upload",
      "https://www.googleapis.com/auth/youtube.readonly");
Run Code Online (Sandbox Code Playgroud)

这样我就可以:

SendToGoogleUtils.getGoogleAccountCredential(activity, accountName, YOUTUBE_SCOPES );
Run Code Online (Sandbox Code Playgroud)

Java编译器是否会创建有效的YOUTUBE_SCOPES文字?

java google-api guava

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

为什么我的while循环不能在paintComponent中工作?

当我运行此代码时,我只看到一个空白(白色)面板,我想知道原因.

这是我的代码:

Graph.java

public class Graph extends JPanel {
    private static final long serialVersionUID = -397959590385297067L;
    int screen=-1;
    int x=10;
    int y=10;
    int dx=1;
    int dy=1;       
    boolean shouldrun=true;
    imageStream imget=new imageStream();

        protected void Loader(Graphics g){

            g.setColor(Color.black);
            g.fillRect(0,0,x,y);
            x=x+1;
            y=y+2;

        }


        @Override
        protected void paintComponent(Graphics g){
            super.paintComponent(g);
                while(shouldrun){
                    Loader(g);   
                    try {
                        Thread.sleep(200);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }    
                }   
        }
}
Run Code Online (Sandbox Code Playgroud)

java swing jpanel paintcomponent

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

为什么我将PDF格式的内容类型作为HTML返回?

我试图使用以下代码查看Web URL的内容类型.

有趣的是,给定URL(http://www.jbssinc.com/inv_pr_pdf/2007-05-08.pdf")的内容类型返回为text/html; charset=iso-8859-1即使它是PDF文档也会.我想了解原因.

这是我的代码:

public static void main(String[] args) throws MalformedURLException{
    URLConnection urlConnection = null;
    URL url  = new URL("http://www.jbssinc.com/inv_pr_pdf/2007-05-08.pdf");
    try {
        urlConnection = url.openConnection();
        urlConnection.setConnectTimeout(10*1000);
        urlConnection.setReadTimeout(10*1000);
        urlConnection.connect();

    } catch (IOException e) {
        System.out.println("Error in establishing connection.\n");
    }
    String contentType = "";
    /* If we were able to get a connection ---> */
    if (urlConnection != null) {
        contentType = urlConnection.getContentType();
    }
    System.out.println(contentType);
}
Run Code Online (Sandbox Code Playgroud)

java url content-type web-crawler mime-types

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