Tag: spring

MongoTemplate聚合 – 按日期分组

我正在尝试使用mongotemplate创建聚合查询,其中按日期(即2016-03-01)而不是datetime(即2016-03-01 16:40:12)进行分组。 dateToString操作存在于mongodb文档中,它可用于使用格式化从日期时间中提取日期: https : //docs.mongodb.org/manual/reference/operator/aggregation/dateToString/但我得到了它可以使用它mongotemplate – 我得到一个NullPointerException。 (我的db版本是3.2) List aggregationOperations = new ArrayList(); aggregationOperations.add( Aggregation.project(“blabla”, …). andExpression(“dateToString(‘%Y-%m-%d’,timeCreated).as(“date”)); aggregationOperations.add(Aggregation.group(“date”).sum(“blabla”).as(“blabla”)); AggregationResults aggregationResults = this.mongoTemplate.aggregate( Aggregation.newAggregation(aggregationOperations), collectionName, resultClass); 当我使用dayOfMonth(timeCreated)来提取日期时,没有例外,但是我找不到如何使用dateToString进行此操作的示例。 我尝试没有”作为日期格式,它也没有工作…… 这是我得到的例外: java.lang.NullPointerException at org.bson.BasicBSONEncoder._putObjectField(BasicBSONEncoder.java:226) at org.bson.BasicBSONEncoder.putObject(BasicBSONEncoder.java:194) at org.bson.BasicBSONEncoder._putObjectField(BasicBSONEncoder.java:255) at org.bson.BasicBSONEncoder.putObject(BasicBSONEncoder.java:194) at org.bson.BasicBSONEncoder._putObjectField(BasicBSONEncoder.java:255) at org.bson.BasicBSONEncoder.putObject(BasicBSONEncoder.java:194) at org.bson.BasicBSONEncoder._putObjectField(BasicBSONEncoder.java:255) at org.bson.BasicBSONEncoder.putIterable(BasicBSONEncoder.java:324) at org.bson.BasicBSONEncoder._putObjectField(BasicBSONEncoder.java:263) at org.bson.BasicBSONEncoder.putObject(BasicBSONEncoder.java:194) at org.bson.BasicBSONEncoder.putObject(BasicBSONEncoder.java:136) at com.mongodb.DefaultDBEncoder.writeObject(DefaultDBEncoder.java:36) at […]

ParameterizedType并创建一个通用的dao

我尝试这个通用代码,因为我不想为我的数据库中的每个实体创建一个dao类,因为我有80个专门用于那些我将只执行CRUD查询的人。 因为在大多数情况下我只需要坚持或通过id进行查找。 public interface GenericDao { T create(T t); T read(PK id); T update(T t); void delete(T t); } 接口的impl @Component public class GenericDaoJpaImpl implements GenericDao { protected Class entityClass; @PersistenceContext protected EntityManager entityManager; public GenericDaoJpaImpl() { ParameterizedType genericSuperclass = (ParameterizedType) getClass() .getGenericSuperclass(); this.entityClass = (Class) genericSuperclass .getActualTypeArguments()[0]; } @Override public T create(T t) { […]

用spring批处理读取复杂的json文件

我有一个复杂的json文件(具有嵌套的json数组)结构,如下所示: {“persons”:[ {“id”:”1″, “firstName”: “X”, “lastName”: “X”, “infos”: [{“address”:[{“city”: “X”, “country”: “X”}]}]}, {“id”:”2″, “firstName”: “Y”, “lastName”: “Y”, “infos”: [{“address”:[{“city”: “Y”, “country”: “Y”}]}]} ]} 我想分别阅读每一行(一个人) 所以我的弹簧批量配置是这样的 mapper类看起来像 public class PersonLineMapper implements LineMapper { private JsonLineMapper delegate; public mapLine(String line, int lineNumber) throws Exception { Map personAsMap = delegate.mapLine(line, lineNumber); Person person = new Person(); // map […]

用Java处理数百万个数据库记录

我需要编写一个批处理作业,该作业从数据库表中提取行并基于某些条件,写入其他表或使用特定值更新此行。 我们使用spring和jdbc来获取结果集,并使用计划每周运行的独立java程序迭代并处理记录。 我知道这不是正确的方法,但我们必须将其作为临时解决方案。 随着记录增长到数百万,我们最终会出现内存exception,所以我知道这不是最好的方法。 你们中的任何人都可以推荐一种处理这种情况的最佳方法吗? 使用线程并为每个线程获取1000条记录并并行处理它们? (要么) 使用任何其他批处理机制来执行此操作(我知道有弹簧批但从未使用过此) (要么) 还有其他想法吗?

在spring哪里可以捕获非rest控制器exception?

我有spring mvc应用程序。 为了捕获exception,我使用了@ExceptionHandler注释。 @ControllerAdvise public class ExceptionHandlerController { @ExceptionHandler(CustomGenericException.class) public ModelAndView handleCustomException(CustomGenericException ex) { …. } } 但我认为在控制器方法调用之后我只会捕获exception。 但是如何捕获在其余环境之外生成的exception? 例如生命周期回调或计划任务。

以编程方式在Spring 3.1中加载属性

我试图以编程方式创建AnnotationConfigApplicationContext。 我得到一个配置类列表和一个属性文件列表,以便在Spring XML文件中使用它。 使用该文件,我可以使用XmlBeanDefinitionReader并加载所有@Configuration定义。 但是,我无法加载属性。 这是我正在做的加载属性.. PropertiesBeanDefinitionReader propReader = new PropertiesBeanDefinitionReader(ctx); for (String propFile : propertyFiles) { propReader.loadBeanDefinitions(new ClassPathResource(propFile)); } 代码只是运行完成没有任何问题,但一旦我调用ctx.refresh() – 它会抛出exception Caused by: java.lang.IllegalStateException: No bean class specified on bean definition at org.springframework.beans.factory.support.AbstractBeanDefinition.getBeanClass(AbstractBeanDefinition.java:381) at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:54) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:990) 所有类都在类路径上可用,如果我只是不以编程方式加载上述属性应用程序就好了(因为我使用其他方式加载属性)。 不确定,我在这里做错了什么。 有任何想法吗? 谢谢。

为什么在装饰器中调用安全认证属性`principal.displayName`会抛出exception?

是否有理由在装饰器中调用安全认证属性principal.displayName会导致问题? 我将它设置为sitemesh装饰器中的变量: 但是它会产生这个exception: java.lang.RuntimeException: javax.servlet.ServletException: javax.servlet.jsp.JspException: Invalid property ‘principal.displayName’ o f bean class [org.springframework.security.authentication.AnonymousAuthenticationToken]: Bean property ‘principal.displayName’ is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter? at com.opensymphony.sitemesh.webapp.decorator.BaseWebAppDecorator.render(BaseWebAppDecorator.java:39) at com.opensymphony.sitemesh.webapp.SiteMeshFilter.doFilter(SiteMeshFilter.java:84) at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157) at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:388) at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216) at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182) at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765) at […]

Jlastic的弹性搜索,Spring

有人能举例说明在jHipster或Spring-boot中使用Elasticsearch吗? 我已经使用jHipster生成了实体。 有占位符的输入:查询! /** * SEARCH /_search/samples/:query -> search for the sample corresponding * to the query. */ @RequestMapping(value = “/_search/samples/{query}”, method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) @Timed public List search(@PathVariable String query) { return StreamSupport .stream(sampleSearchRepository.search(queryString(query)).spliterator(), false) .collect(Collectors.toList()); } 如何使用elasticsearch? 这里有一些我在生成实体中找到的脚本! 我已经尝试过放置Object , q = field:value , 弹性格式的数组,并且总是得到空数组 。 抱歉英语不好!

关于将spring bean注入jsf bean的问题

将spring bean注入jsf bean时遇到问题,导致javax.faces.FacesException(无法实例化类),javax.faces.el.E​​valuationException 这是我的xml文件: 文件WEB-INF / components.xml定义了spring bean 文件WEB-INF / faces-config.xml定义了jsf bean Video Home Bean videoBean rambo.tool.VideoBean session videoService videoService #{Components[“rambo.api.VideoService”]} 文件videoMain.jsp: 在resourceToolExists中:videoBean使用videoService的一些方法 得到:javax.faces.FacesException(无法实例化类rambo.impl.VideoServiceImpl),javax.faces.el.E​​valuationException

Hibernate向数据库发送多余的查询

我有一个奇怪的问题,Hibernate运行的查询比我要求的更多,并且看不到需要。 这是我的控制器: @Autowired UserService users; @RequestMapping(“/test”) @ResponseBody public String test() { User user = users.findUser(1L); return “Found user: “+user.getEmail(); } 这是UserService : @Component public class UserService { @javax.persistence.PersistenceUnit private EntityManagerFactory emf; private JpaTemplate getJpaTemplate() { return new JpaTemplate(emf); } public User findUser(long id) { long start = System.currentTimeMillis(); JpaTemplate jpaTemplate = getJpaTemplate(); User user = […]