This is an automated email from the ASF dual-hosted git repository. zhfeng pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push: new 2be29053816 CAMEL-21991: camel-jpa - Make entityManagerFactory as an autowired option (#17801) 2be29053816 is described below commit 2be290538161f3819a3e3a7aa7a0a815f7b62e03 Author: Zheng Feng <zh.f...@gmail.com> AuthorDate: Fri Apr 18 18:47:17 2025 +0800 CAMEL-21991: camel-jpa - Make entityManagerFactory as an autowired option (#17801) --- .../org/apache/camel/catalog/components/jpa.json | 2 +- .../component/jpa/JpaComponentConfigurer.java | 5 +++++ .../org/apache/camel/component/jpa/jpa.json | 2 +- .../apache/camel/component/jpa/JpaComponent.java | 23 +--------------------- 4 files changed, 8 insertions(+), 24 deletions(-) diff --git a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/jpa.json b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/jpa.json index 1179d721aff..cfe4790b90f 100644 --- a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/jpa.json +++ b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/jpa.json @@ -25,7 +25,7 @@ }, "componentProperties": { "aliases": { "index": 0, "kind": "property", "displayName": "Aliases", "group": "common", "label": "", "required": false, "type": "object", "javaType": "java.util.Map<java.lang.String, java.lang.Class<java.lang.Object>>", "deprecated": false, "autowired": false, "secret": false, "description": "Maps an alias to a JPA entity class. The alias can then be used in the endpoint URI (instead of the fully qualified class name)." }, - "entityManagerFactory": { "index": 1, "kind": "property", "displayName": "Entity Manager Factory", "group": "common", "label": "", "required": false, "type": "object", "javaType": "jakarta.persistence.EntityManagerFactory", "deprecated": false, "autowired": false, "secret": false, "description": "To use the EntityManagerFactory. This is strongly recommended to configure." }, + "entityManagerFactory": { "index": 1, "kind": "property", "displayName": "Entity Manager Factory", "group": "common", "label": "", "required": false, "type": "object", "javaType": "jakarta.persistence.EntityManagerFactory", "deprecated": false, "autowired": true, "secret": false, "description": "To use the EntityManagerFactory. This is strongly recommended to configure." }, "joinTransaction": { "index": 2, "kind": "property", "displayName": "Join Transaction", "group": "common", "label": "", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": true, "description": "The camel-jpa component will join transaction by default. You can use this option to turn this off, for example if you use LOCAL_RESOURCE and join transaction doesn't work with your JPA provider. This option can [...] "sharedEntityManager": { "index": 3, "kind": "property", "displayName": "Shared Entity Manager", "group": "common", "label": "", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": false, "description": "Whether to use Spring's SharedEntityManager for the consumer\/producer. Note in most cases joinTransaction should be set to false as this is not an EXTENDED EntityManager." }, "transactionStrategy": { "index": 4, "kind": "property", "displayName": "Transaction Strategy", "group": "common", "label": "", "required": false, "type": "object", "javaType": "org.apache.camel.component.jpa.TransactionStrategy", "deprecated": false, "autowired": false, "secret": false, "description": "To use the TransactionStrategy for running the operations in a transaction." }, diff --git a/components/camel-jpa/src/generated/java/org/apache/camel/component/jpa/JpaComponentConfigurer.java b/components/camel-jpa/src/generated/java/org/apache/camel/component/jpa/JpaComponentConfigurer.java index cb8f9e0fc41..93a566b2154 100644 --- a/components/camel-jpa/src/generated/java/org/apache/camel/component/jpa/JpaComponentConfigurer.java +++ b/components/camel-jpa/src/generated/java/org/apache/camel/component/jpa/JpaComponentConfigurer.java @@ -46,6 +46,11 @@ public class JpaComponentConfigurer extends PropertyConfigurerSupport implements } } + @Override + public String[] getAutowiredNames() { + return new String[]{"entityManagerFactory"}; + } + @Override public Class<?> getOptionType(String name, boolean ignoreCase) { switch (ignoreCase ? name.toLowerCase() : name) { diff --git a/components/camel-jpa/src/generated/resources/META-INF/org/apache/camel/component/jpa/jpa.json b/components/camel-jpa/src/generated/resources/META-INF/org/apache/camel/component/jpa/jpa.json index 1179d721aff..cfe4790b90f 100644 --- a/components/camel-jpa/src/generated/resources/META-INF/org/apache/camel/component/jpa/jpa.json +++ b/components/camel-jpa/src/generated/resources/META-INF/org/apache/camel/component/jpa/jpa.json @@ -25,7 +25,7 @@ }, "componentProperties": { "aliases": { "index": 0, "kind": "property", "displayName": "Aliases", "group": "common", "label": "", "required": false, "type": "object", "javaType": "java.util.Map<java.lang.String, java.lang.Class<java.lang.Object>>", "deprecated": false, "autowired": false, "secret": false, "description": "Maps an alias to a JPA entity class. The alias can then be used in the endpoint URI (instead of the fully qualified class name)." }, - "entityManagerFactory": { "index": 1, "kind": "property", "displayName": "Entity Manager Factory", "group": "common", "label": "", "required": false, "type": "object", "javaType": "jakarta.persistence.EntityManagerFactory", "deprecated": false, "autowired": false, "secret": false, "description": "To use the EntityManagerFactory. This is strongly recommended to configure." }, + "entityManagerFactory": { "index": 1, "kind": "property", "displayName": "Entity Manager Factory", "group": "common", "label": "", "required": false, "type": "object", "javaType": "jakarta.persistence.EntityManagerFactory", "deprecated": false, "autowired": true, "secret": false, "description": "To use the EntityManagerFactory. This is strongly recommended to configure." }, "joinTransaction": { "index": 2, "kind": "property", "displayName": "Join Transaction", "group": "common", "label": "", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": true, "description": "The camel-jpa component will join transaction by default. You can use this option to turn this off, for example if you use LOCAL_RESOURCE and join transaction doesn't work with your JPA provider. This option can [...] "sharedEntityManager": { "index": 3, "kind": "property", "displayName": "Shared Entity Manager", "group": "common", "label": "", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": false, "description": "Whether to use Spring's SharedEntityManager for the consumer\/producer. Note in most cases joinTransaction should be set to false as this is not an EXTENDED EntityManager." }, "transactionStrategy": { "index": 4, "kind": "property", "displayName": "Transaction Strategy", "group": "common", "label": "", "required": false, "type": "object", "javaType": "org.apache.camel.component.jpa.TransactionStrategy", "deprecated": false, "autowired": false, "secret": false, "description": "To use the TransactionStrategy for running the operations in a transaction." }, diff --git a/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaComponent.java b/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaComponent.java index 284808c7129..ffc8800abed 100644 --- a/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaComponent.java +++ b/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaComponent.java @@ -41,7 +41,7 @@ public class JpaComponent extends HealthCheckComponent { private ExecutorService pollingConsumerExecutorService; - @Metadata + @Metadata(autowired = true) private EntityManagerFactory entityManagerFactory; @Metadata private TransactionStrategy transactionStrategy; @@ -166,30 +166,9 @@ public class JpaComponent extends HealthCheckComponent { return endpoint; } - private void initEntityManagerFactory() { - // lookup entity manager factory and use it if only one provided - if (entityManagerFactory == null) { - Map<String, EntityManagerFactory> map - = getCamelContext().getRegistry().findByTypeWithName(EntityManagerFactory.class); - if (map != null) { - if (map.size() == 1) { - entityManagerFactory = map.values().iterator().next(); - LOG.info("Using EntityManagerFactory found in registry with id [{}] {}", - map.keySet().iterator().next(), entityManagerFactory); - } else { - LOG.debug("Could not find a single EntityManagerFactory in registry as there was {} instances.", - map.size()); - } - } - } else { - LOG.info("Using EntityManagerFactory configured: {}", entityManagerFactory); - } - } - @Override protected void doInit() throws Exception { super.doInit(); - initEntityManagerFactory(); // warn about missing configuration if (entityManagerFactory == null) {