我正在尝试使用Spring-boot+Vaadin创建一个web项目,并且希望使用spring-data-jpa和hibernate从PostgreSQL数据库中获取数据。
在我的Vaadin看来,我尝试自动连接我的服务类,但我总是得到null,而且eror堆栈跟踪没有告诉我原因。
数据库得我得配置:
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = arrayOf("com.apache.vaadin.model"))
@ComponentScan(basePackages = arrayOf("com.apache.vaadin.model"))
open class DataSourceConfig {
@Autowired
lateinit var environment: Environment
@Bean
open fun entityManagerFactory(): LocalContainerEntityManagerFactoryBean {
var entityManagerFactory: LocalContainerEntityManagerFactoryBean = LocalContainerEntityManagerFactoryBean()
entityManagerFactory.apply {
dataSource = dataSource()
setPackagesToScan("com.apache.vaadin.model")
var vendorAdapter: HibernateJpaVendorAdapter = HibernateJpaVendorAdapter()
vendorAdapter.apply {
setGenerateDdl(true)
setShowSql(true)
}
var properties: Properties = Properties()
properties.apply {
put("database.dialet", "org.hibernate.dialect.PostgreSQL95Dialect")
put("database.globally_quoted_identifiers", "false")
put("database.enable_lazy_load_no_trans", "true")
put("database.show_sql", "true")
}
jpaVendorAdapter = vendorAdapter
setJpaProperties(properties)
}
return entityManagerFactory
}
@Primary
@Bean
open fun dataSource(): DataSource {
var source: ComboPooledDataSource = ComboPooledDataSource()
source.apply {
driverClass = "org.postgresql.Driver"
jdbcUrl = "jdbc:postgresql://localhost:5432/ignite"
user = "postgres"
password = "1111"
acquireIncrement = 5
idleConnectionTestPeriod = 60
maxPoolSize = 20
minPoolSize = 10
initialPoolSize = 10
}
return source
}
@Bean
open fun transactionManager() : PlatformTransactionManager {
var manager: JpaTransactionManager = JpaTransactionManager()
manager.apply {
entityManagerFactory = entityManagerFactory().nativeEntityManagerFactory
}
return manager
}
@Bean
open fun exceptionTranslator(): PersistenceExceptionTranslationPostProcessor = PersistenceExceptionTranslationPostProcessor()
}
视图和UI:
@SpringUI
@Title(value = "Apache")
class Navigator : UI() {
@Autowired
lateinit var viewProvider: SpringViewProvider
init {
}
private val INDEX = ""
override fun init(p0: VaadinRequest?) {
navigator = Navigator(this, this)
navigator.addProvider(viewProvider)
navigator.addView(INDEX, Index::class.java)
}
}
@UIScope
@SpringView
open class Index : VerticalLayout(), View {
private val logger: Logger = Logger.getLogger(Index::class.java)
@Autowired
lateinit var clientService: IClientService // <--- error is here
init {
var menuBar: MenuBar = MenuBar()
var file = menuBar.addItem("File", null, null)
file.addItem("Save", {_ -> logger.info("Save clicked")})
file.addItem("Load", {_ -> logger.info("Load clicked")})
file.addItem("Open", {_ -> logger.info("Open clicked")})
file.addItem("Close", {_ -> logger.info("Close clicked")})
var settings = menuBar.addItem("Settings", null, null)
settings.addItem("DataBase", {_ -> logger.info("DataBase clicked")})
addComponent(menuBar)
var createButton: Button = Button("Create", { _ -> logger.info("Create clicked")})
var readButton: Button = Button("Read", { _ -> logger.info(clientService.findAll())})
var updateButton: Button = Button("Update", { _ -> logger.info("Update clicked")})
var deleteButton: Button = Button("Delete", { _ -> logger.info("Delete clicked")})
addComponent(createButton)
addComponent(readButton)
addComponent(updateButton)
addComponent(deleteButton)
}
override fun enter(p0: ViewChangeListener.ViewChangeEvent?) {
Notification.show("Welcome to the main com.ignite.app.view")
}
}
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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>com.apache.vaadin</groupId>
<artifactId>ignite</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>ignite</name>
<description></description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<vaadin.version>8.0.6</vaadin.version>
<kotlin.version>1.1.3-2</kotlin.version>
<hibernate>5.2.9.Final</hibernate>
<hibernate-validator>5.4.1.Final</hibernate-validator>
<postgres>42.1.1</postgres>
</properties>
<dependencies>
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>vaadin-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jre8</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-test</artifactId>
<version>${kotlin.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>
<version>${hibernate}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-c3p0</artifactId>
<version>${hibernate}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>${hibernate-validator}</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>${postgres}</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>vaadin-bom</artifactId>
<version>${vaadin.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
<configuration>
<jvmTarget>1.8</jvmTarget>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>testCompile</id>
<phase>test-compile</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
我的服务:
@NoRepositoryBean
interface IClientService : CrudRepository<Client, Long>
@Service
class ClientService : IClientService {
private val logger: Logger = Logger.getLogger(ClientService::class.java)
@Autowired
lateinit var clientRepository: ClientRepository
override fun delete(p0: Long?) {
clientRepository.delete(p0)
}
override fun delete(p0: MutableIterable<Client>?) {
clientRepository.delete(p0)
}
override fun delete(p0: Client?) {
clientRepository.delete(p0)
}
override fun <S : Client?> save(p0: MutableIterable<S>?): MutableIterable<S> = clientRepository.save(p0)
override fun <S : Client?> save(p0: S): S = clientRepository.save(p0)
override fun findAll(p0: MutableIterable<Long>?): MutableIterable<Client> = clientRepository.findAll(p0)
override fun findAll(): MutableIterable<Client> = clientRepository.findAll()
override fun exists(p0: Long?): Boolean = clientRepository.exists(p0)
override fun findOne(p0: Long?): Client = clientRepository.findOne(p0)
override fun count(): Long = clientRepository.count()
override fun deleteAll() {
clientRepository.deleteAll()
}
}
@Repository
interface ClientRepository : CrudRepository<Client, Long>
更新的解决方案:
@SpringUI
@Title(value = "Apache")
class Navigator : UI() {
@Autowired
lateinit var viewProvider: SpringViewProvider
@Autowired
lateinit var index: Index// <-
private val INDEX = "index"
override fun init(p0: VaadinRequest?) {
navigator = Navigator(this, this)
navigator.addProvider(viewProvider)
navigator.addView(INDEX, index) // <-
}
}
@UIScope
@SpringView
@SpringComponent // <-
open class Index : VerticalLayout(), View {...}
这是因为您试图访问init
块中的clientservice
,您不应该这样做,因为spring依赖项注入发生在对象就绪创建之后。例如:
@Autowired
lateinit var clientService: IClientService
init{
// v--- `clientService` is not injected by spring yet.
clientService;
}
另一方面,您应该为spring组件初始化实现initializingbean
,例如:
open class Index : VerticalLayout(), View, InitializingBean {
@Autowired
lateinit var clientService: IClientService
override fun afterPropertiesSet(){
//copy your init block code here
}
}
Json服务器数据显示,在使用retrofit2和rxjava2的android中,当参数传递给inteface时,会出现错误。 接口
我不知道这是怎么回事 主要活动 我遇到了这个问题 2022-03-18 09:18:27.393 13874-13874/com。实例githubuser2 E/AndroidRuntime:致命异常:主进程:com。实例githubuser2,PID:13874 java。lang.RuntimeException:无法启动活动组件信息{com.example.githubuser2/com.e
我有一个类,我注入到ViewModel ViewModel工厂,当在活动中的onCreate方法中初始化视图模型时,它说传递的值没有初始化。 下面是我的代码 我对Kotlin很陌生,所以尝试过调试,但在这个问题上卡住了。 这是Mainactive代码: 我的ViewModelFactory: 我的班级回购: 这是我收到的错误:
我知道一个类似的问题已经在这里得到了回答。但那是由于黄油刀库的问题,但我的情况不同。在我的例子中,当我在基本活动中使用dagger injected属性时,它显示了由:kotlin.uninitializedPropertyAccessException引起的错误:lateinit属性pref尚未初始化 AppComponent
我试图将ViewModelFactory注入到活动中,但它总是抛出同样的错误:lateinit属性ViewModelFactory尚未初始化。我找不到我可能做错了什么。请参阅上面我的类中的代码 AppComponent.kt TweetSentimentsApplication.kt
我正在使用Dagger2来注入类依赖,比如Bellow。 但我在TranslateProvider.kt收到一个错误: UninitializedPropertyAccessException:lateinit属性appComponent尚未初始化 我不明白,请帮帮我。 谢了!