似乎 ConditionalOnProperty 仅适用于类路径中的属性,如资源文件夹中的 application.properties。我需要一个最终用户可以通过外部属性打开和关闭的属性。一个例子非常简单:
配置类,读取外部属性。Sys.out 显示它正在正确读取文件。
@Configuration
@EnableAutoConfiguration
@PropertySource("file:/Users/end.user/MyApp/config/MyApp.properties")
public class PropertyConfigurer {
@Value("${featureOne}")
private String featureOne;
@PostConstruct
public void init() {
System.out.println("FeatureOne : " + featureOne);
}
}
Run Code Online (Sandbox Code Playgroud)
要素类,如果通过 ConditionalOnProperty 启用该属性,则该组件类将被放入应用程序上下文中以便能够使用,否则永远不会实例化该组件。
@Component
@ConditionalOnProperty(name="featureOne", havingValue = "true")
public class FeatureOne {
@PostConstruct
public void init() {
System.out.println("Feature initialized");
}
}
Run Code Online (Sandbox Code Playgroud)
正如您可以想象的那样,由于“featureOne”属性在构造此类之后才可用于 spring 上下文,因此我永远不会看到“功能已初始化”。如果有某种方法可以在类实例化时强制来自 @PropertySource 的属性可用于 spring 上下文。还是有其他方式?我也尝试过 @DependsOn 来自 FeatureOne 的 PropertyConfigurer,但有趣的是,这也不起作用。
假设您正在创建类型为 的新实体,假设您知道ID 为 1 的a存在,那么UserUser 具有嵌套对象,是否有一种简单的方法可以在 new和 existing之间形成关联?BillingBillingUserBilling
假设获取一个Billing对象设置给用户是一个昂贵的操作,因此获取整个 Billing 对象并将其设置给用户的解决方案不是一个选项。
我的问题是,是否有使用 spring 数据保存实体与其嵌套对应物之间的这种关系的快捷方法?
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int userId;
private String name;
@ManyToOne
@JoinColumn(name = "billing_id")
private Billing userBill;
// constructor
// getters and setters
}
Run Code Online (Sandbox Code Playgroud)
例如在 sudo 代码中:
User bob = new User();
bob.billingId.id = 1;
userRepository.save(bob);
Run Code Online (Sandbox Code Playgroud) 我正在为我的控制器类编写单元测试。我正在使用 spring webflux。因此我正在使用 编写测试WebTestClient。这是我的控制器方法
@PutMapping("/updatedocument/{documentType}")
public ResponseEntity<String> updateDocument(@PathVariable String documentType,
@RequestParam("file") MultipartFile file) {
...................
}
Run Code Online (Sandbox Code Playgroud)
当我从 Postman 或任何其他客户端调用时,此代码有效。我在编写单元测试时遇到困难。我正进入(状态
“所需的 MultipartFile 参数‘文件’不存在”
错误 。这是我的测试方法。
@Test
void updateDocument() throws IOException {
.............
MultipartBodyBuilder multipartBodyBuilder = new MultipartBodyBuilder();
multipartBodyBuilder.part("file", new ClassPathResource("somefile"))
.contentType(MediaType.MULTIPART_FORM_DATA)
webTestClient.put()
.uri("/customer/updatedocument/ID")
.body(BodyInserters.fromMultipartData(multipartBodyBuilder.build()))
.exchange()
.expectStatus().isOk();
}
Run Code Online (Sandbox Code Playgroud)
任何建议都非常感激。请注意。我正在使用WebTestClient而不是MovkMvc
我对 Python 还很陌生。我正在尝试使用threading模块。我遇到了Event物体。
事件对象具有
wait
set
clear
函数。我明白 wait、set 和clear 正在做什么。但我不太明白为什么有一个单独的明确方法。难道不能将其包含在 set 方法实现中吗?
到目前为止我看到的大多数代码都是set方法后面跟着一个clear方法。以便后续的wait方法调用wait。
我来自 Java 世界,我忍不住将其与 wait 和 notification 方法调用进行比较。需要明确的是,Java 中没有等效的方法调用。
是否存在使用 set 但不明确方法的用例?
我知道这可能与此重复.
何时使用@RestController vs @RepositoryRestResource
但是我有一些在这个问题上没有解决的问题.
使用时@RepositoryRestResource,默认情况下会暴露每个方法.我觉得有点烦人.如果我在这里错了,请纠正我.例如,在下面的例子中
@RepositoryRestResource
public interface ProductRepository extends MongoRepository<Product, String> {}
Run Code Online (Sandbox Code Playgroud)如果我只想暴露findAll()和findOne()而不是任何其他方法,尤其是删除.要做到这一点,我需要做这样的事情
@RepositoryRestResource
public interface ProductRepository extends MongoRepository<Product, String> {
@RestResource(exported = false)
@Override
default void delete(String s) {
}
@RestResource(exported = false)
@Override
default void delete(Product product) {
}
@RestResource(exported = false)
@Override
default void delete(Iterable<? extends Product> iterable) {
}
@RestResource(exported = false)
@Override
default void deleteAll() {
}
}
Run Code Online (Sandbox Code Playgroud)
我觉得这真的是很多不需要的代码.这与Rest Controller方法相比要好得多
我相信最好使用ResponseEntity从REST端点返回任何值.但是使用spring-data-rest方法,我不知道该怎么做.
我找不到任何通过RepositoryRestResource公开的单元测试(非IT)REST端点的方法.但是使用REST控制器方法,我可以使用MockServletContext,测试我的REST端点MockMvc,MockMvcBuilders
鉴于所有这些,使用sping-data-rest(HATEOS除外)是否仍然有利?
请澄清
我正在尝试运行Spring-Boot应用程序,但是在将应用程序作为Java应用程序运行时却出现以下错误
org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is java.lang.NoSuchMethodError: org.apache.catalina.Context.addServletMapping(Ljava/lang/String;Ljava/lang/String;)V
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:137) ~[spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:537) ~[spring-context-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693) [spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360) [spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) [spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118) [spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107) [spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
at main.java.start.WebApplication.main(WebApplication.java:21) [classes/:na]
Caused by: java.lang.NoSuchMethodError: org.apache.catalina.Context.addServletMapping(Ljava/lang/String;Ljava/lang/String;)V
at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory.addServletMapping(TomcatEmbeddedServletContainerFactory.java:290) ~[spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory.addDefaultServlet(TomcatEmbeddedServletContainerFactory.java:270) ~[spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory.prepareContext(TomcatEmbeddedServletContainerFactory.java:215) ~[spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory.getEmbeddedServletContainer(TomcatEmbeddedServletContainerFactory.java:178) ~[spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.createEmbeddedServletContainer(EmbeddedWebApplicationContext.java:164) ~[spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:134) ~[spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
... 8 common frames omitted
Run Code Online (Sandbox Code Playgroud)
我的POM文件如下
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>Web.Final.Project</groupId>
<artifactId>Web.Final.Project</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>fitness365-group-project</name>
<description>Fitness 365 …Run Code Online (Sandbox Code Playgroud) 在 Spring Java 项目中,我有以下类:
@SuppressWarnings({"PMD", "Checkstyle"})
@SpringBootApplication
public class ToolBoxApplication {
public static void main(final String[] args) {
SpringApplication.run(ToolBoxApplication.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)
使用 Jenkins 构建告诉我,我不应该在实用程序类中拥有公共或默认构造函数。
在我的 checkstyle.xml 和 Treewalker 文件中,我有
<!-- Make the @SuppressWarnings annotations available to Checkstyle -->
<module name="SuppressWarningsHolder" />
Run Code Online (Sandbox Code Playgroud)
和模块
我试图使用抑制特定检查
@SuppressWarnings({"PMD", "checkstyle:HideUtilityClassConstructor"})
但这也不起作用。“PMD”抑制确实有效(它有效地报告了相同的错误)。
需要获取要在数据库上删除的 id 但我无法通过这种方式获取 id 参数
@RequestMapping(value = {"/delete/search/","/delete/search"}, method = RequestMethod.DELETE)
@ResponseBody
public Integer deleteUser(@RequestBody Integer id_search) {
return id_search;
}
Run Code Online (Sandbox Code Playgroud)
我收到此错误消息
"message": "JSON parse error: Can not deserialize instance of java.lang.Integer out of START_OBJECT token; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.Integer out of START_OBJECT token\n at [Source: java.io.PushbackInputStream@1b77938; line: 1, column
Run Code Online (Sandbox Code Playgroud) 在我的项目中,我们使用的是 Spring 注入的 MongoTemplate
private final MongoTemplate mongoTemplate;
Run Code Online (Sandbox Code Playgroud)
我知道 mongo 模板实现了 MongoOperations、ApplicationContextAware,所以我们通过模板获得了上下文感知方法,如果我使用如下所示的 mongo 操作类型对象,我们就不会得到这种方法(spring 将在其中注入 mongo 模板对象)
private final MongoOperations mongoOperations;
Run Code Online (Sandbox Code Playgroud)
我的疑问是 1. 我们这样做是否违反了“编程接口”范式 2. 我们应该使用哪种模式,为什么?
我对这三个概念感到困惑。
根据我的阅读,使用QueryDsl或JPA元模型的主要好处之一是类型安全。
但是,即使使用Criteria API,我也可以实现类型安全。(我在eclipselink中使用JPA)
javax.persistence.EntityManager 有两种变体
public Query createQuery(String sqlString);
public <T> TypedQuery<T> createQuery(CriteriaQuery<T> criteriaQuery);
Run Code Online (Sandbox Code Playgroud)
我同意第一个版本,在该版本中我将sql作为字符串传递,但我没有类型安全性。但是有了第二个版本,我得到了类型安全性。还是我在这里想念什么?有人可以举例说明如何使用标准不安全。
QueryDsl和JPA静态元模型有什么区别?
java ×7
spring ×5
spring-boot ×3
criteria ×1
jpa ×1
jpa-2.0 ×1
maven ×1
pom.xml ×1
properties ×1
python ×1
python-3.x ×1
querydsl ×1
rest ×1
spring-data ×1
type-safety ×1
unit-testing ×1