Java 语法错误-@PostConstructor注释

Java 语法错误-@PostConstructor注释,java,spring,spring-mvc,jpa,annotations,Java,Spring,Spring Mvc,Jpa,Annotations,我正在学习Spring的教程。在本例中,它创建了一个带有@PostConstruct注释的方法。但是我试图把但是Spring抛出语法错误 然后Spring为我提供3个选项: 创建注释 在文件中重命名 修复项目设置 有人知道我该怎么办 package cr.test.jba.service; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.

我正在学习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");
    }
和应用程序上下文:

<?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;

@如果答案有助于你解决问题,请接受答案。对不起,刚才我看到了你的帖子。非常感谢。