Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring Bug:考虑定义一个名为“Y*X27”的bean;实体管理工厂&x27;在您的配置中_Java_Spring_Spring Mvc_Spring Boot_Spring Data Jpa - Fatal编程技术网

Java Spring Bug:考虑定义一个名为“Y*X27”的bean;实体管理工厂&x27;在您的配置中

Java Spring Bug:考虑定义一个名为“Y*X27”的bean;实体管理工厂&x27;在您的配置中,java,spring,spring-mvc,spring-boot,spring-data-jpa,Java,Spring,Spring Mvc,Spring Boot,Spring Data Jpa,我在尝试启动Spring Boot应用程序时收到以下错误: 注意:仅当我将@Autowired注释置于idevicepository idevicepository上方时,才会发生此错误 我还没有将DeviceDao.java持久化到数据库中,但是正在创建实体 > *************************** > APPLICATION FAILED TO START > *************************** > > Descript

我在尝试启动Spring Boot应用程序时收到以下错误:

注意:仅当我将
@Autowired
注释置于
idevicepository idevicepository上方时,才会发生此错误DeviceDao.java
类中的code>

我还没有将
DeviceDao.java
持久化到数据库中,但是正在创建实体

> *************************** 
> APPLICATION FAILED TO START
> ***************************
> 
> Description: 
> Field iDeviceRepository in com.abc.dao.DeviceDao required a bean named 'entityManagerFactory' that could not be found. 
> Action:
> Consider defining a bean named 'entityManagerFactory' in your configuration.
这是项目的目录结构:

├───src
│   ├───main
│   │   ├───java
│   │   │   └───com
│   │   │       └───abc
│   │   │           ├───controller
│   │   │           ├───dao
│   │   │           │   └───repositories
│   │   │           ├───init
│   │   │           ├───model
│   │   │           ├───service
│   │   │           └───util
│   │   │               ├───common
│   │   │               ├───enums
│   │   │               ├───exceptions
│   │   │               └───interfaces
│   │   └───resources
│   │       ├───static
│   │       │   ├───css
│   │       │   ├───fonts
│   │       │   ├───img
│   │       │   └───js
│   │       └───templates
com.abc.init.Application.java

package com.abc.init;

@SpringBootApplication
@EnableJpaRepositories("com.abc.dao.repositories")
@EntityScan(basePackages = { "com.abc.model" })
@ComponentScan(basePackages={ "com.abc.controller", "com.abc.service", "com.abc.dao" })
public class Application
{
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
package com.abc.controller;

@Controller
public class RegisterController
{
    @Autowired
    RegisterServiceImpl registerService;

    @GetMapping("/register")
    public String registerForm(Model model) {
        model.addAttribute("device", new Device());
        return "registerDevice";
    }

    @PostMapping("/register")
    public String registerSubmit(@ModelAttribute Device device) {
        registerService.registerDevice(device)
        return "registerDeviceSuccess";
    }
}
package com.abc.service;

@Service
public class RegisterServiceImpl implements IRegisterService
{
    @Autowired
    DeviceDao devDao;

    public boolean registerDevice (Device device) {
        devDao.saveDevice(device);
        return true;
    }
}
package com.abc.dao;

@Repository
public class DeviceDao 
{
    @Autowired
    IDeviceRepository iDeviceRepository;

    public Device saveDevice(Device device) {
        return iDeviceRepository.save(device);
    }
}
package com.abc.dao.repositories;

@Repository
public interface IDeviceRepository extends CrudRepository<Device, Long> {}
com.abc.controller.RegisterController.java

package com.abc.init;

@SpringBootApplication
@EnableJpaRepositories("com.abc.dao.repositories")
@EntityScan(basePackages = { "com.abc.model" })
@ComponentScan(basePackages={ "com.abc.controller", "com.abc.service", "com.abc.dao" })
public class Application
{
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
package com.abc.controller;

@Controller
public class RegisterController
{
    @Autowired
    RegisterServiceImpl registerService;

    @GetMapping("/register")
    public String registerForm(Model model) {
        model.addAttribute("device", new Device());
        return "registerDevice";
    }

    @PostMapping("/register")
    public String registerSubmit(@ModelAttribute Device device) {
        registerService.registerDevice(device)
        return "registerDeviceSuccess";
    }
}
package com.abc.service;

@Service
public class RegisterServiceImpl implements IRegisterService
{
    @Autowired
    DeviceDao devDao;

    public boolean registerDevice (Device device) {
        devDao.saveDevice(device);
        return true;
    }
}
package com.abc.dao;

@Repository
public class DeviceDao 
{
    @Autowired
    IDeviceRepository iDeviceRepository;

    public Device saveDevice(Device device) {
        return iDeviceRepository.save(device);
    }
}
package com.abc.dao.repositories;

@Repository
public interface IDeviceRepository extends CrudRepository<Device, Long> {}
com.abc.service.RegisterServiceImpl.java

package com.abc.init;

@SpringBootApplication
@EnableJpaRepositories("com.abc.dao.repositories")
@EntityScan(basePackages = { "com.abc.model" })
@ComponentScan(basePackages={ "com.abc.controller", "com.abc.service", "com.abc.dao" })
public class Application
{
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
package com.abc.controller;

@Controller
public class RegisterController
{
    @Autowired
    RegisterServiceImpl registerService;

    @GetMapping("/register")
    public String registerForm(Model model) {
        model.addAttribute("device", new Device());
        return "registerDevice";
    }

    @PostMapping("/register")
    public String registerSubmit(@ModelAttribute Device device) {
        registerService.registerDevice(device)
        return "registerDeviceSuccess";
    }
}
package com.abc.service;

@Service
public class RegisterServiceImpl implements IRegisterService
{
    @Autowired
    DeviceDao devDao;

    public boolean registerDevice (Device device) {
        devDao.saveDevice(device);
        return true;
    }
}
package com.abc.dao;

@Repository
public class DeviceDao 
{
    @Autowired
    IDeviceRepository iDeviceRepository;

    public Device saveDevice(Device device) {
        return iDeviceRepository.save(device);
    }
}
package com.abc.dao.repositories;

@Repository
public interface IDeviceRepository extends CrudRepository<Device, Long> {}
com.abc.util.interfaces.IRegisterService

package com.abc.util.interfaces;

public interface IRegisterService
{
    public boolean registerDevice(Device device);
}
com.abc.dao.DeviceDao.java

package com.abc.init;

@SpringBootApplication
@EnableJpaRepositories("com.abc.dao.repositories")
@EntityScan(basePackages = { "com.abc.model" })
@ComponentScan(basePackages={ "com.abc.controller", "com.abc.service", "com.abc.dao" })
public class Application
{
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
package com.abc.controller;

@Controller
public class RegisterController
{
    @Autowired
    RegisterServiceImpl registerService;

    @GetMapping("/register")
    public String registerForm(Model model) {
        model.addAttribute("device", new Device());
        return "registerDevice";
    }

    @PostMapping("/register")
    public String registerSubmit(@ModelAttribute Device device) {
        registerService.registerDevice(device)
        return "registerDeviceSuccess";
    }
}
package com.abc.service;

@Service
public class RegisterServiceImpl implements IRegisterService
{
    @Autowired
    DeviceDao devDao;

    public boolean registerDevice (Device device) {
        devDao.saveDevice(device);
        return true;
    }
}
package com.abc.dao;

@Repository
public class DeviceDao 
{
    @Autowired
    IDeviceRepository iDeviceRepository;

    public Device saveDevice(Device device) {
        return iDeviceRepository.save(device);
    }
}
package com.abc.dao.repositories;

@Repository
public interface IDeviceRepository extends CrudRepository<Device, Long> {}
com.abc.dao.repositories.IDeviceRepository.java

package com.abc.init;

@SpringBootApplication
@EnableJpaRepositories("com.abc.dao.repositories")
@EntityScan(basePackages = { "com.abc.model" })
@ComponentScan(basePackages={ "com.abc.controller", "com.abc.service", "com.abc.dao" })
public class Application
{
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
package com.abc.controller;

@Controller
public class RegisterController
{
    @Autowired
    RegisterServiceImpl registerService;

    @GetMapping("/register")
    public String registerForm(Model model) {
        model.addAttribute("device", new Device());
        return "registerDevice";
    }

    @PostMapping("/register")
    public String registerSubmit(@ModelAttribute Device device) {
        registerService.registerDevice(device)
        return "registerDeviceSuccess";
    }
}
package com.abc.service;

@Service
public class RegisterServiceImpl implements IRegisterService
{
    @Autowired
    DeviceDao devDao;

    public boolean registerDevice (Device device) {
        devDao.saveDevice(device);
        return true;
    }
}
package com.abc.dao;

@Repository
public class DeviceDao 
{
    @Autowired
    IDeviceRepository iDeviceRepository;

    public Device saveDevice(Device device) {
        return iDeviceRepository.save(device);
    }
}
package com.abc.dao.repositories;

@Repository
public interface IDeviceRepository extends CrudRepository<Device, Long> {}
堆栈跟踪:

java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'registerController': Unsatisfied dependency expressed through field 'registerService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'registerServiceImpl': Unsatisfied dependency expressed through field 'devDao'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'deviceDao': Unsatisfied dependency expressed through field 'iDeviceRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'IDeviceRepository': Cannot create inner bean '(inner bean)#36dce7ed' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#36dce7ed': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' available
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'registerServiceImpl': Unsatisfied dependency expressed through field 'devDao'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'deviceDao': Unsatisfied dependency expressed through field 'iDeviceRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'IDeviceRepository': Cannot create inner bean '(inner bean)#36dce7ed' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#36dce7ed': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' available
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'deviceDao': Unsatisfied dependency expressed through field 'iDeviceRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'IDeviceRepository': Cannot create inner bean '(inner bean)#36dce7ed' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#36dce7ed': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' available
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'IDeviceRepository': Cannot create inner bean '(inner bean)#36dce7ed' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#36dce7ed': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' available
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#36dce7ed': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' available
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' available

任何人都可以提供建议吗?

在spring boot中,您不需要使用repository注释来注释您的repository类

@Repository
您只需要在接口上扩展JPARepository,剩下的就由Spring boot来处理了。 例如:

public interface YourRepository extends JpaRepository<YourDomain, Serializable> {

    YourDomain findBysomeparameter(Long parameter);

}
除非您正在进行一些配置,否则SpringBoot会自动执行此操作


我希望这会有所帮助。

几个月前,当我开始学习Spring Boot时,我遇到了类似的问题。我不确定Spring是否认真对待目录结构。我的目录结构与@Pawan类似。您所要做的就是将存储库文件夹向上移动一级,即将其移动到父文件夹中。用@Service注释DeviceDao类。最后在@EnableJpaRepository中更新存储库路径。这对我有用。希望有此帮助。

删除
@EnableJpaRepositories、@EntityScan、@ComponentScan
注释并检查是否出现此异常。Springboot会自动进行很多配置,这些注释可能会对其产生不正确的影响。此外,您还应该在执行某些业务逻辑的类(如您的案例中的RegisterServiceImpl)上使用用户
@Service
注释
@Repository
annotation专用于负责从不同来源获取数据的类。不幸的是,它不起作用-由于项目的结构,我认为我需要显式使用3个注释。我已经添加了堆栈跟踪,您是否也可以发布
application.properties
文件?可能会有帮助。@KamilW。ServiceImpl类应该具有
@Service
而不是
@Repository
?将
应用程序
放入
com.abc
而不是
com.abc.init
。删除所有注释,但
@SpringBootApplication
除外。当我将接口更新为
公共接口idevicepository扩展了JpaRepository{}
并删除
@Repository
注释时,它仍然抛出相同的错误。至于注释-参见我对问题的最后一点评论-由于项目的结构,注释目前是必需的,但即使将
Application.java
类移动到
com.abc
包并删除注释,它仍然无法构建