Intellij idea - 有什么工具可以将 xml bean 转换为 spring 的自动注释?

Intellij idea - any tool to transform xml beans to autowiing annotations for spring?

我是 spring 的新手,正在尝试自学。 我想知道是否有任何工具可以将 xml beans 转换为自动装配注释。

非常感谢如何使用示例手动执行此操作(除了设置)。

我不认为存在任何工具,也许我错了.. 如果我很清楚你想混合 xml 和 java。如果是,你应该在 xml 中包含 bean,并直接在 bean 配置文件中包含 AutowiredAnnotationBeanPostProcessor。然后你可以通过 @Autowired 自动装配 bean,它可以应用于 setter 方法、构造函数或字段。

在我看来,最好从注释开始学习,因为目前大多数使用带注释的 spring 的公司只有旧项目包含 xml 配置。我知道什么都知道很好 :)

--> 好吧,如果你想将配置移动到注解,你可以用简单的方式做到这一点

第一次创建配置 class

@Configuration  
@ComponentScan 
public class SampleConfiguration {...}

然后像下面这样创建 bean


@Bean
public DataSource getDataSource() {
    // here you can use BasicDataSource, DataSourceBuilder, DriverManagerDataSource or other class 
    // for example
    BasicDataSource dataSource = new BasicDataSource();
    dataSource.setDriverClassName(...);
    dataSource.setUrl(...);
    dataSource.setUsername(...);
    dataSource.setPassword(...);
    return dataSource;
}

@Bean
public JdbcTemplate getJdbcTemplate(DataSource dataSource) {
    JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
    jdbcTemplate.setResultsMapCaseInsensitive(true);
    return jdbcTemplate;
}

另一种创建 bean 的方法是使用注释 @Component@Repository@Service@Controller 创建 class,但要确保主要 class 是目录的最高层级或仅在 @ComponentScan 基本包中提供:)