1

I have a spring bean class with a constructor with multiple parameters and @Inject annotation.
Is there a way to use spring Java configuration class to create a bean for the class without actually writing code for creating the object? Something like using @Bean on a field?

@Bean(MyClassName.class) private MyInterfaceName myBean;

Or maybe by making the configuration class abstract and the bean method abstract, like:

@Bean(MyClassName.class) abstract MyInterfaceName myBean();

It's quite annoying (and pointless) to write each time the whole method that only creates a new object if you know that you only have 1 implementation of the class and you want to use auto wiring and constructor injection.

StanislavL
  • 56,971
  • 9
  • 68
  • 98
mike27
  • 965
  • 3
  • 12
  • 22
  • I just want to add that I want to use construction injection, I don't like field auto wiring. and also I don't want to use classpath scanning to look for beans, I want to have a Java configuration class where all the beans are explicitly created, I just don't want to write code that actually creates each object. – mike27 Feb 16 '17 at 11:03

2 Answers2

0

You can use @Component annotation. According to Spring documentation:

@Component indicates that an annotated class is a "component". Such classes are considered as candidates for auto-detection when using annotation-based configuration and classpath scanning.

0

Use the @Component annotation.

Indicates that an annotated class is a "component". Such classes are considered as candidates for auto-detection when using annotation-based configuration and classpath scanning. Other class-level annotations may be considered as identifying a component as well, typically a special kind of component: e.g. the @Repository annotation or AspectJ's @Aspect annotation.

Here is an example:

import org.springframework.stereotype.Component;

@Component
public class CustomerDAO
{
    @Override
    public String toString() {
        return "Hello , This is CustomerDAO";
    }
}

A DAO class:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class CustomerService
{
    @Autowired
    CustomerDAO customerDAO;

    @Override
    public String toString() {
        return "CustomerService [customerDAO=" + customerDAO + "]";
    }
}

And a runner class:

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App
{
    public static void main( String[] args )
    {
        ApplicationContext context =
           new ClassPathXmlApplicationContext(new String[] {"Spring-AutoScan.xml"});

        CustomerService cust = (CustomerService)context.getBean("customerService");
        System.out.println(cust);

    }
}

And your output:

CustomerService [customerDAO=Hello , This is CustomerDAO]
Keith
  • 3,079
  • 2
  • 17
  • 26
  • You can use `@Service`, `@Repository` annotation for service and dao layer respectively. These are same as `@Component`, To know diff, refer http://stackoverflow.com/questions/6827752/whats-the-difference-between-component-repository-service-annotations-in#answer-6897038 – Ramanujan R Feb 16 '17 at 10:53