小编dnc*_*253的帖子

Java - 在构造函数中设置后,任何使变量不可修改的方法?

我有一个成员变量,在类中我想确保在构造函数中设置后没有任何更改.所以,它看起来像这样:

public class MyClass
{
    private String myVar = null;

    public MyClass(DataObj dataObj)
    {
        myVar = dataObj.getFinalVarValue();
        //at this point I don't want anything to be able change the value of myVar
    }

    ....
}
Run Code Online (Sandbox Code Playgroud)

我的第一个想法是看到我可以将final修饰符添加到变量中,但编译器抱怨为最终字段赋值.有可能做到这一点吗?

java final

3
推荐指数
1
解决办法
726
查看次数

你如何模拟作为函数的Angular服务?

我们有一个我们称之为a的东西CORShttpService,它基本上是一个服务器的包装器$http,但封装了我们需要的一些CORS功能.我现在正在为CORShttpService注入其中的服务编写一些测试.此服务的代码如下:

CORShttpService({method: requestMethod, url: getUrl(path), data: data}).
    success(function(data, status, headers) {
        //do success stuff
    }).
    error(function(data, status, headers) {
       //do error stuff
    });
Run Code Online (Sandbox Code Playgroud)

我想嘲笑电话CORShttpService,但我不知道如何去做.我正在使用Jasmine,它的spyOn功能需要一个对象来模拟对象上的函数.我CORShttpService没有任何对象,所以我不知道怎么去嘲笑它.是的,我可以$httpBackend用来模拟最终设置的请求CORShttpService,但我不希望它首先进入该服务.我想隔离单元测试并简单地模拟外部调用.有什么方法可以嘲笑这个只是一个功能的服务吗?

javascript unit-testing jasmine angularjs

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

JPA with Java Generics

我有几个具有基本LongID和其他属性的实体.这些实体与另一个实体具有一对多的关系,该实体为第一个实体保存不同的自定义(用户输入的)翻译.基本上是实体对,其中一个包含所有非翻译内容,另一个包含文本属性的多个翻译.

为了减少代码和注释的重复,我想为其中一对中的每个实体创建一个抽象类.对于多个翻译,我创建了一个这样的类:

@MappedSuperclass
public abstract class CustomTranslations
{
    @Id
    protected Long id;
    @Id
    protected String locale;
}
Run Code Online (Sandbox Code Playgroud)

对于主要实体,我有这个:

@MappedSuperclass
public abstract class CustomTranslationsHolder<T extends CustomTranslations>
{
    @OneToMany(fetch=FetchType.EAGER)
    @JoinColumn(name="ID")
    @MapKey(name="locale")
    protected Map<String, T> translationsByLocale;
}
Run Code Online (Sandbox Code Playgroud)

所以说其中一个实体对是Foo.我会这样的:

@Entity
@Table(name="FOO_TRANSLATIONS")
public class FooTranslations extends CustomTranslations
{
    private String title;
    private String description;
    ....
}
Run Code Online (Sandbox Code Playgroud)

还有这个:

@Entity
public class Foo extends CustomTranslationsHolder<FooTranslations>
{
    @Id
    private Long id;
    private String whatever;
    private Integer blah;
    ...
    public String getTitle(String locale)
    {
        return …
Run Code Online (Sandbox Code Playgroud)

java generics jpa eclipselink

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

传递给子类的constructor-arg时"无法解析匹配的构造函数"错误

我有以下课程:

public abstract class ParentClass
{
    public ParentClass()
    {
        throw new RuntimeException("An ID must be specified.");
    }

    public ParentClass(String id)
    {
        this(id, DEFUALT_ARG_VALUE);
    }

    public ParentClass(String id, int anotherArg)
    {
        this.id = id;
        //stuff with anotherArg
    }

    public abstract void doInstanceStuff();
}

public class ChildClass extends ParentClass
{
    @Override
    public void doInstanceStuff()
    {
        //....
    }
}
Run Code Online (Sandbox Code Playgroud)

在我的应用程序上下文中我有这个:

<bean id="myChildInstance" class="com.foo.bar.ChildClass " scope="singleton">
    <constructor-arg value="myId" />
</bean>
Run Code Online (Sandbox Code Playgroud)

问题是,当服务器启动时,我收到以下错误:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ivpluginHealthCheckTest' defined in ServletContext resource [/WEB-INF/spring/root-context.xml]: Could …
Run Code Online (Sandbox Code Playgroud)

java inheritance spring constructor

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