在Spring启动应用程序中的save()和update()上获取StackOverflow异常?

时间:2014-06-24 10:09:29

标签: java maven spring-security spring-boot

只有当我调用save()或update()时才会收到StackOverflow异常,否则它会正常工作。

我的POM文件是

<project xmlns="http://maven.apache.org/POM/4.0.0" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <name>SpringBootApp</name>
      <groupId>com.sagar</groupId>
      <artifactId>Sample</artifactId>
      <version>1.0.0-SNAPSHOT</version>
      <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.0.2.RELEASE</version>
        <relativePath/>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <start-class>com.sagar.Application</start-class>
        <java.version>1.7</java.version>
        <cglib.version>3.0</cglib.version>
        <jackson-mapper-asl.version>1.9.12</jackson-mapper-asl.version>
        <sqlite-jdbc.version>3.7.2</sqlite-jdbc.version>
        <mysql-connector-java.version>5.1.30</mysql-connector-java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- HHibernate Validator Engine -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>5.1.0.Final</version>
        </dependency>

        <!-- SQLite database JDBC -->
        <dependency>
            <groupId>org.xerial</groupId>
            <artifactId>sqlite-jdbc</artifactId>
            <version>${sqlite-jdbc.version}</version>
        </dependency>

        <!-- MySQL java connector -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql-connector-java.version}</version>
        </dependency>

        <!-- Apache Commons Upload --> 
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.2.2</version>
        </dependency>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-io</artifactId>
            <version>1.3.2</version>
        </dependency>
</dependencies>
    <build>
        <finalName>sample</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

控制器方法:

@RequestMapping(value = API.HOME, 
        method = RequestMethod.GET)
public String home(HttpServletRequest httpRequest, @CurrentUser
        CustomUserDetails currentUserDetail) {
    User user = new User();
    user.setUserId("user1");
    user.setUserName("user1");
    user.setPassword("pass1");
    user.setEmailId("user1@company.com");
    myRepository.getUserService().save(user);
    return new "Success";
}

我有一个JPAConfig.java,我设置了我的dataSource, entityManagerFactory, jpaProperties, jpaVendorAdapter and transactionManager bean和SecurityConfig.java并实现了protected void configure(HttpSecurity http)

谢谢

1 个答案:

答案 0 :(得分:0)

很抱歉问题出在我的代码上,即

public class UserRepository implements User {
    @Autowired
    private UserRepository userRepository;

    @Transactional
    public boolean save(User user) {
        User u = userRepository.save(user);
        if(u == null) {
            return false;
        } else {
            return true;
        }
    }
}

无限地调用save()。这是我在spring 3.1.1中的旧实现,这不适用于spring 4.0.5。