Home > Net >  How do I Configure a Spring Project without Xml file and only using Java?
How do I Configure a Spring Project without Xml file and only using Java?

Time:08-19

I am currently learning Spring Framework.

I have Created 1 Bean Object (Class and Interface) and 1 Dependency (Interface and Class). When I try to Configure Spring Beans without a xml file and with Pure Java class the below Error shows up:

Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'swimCoach' available
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:863)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1344)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:309)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:213)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1160)
    at com.mandeep.spring.JavaConfigApp.main(JavaConfigApp.java:14)

The Spring Configuration Class "SportConfig.java"is as below:

package com.mandeep.spring;


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

@Configuration
//@ComponentScan("com.mandeep.spring")
public class SportConfig {
    
    //Define a Method for SadFortuneService
    @Bean
    public FortuneService sadFortuneService() {
        return new SadFortuneService();
    }
    
    //Define a Method for SwimCoach and Inject Dependency
    @Bean
    public Coach swimCoach() {
        return new SwimCoach(sadFortuneService());
    }
}

The Main Function Class "JavaConfigApp.java" is as below:

package com.mandeep.spring;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class JavaConfigApp {

    public static void main(String[] args) {
        
        //Read Spring Config. File
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext("SportConfig.class");
        
        
        //Get Bean From Spring Container
        Coach theCoach = context.getBean("swimCoach",Coach.class);
        
        //Call Method from the Bean
        System.out.println(theCoach.getDailyWorkout());
        
        System.out.println(theCoach.getDailyFortune());
        
        
        
        //Close the Context
        context.close();        
    }

}

The "SwimCoach.java" class implements Coach interface and is as below:

package com.mandeep.spring;


public class SwimCoach implements Coach {

    private FortuneService fortuneService;
    
    public SwimCoach(FortuneService fortuneService) {
        super();
        this.fortuneService = fortuneService;
    }

    @Override
    public String getDailyWorkout() {
        
        return "Swim For 100 rounds";
    }

    @Override
    public String getDailyFortune() {
        
        return fortuneService.getFortune();
    }

    @Override
    public String getEmail() {
        
        return null;
    }

}

The Dependency class "SadFortuneService.java" implements FortuneService interface and is as below:

package com.mandeep.spring;


public class SadFortuneService implements FortuneService {

    @Override
    public String getFortune() {
        
        return "Today is your Bad Luck";
    }

}

CodePudding user response:

Try scanning package instead:

AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.scan("com.mandeep.spring");

CodePudding user response:

Try to use component class to construct your context

AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SportConfig.class);

When use basePackages, we should add full path to the packages to scan.

  • Related