到 2018 年 12 月,我一直在 WildFly (15.x -> 20.x) 上成功使用 Kotlin (1.4.10) 和 JSF (XHTML)。使用 Java EE (8) 和 JSF。
我现在正在使用 WildFly (32.0.0.Final) 上的简单 Jakarta Faces (4.0) 和 Mojarra 上的“standalone.xml”来迁移现有的工作 Jakarta (Java) EE (10.0.0 和 11.0.0-M2) ( 4.0.7) 到 Kotlin (1.9.23 和 2.0.0.RC1)。
XHTML:
<f:view>
<h:body>
<p:dataTable value="#{usersKotlinController.allUsers}"
lazy="true"
style="width:100%"
var="usersKotlin"
rowKey="#{usersKotlin.id}"
border="1"
headerText="Kotlin Users"
scrollable="false">
<p:column width="100%" visible="true">
<f:facet name="header">
<h:outputText value="Kotlin Users"/>
</f:facet>
<h:outputText value="#{usersKotlin.mobile}">
</h:outputText>
</p:column>
</p:dataTable>
</h:body>
</f:view>
kt控制器:
@Named("usersKotlinController")
open class UsersKotlinController : Serializable {
@Inject
private lateinit var usersKotlinEJB: UsersKotlinEJB
fun getAllUsers(): List<UsersKotlin> {
println("***** 1.1 fun getAllUsers(): List<UsersKotlin")
val usersKotlinList: List<UsersKotlin> = usersKotlinEJB.getAllUsers()
println("***** 1.2 UserKotlinEJB usersKotlinList = " + usersKotlinList)
println("***** 1.3 UserKotlinEJB usersKotlinList size = " + usersKotlinList.size)
return usersKotlinList
}
companion object {
/**
*
*/
private const val serialVersionUID = 1L
/**
*
*/
private val logger = KotlinLogging.logger {}
}
}
kt EJB:
@Stateless
open class UsersKotlinEJB {
val datastoreBeWhereWhen: Datastore = Morphia.createDatastore(MongoClients.create(), "beWhereWhenDB")
open fun getAllUsers(): List<com.bewherewhen.kotlin.entity.morphia.users.UsersKotlin> {
val usersQuery = datastoreBeWhereWhen.find(com.bewherewhen.kotlin.entity.morphia.users.UsersKotlin::class.java)
println("***** 2.1 UserKotlinEJB usersQuery = " + usersQuery)
val usersList: List<com.bewherewhen.kotlin.entity.morphia.users.UsersKotlin> = usersQuery.iterator().toList()
println("***** 2.2 UserKotlinEJB usersList size = " + usersList.size)
return usersList
}
}
kt 实体:
@Entity
data class UsersKotlin(
@Id
val id: ObjectId?,
var mobile: String? = null,
var email: String? = null,
var salutation: String? = null,
var title: String? = null,
var forename: String? = null,
var surname: String? = null,
var house_name_number: String? = null,
var post_code_zip: String? = null,
var dateStart: Date? = null,
@Reference
var myEventDateKotlins: List<MyEventDatesKotlin> // etc ...
)
在 WildFly 上启动并“运行”:
INFO [org.jboss.as.ejb3.deployment] (MSC service thread 1-2) WFLYEJB0473: JNDI bindings for session bean named 'UsersKotlinEJB' in deployment unit 'subdeployment "BeWhereWhenJAR.jar" of deployment "BeWhereWhen.ear"' are as follows:
java:global/BeWhereWhen/BeWhereWhenJAR/UsersKotlinEJB!com.bewherewhen.kotlin.ejb.UsersKotlinEJB
java:app/BeWhereWhenJAR/UsersKotlinEJB!com.bewherewhen.kotlin.ejb.UsersKotlinEJB
java:module/UsersKotlinEJB!com.bewherewhen.kotlin.ejb.UsersKotlinEJB
java:global/BeWhereWhen/BeWhereWhenJAR/UsersKotlinEJB
java:app/BeWhereWhenJAR/UsersKotlinEJB
java:module/UsersKotlinEJB
INFO [jakarta.enterprise.resource.webcontainer.faces.config] (ServerService Thread Pool -- 89) Initializing Mojarra 4.0.6 for context '/BeWhereWhen'
INFO [jakarta.enterprise.resource.webcontainer.faces.config] (ServerService Thread Pool -- 89) Monitoring file:/usr/local/Cellar/wildfly-as/wildfly-32.0.0.Beta1/standalone/tmp/vfs/deployment/deployment8f76f3833e8a1aba/BeWhereWhenWAR.war-b52a211bf32ce343/WEB-INF/faces-config.xml for modifications
INFO [org.primefaces.webapp.PostConstructApplicationEventListener] (ServerService Thread Pool -- 89) Running on PrimeFaces 13.0.8
INFO [org.wildfly.extension.undertow] (ServerService Thread Pool -- 89) WFLYUT0021: Registered web context: '/BeWhereWhen' for server 'default-server'
INFO [org.jboss.as.server] (External Management Request Threads -- 1) WFLYSRV0010: Deployed "BeWhereWhen.ear" (runtime-name : "BeWhereWhen.ear")
当我运行“localhost:8080/BeWhereWhen”时,它会显示该页面,但我的 Kotlin“控制器”上不会显示任何日志。确实如此:
open class UsersKotlinController : Serializable {
被忽略。没有错误。
我的技术。规格:
我看过:
企业版 Kotlin:Kotlin 和 Jakarta EE
Baeldung Michal Aibin:使用 Kotlin 的 Java EE 应用程序
有人能够在 Mojarra 上使用 Kotlin 1.9.x 和 Jakara Faces 4.0 吗?
[#2]
刚刚将 Kotlin 升级到 2.0.0-RC3:
implementation("org.jetbrains.kotlin:kotlin-stdlib:2.0.0-RC3")
implementation("org.jetbrains.kotlin:kotlin-maven-allopen:2.0.0-RC3")
首先注意到“WELD-000119:不生成任何bean”:
INFO [org.jboss.weld.Bootstrap] (Weld Thread Pool -- 2) WELD-000119: Not generating any bean definitions from com.bewherewhen.kotlin.controller.UsersKotlinController because of underlying class loading error: Type io.github.oshai.kotlinlogging.KLogger from [Module "deployment.BeWhereWhenKt.ear.BeWhereWhenKtJAR.jar" from Service Module Loader] not found.
If this is unexpected, enable DEBUG logging to see the full error.
设置 WildFly:子系统 |记录 |配置|水平|调试:没有错误...只有信息和警告 (x3)
WARN [org.wildfly.extension.elytron] (MSC service thread 1-2) WFLYELY00023: KeyStore file '/usr/local/Cellar/wildfly-as/wildfly-32.0.0.Final/standalone/configuration/application.keystore' does not exist. Used blank.
WARN [org.wildfly.extension.elytron] (MSC service thread 1-2) WFLYELY01084: KeyStore /usr/local/Cellar/wildfly-as/wildfly-32.0.0.Final/standalone/configuration/application.keystore not found, it will be auto-generated on first use with a self-signed certificate for host localhost
WARN [org.jboss.as.ejb3] (MSC service thread 1-8) WFLYEJB0131: Jakarta Enterprise Beans com.bewherewhen.kotlin.ejb.UsersKotlinEJB should not have a final or static method (getDatastoreBeWhereWhen)
[#3]
已将工作 Java 代码(使用 IntelliJ IDEA 2024.1.2 Ultimate EditionJ)转换为 Kotlin 2.0。
我的 EAR 无法在 WildFly 32.0.0.Final 上部署:
10:28:12,027 INFO [org.jboss.weld.deployer] (MSC service thread 1-5) WFLYWELD0003: Processing weld deployment BeWhereWhenWAR.war
10:28:12,086 INFO [org.jboss.weld.Version] (MSC service thread 1-8) WELD-000900: 5.1.2 (Final)
10:28:12,361 ERROR [org.jboss.msc.service.fail] (MSC service thread 1-6) MSC000001: Failed to start service jboss.deployment.unit."BeWhereWhen.ear".WeldStartService: org.jboss.msc.service.StartException in service jboss.deployment.unit."BeWhereWhen.ear".WeldStartService: Failed to start service
at [email protected]//org.jboss.msc.service.ServiceControllerImpl$StartTask.execute(ServiceControllerImpl.java:1609)
at [email protected]//org.jboss.msc.service.ServiceControllerImpl$ControllerTask.run(ServiceControllerImpl.java:1438)
at [email protected]//org.jboss.threads.ContextClassLoaderSavingRunnable.run(ContextClassLoaderSavingRunnable.java:35)
at [email protected]//org.jboss.threads.EnhancedQueueExecutor.safeRun(EnhancedQueueExecutor.java:1990)
at [email protected]//org.jboss.threads.EnhancedQueueExecutor$ThreadBody.doRunTask(EnhancedQueueExecutor.java:1486)
at [email protected]//org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1377)
at java.base/java.lang.Thread.run(Thread.java:1570)
Caused by: org.jboss.weld.exceptions.DefinitionException: WELD-000810: Cannot place qualifiers on final fields: [EnhancedAnnotatedFieldImpl] @Inject private final com.bewherewhen.kotlin.controller.EventsMeetingsSocialControllerKt.eventsMeetingsSocialEJBKt
at [email protected]//org.jboss.weld.injection.InjectionPointFactory.addFieldInjectionPoint(InjectionPointFactory.java:229)
at [email protected]//org.jboss.weld.injection.InjectionPointFactory.getFieldInjectionPoints(InjectionPointFactory.java:215)
at [email protected]//org.jboss.weld.injection.producer.DefaultInjector.<init>(DefaultInjector.java:56)
at [email protected]//org.jboss.weld.injection.producer.ResourceInjector.<init>(ResourceInjector.java:57)
at [email protected]//org.jboss.weld.injection.producer.ResourceInjector.of(ResourceInjector.java:48)
at [email protected]//org.jboss.weld.injection.producer.BeanInjectionTarget.<init>(BeanInjectionTarget.java:66)
at [email protected]//org.jboss.weld.injection.producer.BeanInjectionTarget.createDefault(BeanInjectionTarget.java:48)
at [email protected]//org.jboss.weld.manager.InjectionTargetFactoryImpl.chooseInjectionTarget(InjectionTargetFactoryImpl.java:145)
at [email protected]//org.jboss.weld.manager.InjectionTargetFactoryImpl.createInjectionTarget(InjectionTargetFactoryImpl.java:90)
at [email protected]//org.jboss.weld.bean.ManagedBean.<init>(ManagedBean.java:105)
at [email protected]//org.jboss.weld.bean.ManagedBean.of(ManagedBean.java:83)
at [email protected]//org.jboss.weld.bootstrap.AbstractBeanDeployer.createManagedBean(AbstractBeanDeployer.java:284)
at [email protected]//org.jboss.weld.bootstrap.BeanDeployer.createClassBean(BeanDeployer.java:284)
at [email protected]//org.jboss.weld.bootstrap.ConcurrentBeanDeployer$2.doWork(ConcurrentBeanDeployer.java:69)
at [email protected]//org.jboss.weld.bootstrap.ConcurrentBeanDeployer$2.doWork(ConcurrentBeanDeployer.java:66)
at [email protected]//org.jboss.weld.executor.IterativeWorkerTaskFactory$1.call(IterativeWorkerTaskFactory.java:62)
at [email protected]//org.jboss.weld.executor.IterativeWorkerTaskFactory$1.call(IterativeWorkerTaskFactory.java:55)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:317)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1144)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:642)
at java.base/java.lang.Thread.run(Thread.java:1570)
at [email protected]//org.jboss.threads.JBossThread.run(JBossThread.java:513)
10:28:12,364 ERROR [org.jboss.as.controller.management-operation] (External Management Request Threads -- 1) WFLYCTL0013: Operation ("add") failed - address: ([("deployment" => "BeWhereWhen.ear")]) - failure description: {"WFLYCTL0080: Failed services" => {"jboss.deployment.unit.\"BeWhereWhen.ear\".WeldStartService" => "Failed to start service
Caused by: org.jboss.weld.exceptions.DefinitionException: WELD-000810: Cannot place qualifiers on final fields: [EnhancedAnnotatedFieldImpl] @Inject private final com.bewherewhen.kotlin.controller.EventsMeetingsSocialControllerKt.eventsMeetingsSocialEJBKt"}}
10:28:12,364 ERROR [org.jboss.as.controller.management-operation] (External Management Request Threads -- 1) WFLYCTL0013: Operation ("add") failed - address: ([("deployment" => "BeWhereWhen.ear")]) - failure description: {"WFLYCTL0080: Failed services" => {"jboss.deployment.unit.\"BeWhereWhen.ear\".WeldStartService" => "Failed to start service
Caused by: org.jboss.weld.exceptions.DefinitionException: WELD-000810: Cannot place qualifiers on final fields: [EnhancedAnnotatedFieldImpl] @Inject private final com.bewherewhen.kotlin.controller.EventsMeetingsSocialControllerKt.eventsMeetingsSocialEJBKt"}}
10:28:12,365 ERROR [org.jboss.as.server] (External Management Request Threads -- 1) WFLYSRV0021: Deploy of deployment "BeWhereWhen.ear" was rolled back with the following failure message:
{"WFLYCTL0080: Failed services" => {"jboss.deployment.unit.\"BeWhereWhen.ear\".WeldStartService" => "Failed to start service
Caused by: org.jboss.weld.exceptions.DefinitionException: WELD-000810: Cannot place qualifiers on final fields: [EnhancedAnnotatedFieldImpl] @Inject private final com.bewherewhen.kotlin.controller.EventsMeetingsSocialControllerKt.eventsMeetingsSocialEJBKt"}}
和:
10:50:41,805 ERROR [org.jboss.msc.service.fail] (MSC service thread 1-3) MSC000001: Failed to start service jboss.deployment.unit."BeWhereWhen.ear".WeldStartService: org.jboss.msc.service.StartException in service jboss.deployment.unit."BeWhereWhen.ear".WeldStartService: Failed to start service
at [email protected]//org.jboss.msc.service.ServiceControllerImpl$StartTask.execute(ServiceControllerImpl.java:1609)
at [email protected]//org.jboss.msc.service.ServiceControllerImpl$ControllerTask.run(ServiceControllerImpl.java:1438)
at [email protected]//org.jboss.threads.ContextClassLoaderSavingRunnable.run(ContextClassLoaderSavingRunnable.java:35)
at [email protected]//org.jboss.threads.EnhancedQueueExecutor.safeRun(EnhancedQueueExecutor.java:1990)
at [email protected]//org.jboss.threads.EnhancedQueueExecutor$ThreadBody.doRunTask(EnhancedQueueExecutor.java:1486)
at [email protected]//org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1377)
at java.base/java.lang.Thread.run(Thread.java:1570)
Caused by: org.jboss.weld.exceptions.DefinitionException: WELD-000810: Cannot place qualifiers on final fields: [EnhancedAnnotatedFieldImpl] @Inject private final com.bewherewhen.kotlin.controller.EventsMeetingsSocialControllerKt.eventsMeetingsSocialEJBKt
at [email protected]//org.jboss.weld.injection.InjectionPointFactory.addFieldInjectionPoint(InjectionPointFactory.java:229)
at [email protected]//org.jboss.weld.injection.InjectionPointFactory.getFieldInjectionPoints(InjectionPointFactory.java:215)
at [email protected]//org.jboss.weld.injection.producer.DefaultInjector.<init>(DefaultInjector.java:56)
at [email protected]//org.jboss.weld.injection.producer.ResourceInjector.<init>(ResourceInjector.java:57)
at [email protected]//org.jboss.weld.injection.producer.ResourceInjector.of(ResourceInjector.java:48)
at [email protected]//org.jboss.weld.injection.producer.BeanInjectionTarget.<init>(BeanInjectionTarget.java:66)
at [email protected]//org.jboss.weld.injection.producer.BeanInjectionTarget.createDefault(BeanInjectionTarget.java:48)
at [email protected]//org.jboss.weld.manager.InjectionTargetFactoryImpl.chooseInjectionTarget(InjectionTargetFactoryImpl.java:145)
at [email protected]//org.jboss.weld.manager.InjectionTargetFactoryImpl.createInjectionTarget(InjectionTargetFactoryImpl.java:90)
at [email protected]//org.jboss.weld.bean.ManagedBean.<init>(ManagedBean.java:105)
at [email protected]//org.jboss.weld.bean.ManagedBean.of(ManagedBean.java:83)
at [email protected]//org.jboss.weld.bootstrap.AbstractBeanDeployer.createManagedBean(AbstractBeanDeployer.java:284)
at [email protected]//org.jboss.weld.bootstrap.BeanDeployer.createClassBean(BeanDeployer.java:284)
at [email protected]//org.jboss.weld.bootstrap.ConcurrentBeanDeployer$2.doWork(ConcurrentBeanDeployer.java:69)
at [email protected]//org.jboss.weld.bootstrap.ConcurrentBeanDeployer$2.doWork(ConcurrentBeanDeployer.java:66)
at [email protected]//org.jboss.weld.executor.IterativeWorkerTaskFactory$1.call(IterativeWorkerTaskFactory.java:62)
at [email protected]//org.jboss.weld.executor.IterativeWorkerTaskFactory$1.call(IterativeWorkerTaskFactory.java:55)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:317)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1144)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:642)
at java.base/java.lang.Thread.run(Thread.java:1570)
at [email protected]//org.jboss.threads.JBossThread.run(JBossThread.java:513)
10:50:41,807 ERROR [org.jboss.as.controller.management-operation] (External Management Request Threads -- 1) WFLYCTL0013: Operation ("add") failed - address: ([("deployment" => "BeWhereWhen.ear")]) - failure description: {"WFLYCTL0080: Failed services" => {"jboss.deployment.unit.\"BeWhereWhen.ear\".WeldStartService" => "Failed to start service
Caused by: org.jboss.weld.exceptions.DefinitionException: WELD-000810: Cannot place qualifiers on final fields: [EnhancedAnnotatedFieldImpl] @Inject private final com.bewherewhen.kotlin.controller.EventsMeetingsSocialControllerKt.eventsMeetingsSocialEJBKt"}}
10:50:41,807 ERROR [org.jboss.as.controller.management-operation] (External Management Request Threads -- 1) WFLYCTL0013: Operation ("add") failed - address: ([("deployment" => "BeWhereWhen.ear")]) - failure description: {"WFLYCTL0080: Failed services" => {"jboss.deployment.unit.\"BeWhereWhen.ear\".WeldStartService" => "Failed to start service
Caused by: org.jboss.weld.exceptions.DefinitionException: WELD-000810: Cannot place qualifiers on final fields: [EnhancedAnnotatedFieldImpl] @Inject private final com.bewherewhen.kotlin.controller.EventsMeetingsSocialControllerKt.eventsMeetingsSocialEJBKt"}}
10:50:41,807 ERROR [org.jboss.as.server] (External Management Request Threads -- 1) WFLYSRV0021: Deploy of deployment "BeWhereWhen.ear" was rolled back with the following failure message:
{"WFLYCTL0080: Failed services" => {"jboss.deployment.unit.\"BeWhereWhen.ear\".WeldStartService" => "Failed to start service
Caused by: org.jboss.weld.exceptions.DefinitionException: WELD-000810: Cannot place qualifiers on final fields: [EnhancedAnnotatedFieldImpl] @Inject private final com.bewherewhen.kotlin.controller.EventsMeetingsSocialControllerKt.eventsMeetingsSocialEJBKt"}}
有人知道WELD的问题吗?短暂性脑缺血发作。
修复了将我的 Java 迁移到 Kotlin 时因“字段不能作为注入而最终注入”引起的问题。
将“lateinit var”添加到“控制器”:
@Named("eventsMeetingsSocialControllerKt")
@RequestScoped
open class EventsMeetingsSocialControllerKt {
private lateinit var eventsMeetingsSocialList: List<com.bewherewhen.kotlin.entity.morphia.EventsMeetingsSocial>
@Inject
private lateinit var eventsMeetingsSocialEJBKt: EventsMeetingsSocialEJBKt
...
}
将“lateinit var”添加到“EJB”:
@Stateless
@LocalBean
open class EventsMeetingsSocialEJBKt {
...
private lateinit var eventsMeetingsSocialsLists:
List<EventsMeetingsSocial>
open fun allEventsMeetingsSocial(): List<EventsMeetingsSocial> {
val eventsMeetingsSocialQuery =
datastoreBeWhereWhen.find(EventsMeetingsSocial::class.java)
eventsMeetingsSocialQuery.iterator().toList()
eventsMeetingsSocialsLists = eventsMeetingsSocialQuery.iterator().toList()
...
}
}
“可序列化”到实体:
@Entity(value = "eventsMeetingsSocial", useDiscriminator = false)
@Indexes(Index(options = IndexOptions(name = "eventsMeetingsSocial")))
class EventsMeetingsSocial : Serializable {
/**
*
*/
private var eventsMeetingsSocial: String? = null
/**
*
*/
fun EventsMeetingsSocial() {
}
/**
*
* @param eventsMeetingsSocial String
*/
fun EventsMeetingsSocial(eventsMeetingsSocial: String?) {
this.eventsMeetingsSocial = eventsMeetingsSocial
}
/**
*
* @return
*/
fun getEventsMeetingsSocial(): String? {
return eventsMeetingsSocial
}
/**
*
* @param eventsMeetingsSocial String
*/
fun setEventsMeetingsSocial(eventsMeetingsSocial: String?) {
this.eventsMeetingsSocial = eventsMeetingsSocial
}
override fun toString(): String {
return "EventsMeetingsSocialKotlin{" +
"eventsMeetingsSocial='" + eventsMeetingsSocial + '\'' +
'}'
}
}
数据:
INFO [org.mongodb.driver.cluster] (cluster-ClusterId{value='6661d00b2cab0c1c84295dad', description='null'}-localhost:27017) Monitor thread successfully connected to server with description ServerDescription{address=localhost:27017, type=STANDALONE, state=CONNECTED, ok=true, minWireVersion=0, maxWireVersion=21, maxDocumentSize=16777216, logicalSessionTimeoutMinutes=30, roundTripTimeNanos=408750}
INFO [com.bewherewhen.kotlin.ejb.EventsMeetingsSocialEJBKt] (default task-1) ***** 8.1 EventsMeetingsSocial eventsMeetingsSocialsLists = 5
INFO [com.bewherewhen.kotlin.controller.EventsMeetingsSocialControllerKt] (default task-1) ***** 7.1 eventsMeetingsSocialKtEJB.getAllEventsMeetingsSocial size = 5
INFO [com.bewherewhen.kotlin.controller.EventsMeetingsSocialControllerKt] (default task-1) ***** 7.2 eventsMeetingsSocialJtEJB.getAllEventsMeetingsSocial = [EventsMeetingsSocialKotlin{eventsMeetingsSocial='Friends'}, EventsMeetingsSocialKotlin{eventsMeetingsSocial='Family'}, EventsMeetingsSocialKotlin{eventsMeetingsSocial='Colleague'}, EventsMeetingsSocialKotlin{eventsMeetingsSocial='Neighbours'}, EventsMeetingsSocialKotlin{eventsMeetingsSocial='Acquaintances'}]