清洁代码 - 应用@Autowired应该在哪里?

时间:2017-04-02 22:08:55

标签: java spring spring-boot coding-style autowired

我将从一个简单的例子开始。你有一个Spring启动应用程序,它在初始化时运行CommandLineRunner类。

// MyCommandLineRunner.java
public class MyCommandLineRunner implements CommandLineRunner {
    private final Log logger = LogFactory.getLog(getClass());
    @Autowired //IntelliJ Warning
    private DataSource ds;
    @Override
    public void run(String... args) throws Exception {
        logger.info("DataSource: " + ds.toString());
    }
}
// Application.java
@SpringBootApplication
public class Application {
    public static void main(String... args) {
        SpringApplication.run(Application.class, args); 
    }
    @Bean
    public MyCommandLineRunner schedulerRunner() {
        return new MyCommandLineRunner();
    }
}

现在,像这样,这个工作,一切都很好。但是,IntelliJ会报告@Autowired所在的警告(我在评论中标记的位置)

  

Spring团队建议:始终在bean中使用基于构造函数的依赖注入。始终使用断言来强制依赖。

现在如果我遵循这个,我有一个基于构造函数的依赖注入

@Autowired
public MyCommandLineRunner(DataSource ds) { ... }

这也意味着我必须编辑Application.java,因为构造函数需要一个参数。在Application.java如果我尝试使用setter注入,我会得到相同的警告。如果我重构那个,我会在一些情况下得到一些令人讨厌的代码。

// MyCommandLineRunner.java
public class MyCommandLineRunner implements CommandLineRunner {
    private final Log logger = LogFactory.getLog(getClass());
    private DataSource ds;
    @Autowired // Note that this line is practically useless now, since we're getting this value as a parameter from Application.java anyway.
    public MyCommandLineRunner(DataSource ds) { this.ds = ds; }
    @Override
    public void run(String... args) throws Exception {
        logger.info("DataSource: " + ds.toString());
    }
}
// Application.java
@SpringBootApplication
public class Application {
    private DataSource ds;
    @Autowired
    public Application(DataSource ds) { this.ds = ds; }
    public static void main(String... args) {
        SpringApplication.run(Application.class, args); 
    }
    @Bean
    public MyCommandLineRunner schedulerRunner() {
        return new MyCommandLineRunner(ds);
    }
}

上面的代码会产生相同的结果,但不会在IntelliJ中报告任何警告。 我很困惑,第二代码比第一代更好?我是否遵循了错误的逻辑?这应该采用不同的方式连接吗?

简而言之,这样做的正确方法是什么?

提前致谢!

note 抱歉,如果有语法或小的逻辑错误,我在这里直接输入代码。 DataSource只是一个纯粹的例子,这个问题适用于任何自动装配的东西。

note 2 只是说MyCommandLineRunner.java不能有另一个空的构造函数,因为DataSource需要自动装配/初始化。它将报告错误,不会被编译。

2 个答案:

答案 0 :(得分:8)

有几种方法可以改善它。

  1. 您可以从@Autowired中删除MyCommandLineRunner,因为您要让@Bean方法构建它的实例。将DataSource作为参数直接注入方法。

  2. 或删除@Autowired并移除@Bean并在@Component上打一个MyCommandLineRunner注释以检测它并删除工厂方法。

  3. MyCommandLineRunner方法中的@Bean内联为lambda。

  4. MyCommandLineRunner

    中没有自动装配
    public class MyCommandLineRunner implements CommandLineRunner {
        private final Log logger = LogFactory.getLog(getClass());
        private final DataSource ds;
    
        public MyCommandLineRunner(DataSource ds) { this.ds = ds; }
    
        @Override
        public void run(String... args) throws Exception {
            logger.info("DataSource: " + ds.toString());
        }
    }
    

    和应用程序类。

    @SpringBootApplication
    public class Application {
    
        public static void main(String... args) {
            SpringApplication.run(Application.class, args); 
        }
    
        @Bean
        public MyCommandLineRunner schedulerRunner(DataSource ds) {
            return new MyCommandLineRunner(ds);
        }
    }
    

    @Component

    的用法
    @Component
    public class MyCommandLineRunner implements CommandLineRunner {
        private final Log logger = LogFactory.getLog(getClass());
        private final DataSource ds;
    
        public MyCommandLineRunner(DataSource ds) { this.ds = ds; }
    
        @Override
        public void run(String... args) throws Exception {
            logger.info("DataSource: " + ds.toString());
        }
    }
    

    和应用程序类。

    @SpringBootApplication
    public class Application {
    
        public static void main(String... args) {
            SpringApplication.run(Application.class, args); 
        }
    
    }
    

    内联CommandLineRunner

    @SpringBootApplication
    public class Application {
    
        private static final Logger logger = LoggerFactory.getLogger(Application.class)
    
        public static void main(String... args) {
            SpringApplication.run(Application.class, args); 
        }
    
        @Bean
        public MyCommandLineRunner schedulerRunner(DataSource ds) {
            return (args) -> (logger.info("DataSource: {}", ds); 
        }
    }
    

    所有这些都是构建实例的有效方法。使用哪一个,使用你觉得舒服的那个。有更多的选择(这里提到的所有变化)。

答案 1 :(得分:3)

考虑将字段ds设为最终字段,然后您不需要@Autowired。详细了解依赖注入http://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-spring-beans-and-dependency-injection.html#using-boot-spring-beans-and-dependency-injection

为了保持代码清洁,您是否考虑过使用Lombok注释? @RequiredArgsConstructor(onConstructor = @__(@Autowired)) 将使用@Autowired注释生成构造函数。在这里查看更多 https://projectlombok.org/features/Constructor.html

您的代码可能如下所示:

@Slf4j
@RequiredArgsConstructor
// MyCommandLineRunner.java
public class MyCommandLineRunner implements CommandLineRunner {

    //final fields are included in the constructor generated by Lombok
    private final DataSource ds;

    @Override
    public void run(String... args) throws Exception {
        log.info("DataSource: {} ", ds.toString());
    }
}

// Application.java
@SpringBootApplication
@RequiredArgsConstructor(onConstructor_={@Autowired}) // from JDK 8
// @RequiredArgsConstructor(onConstructor = @__(@Autowired)) // up to JDK 7
public class Application {

    private final Datasource ds;

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

    @Bean 
    public MyCommandLineRunner schedulerRunner() {
        return new MyCommandLineRunner(ds);
    }
}

稍后修改

没有Lombok的解决方案依赖于Spring在创建bean时注入依赖

@SpringBootApplication
public class Application {

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

    @Bean
    /**
     * dependency ds is injected by Spring
     */
    public MyCommandLineRunner schedulerRunner(DataSource ds) {
        return new MyCommandLineRunner(ds);
    }
}

// MyCommandLineRunner.java
public class MyCommandLineRunner implements CommandLineRunner {
    private final Log logger = LogFactory.getLog(getClass());

    private final DataSource ds;

    public MyCommandLineRunner(DataSource ds){
        this.ds = ds;
    }

    @Override
    public void run(String... args) throws Exception {
        logger.info("DataSource: "+ ds.toString());
    }
}