Spring - @ComponentScan无法检测到bean

时间:2017-07-27 06:17:43

标签: java spring annotations component-scan

我是Spring的新手。我正在尝试使用ComponentScan。我有一个简单的bean,其字符串变量用@Component注释。尝试将@Configuration与java类而不是xml文件一起使用。当我尝试从我的主类访问bean时,它说'找不到bean'

project directory structure

StudentTest.java

   package com.spring.Annotations.tests;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;


@Component
public class StudentTest {
    @Value("${name}")
    private String name;

    public String getName() {
        return name;
    }

    public void setName(
    String name) {
        this.name = name;
    }

    public StudentTest()
    {
        System.out.println("obj created");
    }
}

Config.java

package com.spring.Annotations.tests;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;

@Configuration
@PropertySource("classpath=com.spring.Annoations.tests.project.properties")
@ComponentScan(basePackages={"com.spring.Annotations","com.spring.Annotations.tests"})

public class Config {

        @Bean
        public static PropertySourcesPlaceholderConfigurer properties() {
            PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
            return configurer;
        }


}

App.java

package com.spring.Annotations;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import com.spring.Annotations.tests.StudentTest;

public class App 
{
    public static void main( String[] args )
    {
        AnnotationConfigApplicationContext ctx=new AnnotationConfigApplicationContext("com.spring.Annotations.tests.Config.class");
        StudentTest s=(StudentTest)ctx.getBean("studentTest");
        System.out.println( s.getName() );
    }
}

的pom.xml

<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>com.spring</groupId>
  <artifactId>Annotations</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>Annotations</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <spring.version>4.0.6.RELEASE</spring.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>


        <!-- Spring 3 dependencies -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>


    </dependencies>

</project>

project.properties

name=Madhu

当我运行App.java类时,它会给我以下错误。

Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'studentTest' is defined
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:641)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1157)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:280)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:973)
    at com.spring.Annotations.App.main(App.java:17)

3 个答案:

答案 0 :(得分:0)

context:component-scancontext:annotation-config用于ApplicationContaxt.xml文件。你可以找到示例代码:

<context:component-scan annotation-config="true" base-package="com.demo.test" />

<context:annotation-config /> 

component-scan用于扫描要扫描的所有包。

答案 1 :(得分:0)

为什么要添加&#34; class&#34;到路上?尝试

AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext("com.spring");

答案 2 :(得分:0)

如果弹簧版本大于Spring 3.1,则只能使用此类配置。

您可以查看并评论春季版。