我实现了一个自定义ServletContextListener并将断点放在contextInitialized()和contextDestroyed().
使用WTP启动Web应用程序时,contextInitialized()可以通过调试器停止.但是当我关闭Web应用程序时,断点contextDestroyed()不起作用,Web应用程序就会关闭.是否有可能以及如何contextDestroyed()在eclipse中使用调试器进行调试?
我想检查在关闭Web应用程序期间是否可以正确释放所使用的资源.
在下面的代码中,当端点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) 我想在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?
我有一个非常简单的域模型,一个实体具有一个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 ObjectOptimisticLockingFailureException或OptimisticLockingException。我还希望version在对象中具有该字段的更新值,因为我要将其返回给客户端。
我尝试过的几种选择:
谁能解释一下hibernate二级缓存和spring缓存有什么区别?
在单个应用程序中使用两者有意义吗?如果不推荐那么什么时候使用哪一个?
如果有人给出基于现实生活场景的解释,将非常有助于轻松理解。
我们在内部公司框架中想要的东西之一是 Spring Boot 应用程序中基于文件路径或包的路由。幸运的是,在 java 中,文件和包是相同的......所以两者都可以。
例如,我想强制org.company.api从其包名称中选取下面的所有包。org.company.api.user所以will be/user和org.company.api.user.loginwill be中的控制器/user/login。注意:记住org.company因项目而异。基本上我们希望 api 目录(相当于 api 子包)下的所有内容都用于此目的。
我想我可以使用 webflux 路由器功能来实现这一点,但我不确定是否有更好的方法。如何为目录下的所有文件生成路由器功能?
假设我有一组ID.对于每个ID,我将根据ID在许多不同的表中插入许多记录.在插入差异表之间,将调用不同的业务检查.如果任何检查失败,则基于此ID插入的所有记录都将是ROLLBACK.此批量插入操作是通过使用PL/SQL完成的.COMMIT和ROLLBACK的时间是否影响性能以及它如何影响?例如,在完成所有ID后,我应该在完成一个ID或COMMIT的进程后进行COMMIT吗?
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) 我正在学习 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什么呢?
首先,重要的是要指定应用程序已经部署并且我们的 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类
我读过代码但没有找到ThreadPoolTaskExecutor的默认池类型。ThreadPoolTaskExecutor的默认线程池是什么?newFixedThreadPool 还是 newCachedThreadPool?
我正在创建一个带有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 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) spring ×7
java ×6
spring-boot ×5
hibernate ×3
spring-mvc ×2
debugging ×1
eclipse ×1
java-8 ×1
jpa ×1
jsf ×1
maven ×1
mockmvc ×1
oracle ×1
postgresql ×1
servlets ×1
spring-async ×1
spring-cache ×1
spring-data ×1
sql ×1