In the last blog post, we explained how to automate assembly beans using component scanning and autowire, which is the best way to use them.

However, in some cases, we may not be able to use the autowire feature and have to explicitly configure the bean.

For example, if we reference a third party library and need to assemble a class from that library into a project, we can’t add the @Component annotation to that class, so we can’t use autowiring.

There are two ways to explicitly configure beans in Spring:

  1. Configure beans through JavaConfig
  2. Configure beans through XML

This blog focuses on the implementation of configuration beans through JavaConfig, and the implementation of configuration beans through XML will bea separate blog post later.

We’ll still use the examples from the last blog post, but the code will be modified as appropriate.

package chapter02.javaconfig;

public interface CompactDisc {
    void play(a);
}
Copy the code
package chapter02.javaconfig;

public class SgtPeppers implements CompactDisc {

    @Override
    public void play(a) {
        String title = "Sgt.Pepper's Lonely Hearts Club Band";
        String artists = "The Beatles";
        System.out.println("Playing " + title + " By "+ artists); }}Copy the code
package chapter02.javaconfig;

public class CDPlayer {

    private CompactDisc compactDisc;

    public CDPlayer(CompactDisc compactDisc) {
        this.compactDisc = compactDisc;
    }

    public void play(a) { compactDisc.play(); }}Copy the code

Note: compared to the last blog post, we removed the @component annotation from the SgtPeppers and CDPlayer classes.

1. Create a configuration class

package chapter02.javaconfig;

import org.springframework.context.annotation.Configuration;

@Configuration
public class CDPlayerConfig {}Copy the code

2. The bean declaration

In JavaConfig, we use the @bean annotation to declare beans as follows:

package chapter02.javaconfig;

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

@Configuration
public class CDPlayerConfig {
    @Bean
    public CompactDisc sgtPeppers(a) {
        return newSgtPeppers(); }}Copy the code

The default generated bean ID is the same as the method name, sgtPeppers, but we can customize it:

@Bean(name = "lonelyHeartsClub")
public CompactDisc sgtPeppers(a) {
     return new SgtPeppers();
}
Copy the code

The bean declared above is relatively simple and does not have any other dependencies, but some complex beans, such as CDPlayer, depend on CompactDisc. How do we declare this?

An easy way to do this is to use the sgtPeppers() method just defined as a parameter dependency for the CDPlayer constructor:

@Bean
public CDPlayer cdPlayer(a) {
    return new CDPlayer(sgtPeppers());
}
Copy the code

However, it is more recommended to make the dependency an argument to the bean method and Spring will automatically match the parameter dependency:

@Bean
public CDPlayer cdPlayer(CompactDisc compactDisc) {
    return new CDPlayer(compactDisc);
}
Copy the code

The code for the configuration class is:

package chapter02.javaconfig;

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

@Configuration
public class CDPlayerConfig {
    @Bean
    //@Bean(name = "lonelyHeartsClub")
    public CompactDisc sgtPeppers(a) {
        return new SgtPeppers();
    }

    /*@Bean public CDPlayer cdPlayer() { return new CDPlayer(sgtPeppers()); } * /

    @Bean
    public CDPlayer cdPlayer(CompactDisc compactDisc) {
        return newCDPlayer(compactDisc); }}Copy the code

3. Verify that the bean is successfully assembled

Create test class CDPlayerTest:

package chapter02.javaconfig;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class CDPlayerTest {
    public static void main(String[] args) {
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(CDPlayerConfig.class);
        CompactDisc compactDisc = (SgtPeppers) applicationContext.getBean("sgtPeppers"); compactDisc.play(); CDPlayer cdPlayer = applicationContext.getBean(CDPlayer.class); cdPlayer.play(); }}Copy the code

Running results:

As you can see from the run results, the bean assembly is successful.

4. Source code and reference

Source code address: github.com/zwwhnly/spr… Welcome to download.

Craig Walls: Spring In Action (4th Edition)