语法错误 - @PostConstructor Annotation

时间:2015-02-22 06:19:06

标签: 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.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>

1 个答案:

答案 0 :(得分:2)

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

import javax.annotation.PostConstruct;