语法错误 - @PostConstructor 注释

Syntax Error - @PostConstructor Annotation

我正在学习 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;