【问题标题】:Constructor-Based Dependency Injection within SpringSpring中基于构造函数的依赖注入
【发布时间】:2021-08-14 17:30:03
【问题描述】:

我有这门课:

@Service
    
public class DogUserService  {
    
         
private final ManagerService managerService;
...
}

在我的 context.xml 上:

   <bean id="managerService"
        class="com.dogs.impl.services.ManagerService" />

但是当我运行应用程序时。我有这个错误:

    rvice.class]: Unsatisfied dependency expressed through constructor parameter 1; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: 
No qualifying bean of type 'com.dogs.impl.services.ManagerService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency a
    nnotations: {}

【问题讨论】:

    标签: java spring spring-mvc inversion-of-control ioc-container


    【解决方案1】:

    您需要使用@Bean 注解将 bean 提供给 WebApplicationContext。我已经从 @Configuration bean 中的 context.xml 创建了 managerService bean,并使用 @Bean 方法使 bean 在 webApplicationContext 中可用。这个将 managerService 自动连接的 UserService 类具有此配置 bean 的属性 @ConditionalOnBean。

    以下是我的实现方式。

    应用类

    @SpringBootApplication public class DemoApplication
    {
    
        public static void main(String[] args)
        {
            SpringApplication.run(DemoApplication.class, args);
        }
    
    }
    

    用户服务

    @Service
    @ConditionalOnBean({EnableConfiguration.class})
    public class UserService
    {
        @Autowired
        private ManagerService managerService;
        
        public String run() {
            System.out.println("Going to Run from manager Service");
            return managerService.run();
        }
    
    }
    

    启用配置

    @Configuration
    public class EnableConfiguration implements InitializingBean
    {
    
        ManagerService managerService;
    
        @Bean
        public ManagerService mngrService() {
            return managerService;
        }
    
        @Override public void afterPropertiesSet() throws Exception
        {
            ApplicationContext ctx = new ClassPathXmlApplicationContext( "context.xml" );
            managerService = (ManagerService) ctx.getBean("managerService");
            System.out.println("manager bean:" + managerService.toString());
            mngrService();
    
        }
    }
    

    ManagerService

    public class ManagerService
    {
        private int a;
        private int b;
    
        public ManagerService(int a, int b) {
            this.a = a;
            this.b = b;
        }
    
        public String run() {
            return this.toString();
        }
    
        @Override public String toString()
        {
            return "ManagerService{" + "a=" + a + ", b=" + b + '}';
        }
    }
    

    服务控制器

    @RestController
    public class ServiceController
    {
        @Autowired
        private UserService userService;
    
        @GetMapping("/get")
        public String test() {
            return userService.run();
        }
    }
    

    context.xml

        <bean id="managerService" class="com.example.demo.ManagerService">
            <constructor-arg index="0" value="2"/>
            <constructor-arg index="1" value="5"/>
        </bean>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-21
      • 2011-09-02
      • 1970-01-01
      • 2011-02-02
      • 2018-06-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多