gpt4 book ai didi

java - 语法错误 - @PostConstructor 注解

转载 作者:行者123 更新时间:2023-11-30 03:32:41 25 4
gpt4 key购买 nike

我正在学习 Spring 教程。在示例中,它创建了一个带有 @PostConstruct 注释的方法。但我试图放置,但 Spring 抛出语法错误。

然后 Spring 为我提供了 3 个选项:

  • 创建注释
  • 在文件中重命名
  • 修复项目设置

有人知道我要做什么吗?

package cr.test.jba.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cr.test.jba.entity.Role;
import cr.test.jba.repository.BlogRepository;
import cr.test.jba.repository.ItemRepository;
import cr.test.jba.repository.RoleRepository;
import cr.test.jba.repository.UserRepository;


@Service
public class InitDbService {

@Autowired
private RoleRepository roleRepository;

@Autowired
private UserRepository userRepository;

@Autowired
private BlogRepository blogRepository;

@Autowired
private ItemRepository itemRepository;

@PostConstruct
public void init(){
Role roleUser = new Role();
roleUser.setName("ROLE_USER");
}

和applicationContext:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">

<context:component-scan base-package="cr.test.jba">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>


<jdbc:embedded-database type="HSQL" id="datasource" />

<bean class ="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="emf">
<property name="packagesToScan" value="cr.test.jba.entity"></property>
<property name="dataSource" ref="datasource"></property>
<property name="jpaProperties">
<props>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
</props>
</property>
<property name="persistenceProvider">
<bean class="org.hibernate.jpa.HibernatePersistenceProvider" />

</property>


</bean>

<tx:annotation-driven transaction-manager="transactionManager"/>

<bean class="org.springframework.orm.jpa.JpaTransactionManager" id="TransactionManager" >
<property name="dataSource" ref="datasource"></property>
</bean>

<jpa:repositories base-package="cr.test.jba.repository" entity-manager-factory-ref="emf" transaction-manager-ref="TransactionManager" />

</beans>

最佳答案

您似乎尚未导入 @PostConstruct 注释。请添加以下导入语句:

import javax.annotation.PostConstruct;

关于java - 语法错误 - @PostConstructor 注解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28655150/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com