Spring柠檬BeanCurrentlyInCreationException:创建bean时出错

Sir*_*ann 2 java spring spring-mvc spring-boot spring-lemon

我正在尝试遵循这个 spring-lemon 入门教程(https://naturalprogrammer.gitbooks.io/spring-lemon-getting-started/content/index.html),但在某个时刻我无法进一步进行。我创建了一个新的 spring 启动项目(Spring boot),并且我能够向其中添加 spring 柠檬。除了按照说明进行操作之外,我什么也没做,但是当我开始 Maven 构建时,测试失败并出现以下错误:

org.springframework.beans.factory.BeanCreationException:创建名称为“lmnDemoController”的bean时出错:自动装配依赖项注入失败;嵌套异常是 org.springframework.beans.factory.BeanCreationException:无法自动装配方法:public void com.naturalprogrammer.spring.lemon.LemonController.setLemonService(com.naturalprogrammer.spring.lemon.LemonService); 嵌套异常是 org.springframework.beans.factory.BeanCurrentlyInCreationException:创建名称为“lmnDemoService”的 bean 时出错:名称为“lmnDemoService”的 Bean 已作为循环引用的一部分注入其原始版本中的其他 bean [authenticationSuccessHandler],但已注入最终被包裹。这意味着所述其他 bean 不使用该 bean 的最终版本。这通常是过度渴望类型匹配的结果 - 例如,考虑使用“getBeanNamesOfType”并关闭“allowEagerInit”标志。

我的LmnDemoService.java是:

package com.example;

import org.springframework.stereotype.Service;
import com.naturalprogrammer.spring.lemon.LemonService;

@Service
public class LmnDemoService extends LemonService<User, Long> { 
    @Override
    protected User newUser() {
        return new User();
    }
}
Run Code Online (Sandbox Code Playgroud)

它没有任何其他内容,只是教程中所说的几行内容。我缺少什么?

编辑:

LmndemoApplication.java

package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import com.naturalprogrammer.spring.lemon.LemonConfig;

@SpringBootApplication(scanBasePackageClasses = {LmndemoApplication.class, LemonConfig.class})
public class LmndemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(LmndemoApplication.class, args);
    }
}
Run Code Online (Sandbox Code Playgroud)

LmnDemoController.java

package com.example;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.naturalprogrammer.spring.lemon.LemonController;

@RestController
@RequestMapping("/api/core")
public class LmnDemoController extends LemonController<User, Long> {

}
Run Code Online (Sandbox Code Playgroud)

LmnDemoService.java

package com.example;

import org.springframework.stereotype.Service;

import com.naturalprogrammer.spring.lemon.LemonService;

@Service
public class LmnDemoService extends LemonService<User, Long> {

    @Override
    protected User newUser() {
        return new User();
    }
}
Run Code Online (Sandbox Code Playgroud)

SecurityConfig.java

package com.example;

import org.springframework.context.annotation.Configuration;

import com.naturalprogrammer.spring.lemon.security.LemonSecurityConfig;

@Configuration
public class SecurityConfig extends LemonSecurityConfig {

}
Run Code Online (Sandbox Code Playgroud)

ServletInitializer.java

package com.example;

import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;

public class ServletInitializer extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(LmndemoApplication.class);
    }

}
Run Code Online (Sandbox Code Playgroud)

User.java

package com.example;

import javax.persistence.Entity;
import javax.persistence.Table;

import com.naturalprogrammer.spring.lemon.domain.AbstractUser;

@Entity 
@Table(name="usr")
public class User extends AbstractUser<User,Long> {

    private static final long serialVersionUID = 2716710947175132319L;

}
Run Code Online (Sandbox Code Playgroud)

UserRepository.java

package com.example;

import com.naturalprogrammer.spring.lemon.domain.AbstractUserRepository;

public interface UserRepository extends AbstractUserRepository<User, Long> {

}
Run Code Online (Sandbox Code Playgroud)

application.properties

#Database configuration
spring.jpa.database: MYSQL
spring.jpa.hibernate.ddl-auto: update

spring.datasource.url: jdbc:mysql://localhost:3306/mydatabase
spring.datasource.username: myuser
spring.datasource.password: mypassword

#DevTools configuration
spring.devtools.livereload.enabled: false
spring.devtools.restart.enabled: false

#Application URL
#lemon.application-url: http://localhost:9000

#reCAPTCHA configuration
#lemon.recaptcha.sitekey: your google recaptcha site key
#lemon.recaptcha.secretkey: your google recaptcha secret key

#Remember me configuration
#lemon.remember-me-key: someSecret

#First administrator
lemon.admin.username: admin@example.com
lemon.admin.password: admin

#Email configuration
#spring.mail.host = smtp.gmail.com
#spring.mail.username = xxxxxx@gmail.com
#spring.mail.password = xxxxxx
#
#spring.mail.properties.mail.smtp.auth = true
#spring.mail.properties.mail.smtp.socketFactory.port = 465
#spring.mail.properties.mail.smtp.socketFactory.class =        javax.net.ssl.SSLSocketFactory
#spring.mail.properties.mail.smtp.socketFactory.fallback = false
#spring.mail.properties.mail.smtp.ssl.enable = true

#Handling cross origin requests configuration
#lemon.cors.allowed-origins: http://localhost:9000

#This will disable the protection against JSON vulnerability
#lemon.enabled.json-prefix: false
Run Code Online (Sandbox Code Playgroud)

pom.xml

http://maven.apache.org/xsd/maven-4.0.0.xsd">4.0.0

<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>

<name>lmndemo</name>
<description>Demo project for Spring Boot</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.3.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

    <!-- Spring-lemon -->
    <dependency>
        <groupId>com.naturalprogrammer.spring</groupId>
        <artifactId>spring-lemon</artifactId>
        <version>0.8.5</version><!-- See https://github.com/naturalprogrammer/spring-lemon/releases for latest release -->
    </dependency>
</dependencies>

<build>
    <finalName>lmndemo</finalName>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>
Run Code Online (Sandbox Code Playgroud)

San*_*jay 5

在我看来就像一个循环引用问题。这在我的环境中没有发生,我认为理想情况下 Spring 应该能够自行解决它,但我认为最好是修复 Spring Lemon 代码以使其更加健壮。

那么,您可以尝试在工作区中签出的 Spring Lemon 代码中的类的方法@Lazy上添加注释吗?使其看起来如下:setLemonServiceAuthenticationSuccessHandler

@Autowired
@Lazy // add this line
public void setLemonService(LemonService<?, ?> lemonService) {
    this.lemonService = lemonService;
}
Run Code Online (Sandbox Code Playgroud)

我以前没有尝试过@Lazy,但我认为它应该有效。如果没有,我们需要考虑另一种方法来打破循环依赖。

让我知道。找到解决方案后,我将检查 Spring Lemon 存储库的修复。