我测试了我的春季启动项目作为一个单独的maven项目,一切正常。 我决定将我的应用程序拆分为子模块。 repository,serive和spring boot web模块。自动装配停止工作。 并发生以下错误:
20:12:08.052 [main] WARN o.s.b.c.e.AnnotationConfigEmbeddedWebApplicationContext - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userRestController': Unsatisfied dependency expressed through field 'userService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.plendo.service.interfaces.UserService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
20:12:08.068 [main] WARN o.s.boot.SpringApplication - Error handling failed (Error creating bean with name 'delegatingApplicationListener' defined in class path resource [org/springframework/security/config/annotation/web/configuration/WebSecurityConfiguration.class]: BeanPostProcessor before instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration': Initialization of bean failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'org.springframework.context.annotation.ConfigurationClassPostProcessor.importRegistry' available)
20:12:08.243 [main] ERROR o.s.b.d.LoggingFailureAnalysisReporter - ``
***************************
APPLICATION FAILED TO START
***************************
Description:
Field userService in org.plendo.ui.controller.UserRestController required a bean of type 'org.plendo.service.interfaces.UserService' that could not be found.
答案 0 :(得分:1)
项目结构:包含jpa模块,服务模块和Web模块(spring boot app)的父项目
spring boot应用程序类:web模块
package org.plendo.ui;
@SpringBootApplication public class App { public static void main(String[] args) {
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
RestController类:web模块
package org.plendo.ui.controller;
import org.plendo.jpa.entity.User;
import org.plendo.service.interfaces.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
//@ComponentScan("org.plendo.service.interfaces")
public class UserRestController {
@Autowired
//@Qualifier("userService")
private UserService userService;
@RequestMapping(value = "/saveUser")
public User saveUser(User u) {
return userService.saveUser(u);
}
}
UserServiceImpl:服务模块
import org.apache.log4j.Logger;
import org.plendo.jpa.entity.User;
import org.plendo.jpa.entity.UserRepository;
import org.plendo.service.interfaces.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl implements UserService {
private Logger logger = Logger.getLogger(this.getClass());
@Autowired
UserRepository userRepository;
@Override
public User saveUser(User u) {
return userRepository.save(u);
}
}
UserService接口:服务模块
package org.plendo.service.interfaces;
import org.plendo.jpa.entity.User;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
public interface UserService {
public User saveUser(User u);
}
UserRepository接口:jpa模块
package org.plendo.jpa.entity;
import org.springframework.data.jpa.repository.JpaRepository;
public interface UserRepository extends JpaRepository<User, String>{
}
还有其他事可做吗?