0

I'm new to Spring. I'm trying to use ComponentScan.I have a simple bean with a string variable annotated with @Component. Trying to use the @Configuration with a java class instead of xml file. When I try to access the bean from my main class, it says 'No bean found'

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

When I run App.java class it gives me following error.

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 Answers4

1

use context:component-scan and context:annotation-configinto ApplicationContaxt.xml file. you can find the sample code :

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

<context:annotation-config /> 

component-scan is use for scanning all packages to scan.

Mayank Sharma
  • 403
  • 2
  • 2
  • I wanted to test java equivalent of ApplicationContext.xml. So i did this. Do i need to add these lines again in ApplicationContext ? Then whats the use of java Equivalent of ApplicaionContext. – Madhu Kumar Tallapalli Jul 27 '17 at 06:36
0

Why do yo add "class" to the path? Try

AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext("com.spring");
Andriy Slobodyanyk
  • 1,965
  • 14
  • 15
0

You can only use this type of configuration if your spring version is greater than Spring 3.1.

Can you please check and comment your spring version.

0

AnnotationConfigApplicationContext ctx=new AnnotationConfigApplicationContext(com.spring.Annotations.tests.Config.class);

Remove double quotes inside AnnotationConfigApplicationContext and try

Bhagya
  • 1
  • 1