java - 语法错误 - @PostConstructor 注解

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

最佳答案

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

import javax.annotation.PostConstruct;

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

相关文章:

java - mock bean 并不总是有效

java - Guava 是否具有采用自定义散列/等于函数的 Map 实现?

java - 你能帮我理解java :comp/env (The Enterprise Naming Context aka ENC)?的目的吗

Java 事件处理帮助(通知与 EventObject)

java - 多线程环境下Spring Service实例变量

spring - Webflux JWT 授权无法正常工作

java - 使用 MockMvc 测试 @preAuthorize(custom_implementation)

spring - 在Linux虚拟机中运行嵌入式tomcat java Spring应用程序(Microsoft Azure)

java - 创建名为 'webSecurityConfig' 的 bean 时出错,因为当我尝试将 UserService Bean Autowiring 到 WebSecurityConfig 中时,它不存在

java - JPA + Hibernate + Spring + OneToMany 删除级联