小编Ken*_*han的帖子

通过在eclipse中设置断点来调试ServletContextListener.contextDestroyed()

我实现了一个自定义ServletContextListener并将断点放在contextInitialized()contextDestroyed().

使用WTP启动Web应用程序时,contextInitialized()可以通过调试器停止.但是当我关闭Web应用程序时,断点contextDestroyed()不起作用,Web应用程序就会关闭.是否有可能以及如何contextDestroyed()在eclipse中使用调试器进行调试?

我想检查在关闭Web应用程序期间是否可以正确释放所使用的资源.

java eclipse debugging servlets servlet-listeners

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

Spring 如何从返回 CompletableFuture 对象的端点获取结果?

在下面的代码中,当端点getPerson被命中时,响应将是一个 Person 类型的 JSON。Spring 如何转换CompletableFuture<Person>Person

@RestController
public class PersonController {

    @Autowired
    private PersonService personService;


    @GetMapping("/persons/{personId}" )
    public CompletableFuture<Person> getPerson(@PathVariable("personId") Integer personId) {

        return CompletableFuture.supplyAsync(() -> personService.getPerson(personId));
    }
}
Run Code Online (Sandbox Code Playgroud)

java spring spring-mvc java-8 completable-future

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

使用<f:convertDateTime>格式化日期并将其显示在<h:outputText>中

我想在h:outputText中显示类似"2010-10-20 by Mary"的内容.日期值存储在名为date1的MBean字段中,而用户名存储在名为username的MBean字段中.我使用以下EL表达式和UI控件:

<h:outputText value="#{MBean.date1} by #{MBean.username}">
   <f:convertDateTime pattern="YYYY-MM-DD" timeZone="#{configMB.timeZone}" />
</h:inputText>
Run Code Online (Sandbox Code Playgroud)

可以显示该值.但是,它忽略f:convertDateTime指定的日期格式.无论我如何更改数据格式,它总是显示类似" 2010-06-08 12:35:22.0 by Mary".我怎么解决这个问题??

更新:Zenzen的解决方案适用于以下代码更改.

<h:outputFormat value="{0, date, yyyy-MM-dd} by #{1}">
    <f:param value="#{MBean.date1}" />
    <f:param value="#{MBean.username}" />
</h:outputFormat>
Run Code Online (Sandbox Code Playgroud)

不过我可以格式化值的只读h:inputText使用喜欢的方法 h:outputFormat<f:param>?有时显示的值太长而且使用<h:outputFormat>会生成包含格式化消息的span标记.我希望有一个类似的效果<input type="text">,UI控件具有固定长度,如果消息太长,用户可以滚动查看消息.或者,我如何格式化span标记,使行为看起来像<input type="text">使用css或javascript?

jsf

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

如何使用Spring Data正确处理JPA实体中的version字段

我有一个非常简单的域模型,一个实体具有一个version字段,以便使用JPA(api v2.2)提供的乐观锁定功能。我使用的实现是Hibernate v5.3.10.Final。

@Entity
@Data
public class Activity {

    @Id
    @SequenceGenerator(name = "gen", sequenceName = "gen_seq", allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "gen")
    private Long id;

    @Column
    private String state;

    @Version
    private int version;
}
Run Code Online (Sandbox Code Playgroud)

然后对该实体进行简单的操作,例如临时更改其阶段:

@Transactional
public Activity startProgress(Long id) {
    var entity = activityRepository.findById(id).orElseThrow(RuntimeException::new);

    if (entity.getState() == "this") { // or that, or else, etc
        // throw some exceptions in some cases
    }

    entity.setState("IN_PROGRESS");

    return activityRepository.saveAndFlush(entity);
}
Run Code Online (Sandbox Code Playgroud)

我想要实现的结果是,有一种更新实体的方法,并且该更新还应该增加版本。如果存在不匹配,我希望会抛出a ObjectOptimisticLockingFailureExceptionOptimisticLockingException。我还希望version在对象中具有该字段的更新值,因为我要将其返回给客户端。

我尝试过的几种选择:

  • 只需调用 …

hibernate jpa spring-data spring-data-jpa

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

Hibernate缓存与Spring缓存

谁能解释一下hibernate二级缓存和spring缓存有什么区别?

在单个应用程序中使用两者有意义吗?如果不推荐那么什么时候使用哪一个?

如果有人给出基于现实生活场景的解释,将非常有助于轻松理解。

spring hibernate hibernate-cache spring-boot spring-cache

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

Spring Boot webflux 应用程序中基于文件(或基于包)的 RouterFunction

我们在内部公司框架中想要的东西之一是 Spring Boot 应用程序中基于文件路径或包的路由。幸运的是,在 java 中,文件和包是相同的......所以两者都可以。

例如,我想强制org.company.api从其包名称中选取下面的所有包。org.company.api.user所以will be/userorg.company.api.user.loginwill be中的控制器/user/login。注意:记住org.company因项目而异。基本上我们希望 api 目录(相当于 api 子包)下的所有内容都用于此目的。

我想我可以使用 webflux 路由器功能来实现这一点,但我不确定是否有更好的方法。如何为目录下的所有文件生成路由器功能?

java spring spring-mvc spring-boot spring-webflux

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

COMMIT和ROLLBACK的时间会影响性能吗?

假设我有一组ID.对于每个ID,我将根据ID在许多不同的表中插入许多记录.在插入差异表之间,将调用不同的业务检查.如果任何检查失败,则基于此ID插入的所有记录都将是ROLLBACK.此批量插入操作是通过使用PL/SQL完成的.COMMIT和ROLLBACK的时间是否影响性能以及它如何影响?例如,在完成所有ID后,我应该在完成一个ID或COMMIT的进程后进行COMMIT吗?

oracle

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

找出2个日期之间的月数

select 
  (age('2012-11-30 00:00:00'::timestamp, '2012-10-31 00:00:00'::timestamp)),
  (age('2012-12-31 00:00:00'::timestamp, '2012-10-31 00:00:00'::timestamp)),
  (age('2013-01-31 00:00:00'::timestamp, '2012-10-31 00:00:00'::timestamp)),
  (age('2013-02-28 00:00:00'::timestamp, '2012-10-31 00:00:00'::timestamp))
Run Code Online (Sandbox Code Playgroud)

它给出了以下内容:

  0 years 0 mons 30 days 0 hours 0 mins 0.00 secs
  0 years 2 mons 0 days 0 hours 0 mins 0.00 secs
  0 years 3 mons 0 days 0 hours 0 mins 0.00 secs
  0 years 3 mons 28 days 0 hours 0 mins 0.00 secs
Run Code Online (Sandbox Code Playgroud)

但我希望有下个月的定义,我该怎么做?

  0 years 1 mons 0 days 0 hours 0 mins 0.00 secs
  0 …
Run Code Online (Sandbox Code Playgroud)

sql postgresql

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

http.antMatcher("/**") .authorizeRequests().antMatchers("/") 中的 antMatcher("/**") 需要什么?

我正在学习 spring 安全,我从https://spring.io/guides/tutorials/spring-boot-oauth2/ 中发现了这段代码

 @Override
  protected void configure(HttpSecurity http) throws Exception {
    http
      .antMatcher("/**")
      .authorizeRequests()
        .antMatchers("/", "/login**", "/webjars/**", "/error**")
        .permitAll()
      .anyRequest()
        .authenticated();
  }
Run Code Online (Sandbox Code Playgroud)

我删除.antMatcher("/**")了,代码仍然有效。我知道**匹配路径中的零个或多个目录,所以我认为antMatcher("/**").authorizeRequestes().antMatcher("/login")"/login"直接或间接匹配根路径下的目录,即我希望它匹配路径/login/demo/login但事实并非如此,它只匹配/login根路径正下方的目录。那么究竟需要.antMatcher("/**") here什么呢?

java spring spring-security spring-boot

3
推荐指数
2
解决办法
6395
查看次数

@WebMvcTest需要@Import(SecurityConfig.class)来启用SpringSecurity

首先,重要的是要指定应用程序已经部署并且我们的 spring 安全配置有效。如果未经身份验证的用户尝试访问 API 的任何端点,则会返回 401 Unauthorized。

@WebMvcTest接下来,在这个示例示例中,我想使用和 来测试控制器spring security

@WebMvcTest(EmployeeController.class)   
@Import(SecurityConfig.class)
class EmployeeControllerTest {

    @Autowired
    private WebApplicationContext ctx;

    protected MockMvc mvc;

    @MockBean
    private EmployeeService service;

    @BeforeEach
    public void setUp() {
        this.mvc = MockMvcBuilders
                .webAppContextSetup(ctx)
                .apply(springSecurity())
                .build();
    }

    @Test
    void unauthorized_role_should_return_401() {

       mvc.perform(get("/employees/1").accept(MediaType.APPLICATION_JSON)
            .with(SecurityMockMvcRequestPostProcessors.user("manager").roles("UNKNOWN")))
            .andExpect(status().isUnauthorized())
    }
}
Run Code Online (Sandbox Code Playgroud)

这段代码有效,但我不明白为什么需要将该SecurityConfig类导入到测试类中。事实上,如果我删除@Import,mockMvc返回 200。但是,我发现的每个示例项目都Github只是使用@WebMvcTest,即使该项目有一个SecurityConfig

spring-security spring-boot mockmvc spring-boot-test

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

spring中ThreadPoolTask​​Executor默认的池类型是哪种

我读过代码但没有找到ThreadPoolTask​​Executor的默认池类型。ThreadPoolTask​​Executor的默认线程池是什么?newFixedThreadPool 还是 newCachedThreadPool?

java spring spring-async

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

在使用maven创建spring,hibernate时,在pom.xml中添加依赖项时发现了多个注释错误

我正在创建一个带有maven for spring和hibernate的示例项目.我对这个发展很新.放置依赖之后如

<dependencies>
    <dependency>
        <groupid>org.hibernate</groupid>
        <artifactid>hibernate-core</artifactid>
        <version>3.3.1.GA</version>
    </dependency>

    <dependency>
        <groupid>org.springframework</groupid>
        <artifactid>spring</artifactid>
        <version>${springVersion}</version>
    </dependency>
Run Code Online (Sandbox Code Playgroud)

在eclipse中,它给出了一个错误,因为在这一行找到了多个注释:

- Project build error: 'dependencies.dependency.groupId' for null:null:jar is 
 missing.
- Project build error: 'dependencies.dependency.artifactId' for null:null:jar is 
 missing.
Run Code Online (Sandbox Code Playgroud)

我猜,spring和hibernate的jar文件都丢失了.有人可以告诉我如何解决这个问题.

spring hibernate maven

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

春季安全工作仅适用于最高优先级的订单

我正在尝试将 spring mvc 集成到现有的 spring rest 项目中。春季休息的安全性工作正常。当我尝试以最低优先级顺序为 spring mvc 实现安全性时,它仅适用于 rest api。如果我为 spring mvc 设置了高优先级,那么它将适用于 spring mvc,但对于 rest api,它将重定向到登录页面。

这是我的代码片段

//base class for spring security config
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig 
Run Code Online (Sandbox Code Playgroud)

我有两个静态类

用于弹簧 mvc

@Configuration
@EnableWebSecurity
@Order(1)
public static class SecurityConfig extends WebSecurityConfigurerAdapter
Run Code Online (Sandbox Code Playgroud)

用于休息 api

@Configuration
@EnableWebSecurity
@Order(2)
public static class ApiSecurity extends WebSecurityConfigurerAdapter
Run Code Online (Sandbox Code Playgroud)

对于 spring mvc 配置

@Override
protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                .antMatchers("/admin/**").hasRole("ADMIN")
                .antMatchers("/resources/**").permitAll()
                .anyRequest().authenticated()
                .and()
                .formLogin()
                .loginPage("/admin/login")
                .defaultSuccessUrl("/admin/home",true)

                .permitAll()
                .and()
                .logout() …
Run Code Online (Sandbox Code Playgroud)

java spring spring-security spring-boot

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