0

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

项目目录结构

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");
    }
}

配置文件

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;
        }


}

应用程序.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>

项目属性

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)
4

3 回答 3

1

使用context:component-scancontext:annotation-config归档ApplicationContaxt.xml。你可以找到示例代码:

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

<context:annotation-config /> 

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

于 2017-07-27T06:32:36.283 回答
0

为什么要在路径中添加“类”?尝试

AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext("com.spring");
于 2017-07-27T07:11:54.380 回答
0

如果你的 spring 版本大于 Spring 3.1,你只能使用这种类型的配置。

您能否检查并评论您的春季版本。

于 2017-07-27T12:41:29.183 回答