小编Chr*_*Geo的帖子

战略设计模式,泛型和类型安全

我想创建以下策略模式与Factory结合,但我希望它是类型安全的.到目前为止我做了以下事情:

public interface Parser<T> {

    public Collection<T> parse(ResultSet resultSet);

}


public class AParser implements Parser<String> {

    @Override
    public Collection<String> parse(ResultSet resultSet) {
             //perform parsing, get collection
        Collection<String> cl = performParsing(resultSet); //local private method
        return cl;
    }
}

public class ParserFactory {

    public enum ParserType {
        APARSER
    }


    public static <T> Parser<T> createParser(ParserType parserType) {


        Parser<?> parser = null;
        switch (parserType) {
        case APARSER:
            parser = new AParser();
            break;
        }
            //unchecked cast happens here
        return (Parser<T>) parser;
    }
}


public class …
Run Code Online (Sandbox Code Playgroud)

java generics strategy-pattern factory-pattern

8
推荐指数
2
解决办法
2938
查看次数

Spring Data Rest - 具有默认值的参数

我创建了以下@RepositoryRestResource查询,我想为其余的api创建动态查询.所以基本上我想做的事情如下:

myHost/myApp/data/search/all?name=me&age=20&address=myhome&etc=etc
Run Code Online (Sandbox Code Playgroud)

所以我在下面创建了查询:

   @Query("Select t from Data t " +
            "where " +
            "t.name like :name AND " +
            "t.age = :age AND " +
            "t.address = :address AND " +
            "t.etc= :etc"
    @RestResource(path = "all", rel = "all")
    Page findByAll(@Param("name") String name, @Param("age") String age,
            @Param("address") String address, @Param("etc") String etc, Page page);
Run Code Online (Sandbox Code Playgroud)

显然其中一些可能尚未输入.有没有办法在存储库中定义默认值?所以例如我想要name一个默认值%.

我不完全确定这种方法对于我想要做的是正确的,所以欢迎任何替代建议.

java spring spring-data-rest

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

退出 jframe 时摆动停止计时器

我有一个特定的任务,我按计划的时间间隔运行。基本上,我在 JFrame 中的标签上显示相机提要。但是,当我退出 JFrame 时,应用程序似乎在运行。我怎样才能阻止它?我已经剥离了代码的细节,只留下了相关的部分

public class TaskCLass extends JFrame {

    JPanel p;
    JLabel l;
    Timer timer;

    public TaskCLass() {

        p = new JPanel();
        l = new JLabel("Window");
        add(p);
        p.add(l);
        setSize(700, 600);

        this.addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent e) {
                System.out.println(e);
                timer.purge();
                timer.cancel();
                System.exit(1);
            }

            public void windowClosed(WindowEvent e) {
                System.out.println(e);
                timer.purge();
                timer.cancel();
                System.exit(1);
            }
        });


        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        startTask();
    }

    public static void main(String[] args) {

        new TaskCLass();
    }


    public void startTask() {


        TimerTask t = new TimerTask() { …
Run Code Online (Sandbox Code Playgroud)

java swing camera timer jframe

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

MongoDB按日期聚合查询组

我在mongoDB中有以下集合

{ _id, startTime, duration }
Run Code Online (Sandbox Code Playgroud)

因此,基本的想法是摄像机可以找到人,一旦它检测到一个人就会保存startTime,一旦一个人消失,就可以节省持续时间.因此,该实体基本上说"一个人出现在X时间,并且在摄像机范围内为Y毫秒".startTime和duration都是数值.

所以,我想执行各种查询,例如:1.给我每月/每年的人数2.给我每月的人数,持续时间> 5000毫秒

等等

虽然我对MongoDB很新,但我对聚合框架有点麻烦,所以如果有人让我知道如何进行如上所述的查询,我会很感激,以便获得某种先发制人的想法. .

编辑:

好吧,我做了几次尝试,但没有运气.现在我的对象有这样的形式:

{
  "_id" : ObjectId("52de407c75895eaf5ea99715"),
  "startTime" : "new Date('02 01 2011 08:36:54')",
  "duration" : 27000
}
Run Code Online (Sandbox Code Playgroud)

我正在尝试这个查询:

 db.collection.aggregate(
        {$project : {
             year : {$year : "$startTime"}

        }},
        {$group : {
             _id : {year : "$year"}, 
             count : {$sum : 1}
        }}
    )
Run Code Online (Sandbox Code Playgroud)

但我得到以下例外:

Error occurred in performing aggregation
Command 'aggregate' failed: exception: can't convert from BSON type String to Date (response: { "errmsg" : "exception: can't convert …
Run Code Online (Sandbox Code Playgroud)

mongodb aggregation-framework

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

Angular UI Select,预选数组的唯一值

假设我想要编辑一个现有实体,该实体具有一系列值,这些值也是可选值的一部分.例如

var preSelectedLanguages = [
    {id: 2, iso: "de"},
    {id: 3, iso: "fr"}
]

var languages = [
    {id: 1, iso: "en"},
    {id: 2, iso: "de"},
    {id: 3, iso: "fr"},
    {id: 4, iso: "it"},
    {id: 5, iso: "us"}
]
Run Code Online (Sandbox Code Playgroud)

我的ui-select指令类似于:

<ui-select multiple ng-model="preSelectedLanguages">
    <ui-select-match placeholder="Select language...">
        {{$item.iso}}
    </ui-select-match>
    <ui-select-choices repeat="l in languages track by language.id">
        {{language.iso}}
    </ui-select-choices>
</ui-select>
Run Code Online (Sandbox Code Playgroud)

然而ui-select-choices选项列表似乎无法删除重复项,即使我已经使用过track by language.id.

知道如何正确地做到这一点?

angularjs ui-select angular-ui-select

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

JPQL Group By Distinct

让我们假设我有以下实体(省略了注释/ getter设置器):

@Entity
public class Topic {

   @Id
   private Long id;
      private String title;
   private String text;

   @OneToMany(mappedBy = "topic", cascade = CascadeType.REMOVE)
   private List<Post> posts = new LinkedList();

}

@Entity
public class Post {

    @Id
    private Long id;
    private String text;

    @Temporal(TemporalType.DATE)
    private Date createdOn;

    @Temporal(TemporalType.DATE)
    private Date updatedOn;

    @ManyToOne
    private Topic topic;
}
Run Code Online (Sandbox Code Playgroud)

我想运行一个JPQL基本上Topics根据最新的排序的查询Post.

所以我创建的查询如下:

select t from Topic t join t.posts p
group by t, p
order by p.updatedOn desc
Run Code Online (Sandbox Code Playgroud)

然而 …

java hibernate jpa jpql

5
推荐指数
0
解决办法
451
查看次数

Weblogic 12c(12.1.3)-字符串索引超出范围:51968

我正在尝试Spring Boot在Weblogic 12.1.3上部署Web应用程序。

从控制台部署时,出现以下错误(在应用程序上)

Message icon - Error Unable to access the selected application.
Message icon - Error String index out of range: 51968
Message icon - Error String index out of range: 51968
Message icon - Error String index out of range: 51968
Run Code Online (Sandbox Code Playgroud)

另一方面,它可以从Intellij IDEAautodeploy文件夹成功部署。另外,在其他开发机器上,它甚至可以从控制台成功部署,但在其他机器上则不能。

服务器日志上也没有任何错误。

有什么原因的想法吗?

java weblogic12c

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

Use web.xml security constraints with Spring Boot

I have a peculiar case where I need to use the Application Server (Weblogic) security context for authentication, but Spring Security for authorization. I am using Spring Boot to create my application.

How can I add a security constraint like follows (which would normally be contained in web.xml):

<security-constraint>
        <web-resource-collection>
            <web-resource-name>portal</web-resource-name>
            <description>This is the protected area of the application.</description>
            <url-pattern>/*</url-pattern>
        </web-resource-collection>
        <auth-constraint>
            <description>Requires users to be authenticated but does not require them to be authorized.</description>
            <role-name>*</role-name>
        </auth-constraint>
        <user-data-constraint>
            <description>Encryption …
Run Code Online (Sandbox Code Playgroud)

java security spring

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

EJB 3.1 通用 DAO

我正在尝试实现一个通用 DAO 以在我的项目中使用。到目前为止,我已经实现的代码如下:

基础DAO

public abstract class BaseDAO<T> {

    private Class<T> entityClass;

    public BaseDAO(Class<T> entityClass) {
        this.entityClass = entityClass;
    }

    protected abstract EntityManager getEntityManager();

    public T persist(T entity) {
        getEntityManager().persist(entity);
        return entity;
    }

    public T edit(T entity) {
        getEntityManager().merge(entity);
        return entity;
    }

    public T remove(T entity) {
        getEntityManager().remove(entity);
        return entity;
    }

    public List<T> findAll() {

    Query q = getEntityManager().createQuery("SELECT e FROM " + entityClass.getName()
                + " e");
        List<T> list = (List<T>) q.getResultList();
        return list;
    }

    public T find(Long id) { …
Run Code Online (Sandbox Code Playgroud)

dao ejb java-ee-6

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

嵌入式实体的Spring Data Rest投影

让我们假设我有以下实体:

@Entity
public class Registration {

    @ManyToOne
    private Student student;
    //getters, setters
}

@Entity
public class Student {

    private String id;
    private String userName;
    private String name;
    private String surname;
    //getters, setters
}

@Projection(name="minimal", types = {Registration.class, Student.class})
public interface RegistrationProjection {

    String getUserName();
    Student getStudent();

}
Run Code Online (Sandbox Code Playgroud)

我正在尝试创建以下JSON表示,所以当我使用时,http://localhost:8080/api/registrations?projection=minimal我不需要所有用户数据:

{
  "_links": {
    "self": {
      "href": "http://localhost:8080/api/registrations{?page,size,sort,projection}",
    }
  },
  "_embedded": {
    "registrations": [
      {
        "student": {
          "userName": "user1"
        }
      }
    ],
    "_links": {
      "self": {
        "href": "http://localhost:8080/api/registrations/1{?projection}",
      }
    }
  } …
Run Code Online (Sandbox Code Playgroud)

java spring spring-data-rest

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