使用基于java的批注的弹簧汽车电线豆

问题描述:

我正试图做一个新的简单的Spring启动应用程序演示依赖注入。我想用@Autowired注解导入bean。使用基于java的批注的弹簧汽车电线豆

这是我的代码样品片

---- ---- Example.class

package com.example.project; 


import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.boot.*; 
import org.springframework.boot.autoconfigure.*; 
import org.springframework.web.bind.annotation.*; 

@RestController 
@EnableAutoConfiguration 
public class Example { 


    @Autowired 
    public myBean first; 

    @RequestMapping("/") 
    String home() { 
     return "Hello World!"; 
    } 

    public static void main(String[] args) throws Exception { 
     SpringApplication.run(Example.class, args); 
    } 

} 

---- ---- myBean.class

package com.example.project; 

public class myBean { 

    myBean() 
    { 
     System.out.println("Hi myBean Constructed"); 
    } 
} 

--- BeanConfiguration.class ---

package com.example.project; 

import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.context.annotation.ComponentScan; 

@Configuration 
@ComponentScan(basePackages = "com.example.project") 
public class BeanConfigurationClass { 

    @Bean 
    public myBean getBean() 
    { 
     return new myBean(); 

    } 
} 

--pom.xml ---

<?xml version="1.0" encoding="UTF-8"?> 
<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> 

    <groupId>example</groupId> 
    <artifactId>1</artifactId> 
    <version>1.0-SNAPSHOT</version> 

    <parent> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-parent</artifactId> 
     <version>1.4.2.RELEASE</version> 
    </parent> 
    <build> 
     <plugins> 
      <plugin> 
       <groupId>org.springframework.boot</groupId> 
       <artifactId>spring-boot-maven-plugin</artifactId> 
       <version>1.2.5.RELEASE</version> 
       <executions> 
        <execution> 
         <goals> 
          <goal>repackage</goal> 
         </goals> 
        </execution> 
       </executions> 
      </plugin> 
     </plugins> 
    </build> 
    <dependencies> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-web</artifactId> 
     </dependency> 
    </dependencies> 
</project> 

但是当我尝试运行应用程序是无法找到豆和第一提供了以下错误 场com.example.project.Example需要一个bean无法找到类型为“com.example.project.myBean”的类型。

我也尝试使用基于xml的配置,但面临同样的错误。 这里有什么根本性的错误。

感谢您的期待。

+0

尝试添加公共访问器构造为myBean,从控制器中删除@EnableAutoConfiguration和移动BeanConfigurationClass – kuhajeyan

  1. @EnableAutoConfiguration从控制器移动到您的主应用程序类。
  2. 摆脱BeanConfigurationClass
  3. 添加春季DI类注释的一个上myBean类:

    @Component 
    public class myBean { 
    
        myBean(){ 
         System.out.println("Hi myBean Constructed"); 
        } 
    } 
    
+0

主应用程序类和控制器类是相同的。我需要让他们与众不同吗?如果它们是不同的,我想我只是需要从我的主类呼叫控制器类对象,请纠正我,如果我错了, –

+0

这不是有效的答案。你是什​​么意思摆脱BeanConfigurationClass?也为你的答案添加解释。 – ScanQR

+0

从上面的方法我得到以下错误: 无法启动嵌入式容器;嵌套的例外是org.springframework.context.ApplicationContextException:无法启动EmbeddedWebApplicationContext由于缺少EmbeddedServletContainerFactory豆。 –

类的名称必须以大写字母开始!必须是“我的豆” ..

你应该尽量做到以下,

移动@EnableAutoConfiguration从控制器到您的主应用程序类,即BeanConfigurationClass。这是因为BeanConfigurationClass是您的配置文件,并且所有与配置相关的注释都应放置在此位置。

也可以将myBean重命名为MyBean并用@Component注释进行注释。

编辑:从您的BeanConfigurationClass删除以下注释,并只添加这一个@SpringBootApplication它会照顾所有删除的人。

@Configuration 
@ComponentScan 
@EnableAutoConfiguration 

添加为,

@SpringBootApplication 
BeanConfigurationClass 
+0

嘿,我试过但没有运气。 我收到以下错误: 无法启动嵌入容器;嵌套的例外是org.springframework.context.ApplicationContextException:无法启动EmbeddedWebApplicationContext由于缺少EmbeddedServletContainerFactory豆。 –

+0

@GauravJuneja确保您已将相关的依赖项添加到您的构建文件中。 – ScanQR

+0

BeanConfigurationClass中的MyBean方法应该被称为Bean或组件? 也MyBean.class应该注释的东西? –