1

Can you please provide me with an example of primitive type dependency injection in spring boot. I have tried once but TestConfiguration which is my custom bean definition class does not detect or does not recognize by spring boot application. here is my code,

//Engine class

package com.test2.projectTest;

import org.springframework.stereotype.Component;

@Component
public class Engine {

private String msg;

public String getMsg() {
    return msg;
}

public void setMsg(String msg) {
    this.msg = msg;
}
}

//Test Configuration

package com.test2.projectTest;

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

@Configuration
public class TestConfiguration {

@Bean("engine")
public Engine engine(){
    Engine engine = new Engine();
    engine.setMsg("Message is injected");
    return engine;
}
}

//spring main application

package com.test2.projectTest;

import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
public class ProjectTestApplication {

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

//JUnit Test

package com.test2.projectTest;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.context.ApplicationContext;
import 
org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class ProjectTestApplicationTests {

@Test
public void contextLoads() {

    ApplicationContext apc = new 
AnnotationConfigApplicationContext(TestConfiguration.class);
    Engine e = (Engine) apc.getBean("engine");
    e.getMsg();
}
}

// Output - org.springframework.beans.factory.NoSuchBeanDefinitionException: 
No bean named 'engine' available

Please suggest any solution to above issue

  • 1
    What does this have to do with primitive types? – Todd Jan 11 '18 at 14:02
  • and why are you creating two contexts, one with spring runner, another with annotationconfigapplicationcontext? how does it work in your actual app, outside of tests? – eis Jan 11 '18 at 14:05
  • i'm finding how to inject value into msg property in Engine class which is primitive type using spring boot. Any suggestion?. – Farhan Dulip Jan 11 '18 at 14:05
  • you have a problem with creating spring configuration. your error has nothing to do with value injection. Try to fix your configuration first, so you would be able to create a bean. – eis Jan 11 '18 at 14:06
  • Any ways can you please correct the above code for me please? – Farhan Dulip Jan 11 '18 at 14:06
  • 1
    You could start with the tutorial: https://spring.io/guides/gs/spring-boot/ – eis Jan 11 '18 at 14:10
  • what does the link you provided?. It is redirecting me the same post that i posted. – Farhan Dulip Jan 11 '18 at 14:31

1 Answers1

0

Add @componentscan annotation in main class and provide engine class package and it should work

riteshmaurya
  • 166
  • 1
  • 11