I am new to spring and trying to teach myself. I wonder if there is any tool to transform xml beans to autowiring annotations .
How to do that manually ( apart from setup) with example is really appreciated.
I don't think so that any tool exist, maybe I'm wrong..
If I well understand you want to mix xml and java. If yes you should have beans inside xml and include AutowiredAnnotationBeanPostProcessor
directly in bean configuration file. And then you could be able autowired bean via @Autowired
, and it can be applied on setter method, constructor or a field.
In my opinion better start learning with annotation because currently most of companies using spring with annotation only old projects contain xml configuration. I know is good to know everything :)
--> well If you want to move configuration to annotation you can do it in simply way
1st create configuration class
@Configuration
@ComponentScan
public class SampleConfiguration {...}
then create beans like below
@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;
}
another way of creating beans is creating class with annotation @Component
, @Repository
, @Service
, @Controller
but be sure the main class is the highest hierarchy of directory or just provide in @ComponentScan
base package :)