In order to reduce the configuration in XML in spring, you can create a configuration class (for example, exampleconfiguration) to configure the bean.
1. Configure the spring configuration file to enable Java annotations
<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:batch= "Http://www.springframework.org/schema/batch" xmlns: context= "Http://www.springframework.org/schema/context" xmlns:jdbc= "Http://www.springframework.org/schema/jdbc" xsi:schemalocation= "Http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/ Spring-batch-2.1.xsdhttp://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/ Spring-jdbc-3.0.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-3.0.xsdhttp://www.springframework.org/schema/context Http://www.springframework.org/schema/context /spring-context-3.0.xsd "><context:component-scan base-package=" Com.shanhy.demo "/></beans>
2. Define a configuration class
@Configurationpublic class Exampleconfiguration {@Value ("${batch.jdbc.driver}") Private String driverclassname;@ Value ("${batch.jdbc.url}") private string Driverurl; @Value ("${batch.jdbc.user}") private string driverusername;@ Value ("${batch.jdbc.password}") private String Driverpassword; @Bean (name = "DataSource") Public DataSource DataSource ( {Basicdatasource DataSource = new Basicdatasource ();d atasource.setdriverclassname (driverclassname); Datasource.seturl (Driverurl);d atasource.setusername (driverusername);d Atasource.setpassword (DriverPassword); return dataSource;} @Beanpublic Platformtransactionmanager TransactionManager () {return new Datasourcetransactionmanager (DataSource ());}}
3. Description
@configuration annotated class, equivalence and XML configuration beans; The @bean annotation method is equivalent to the bean configured in XML.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Java-based configuration @configuration and @bean usage in spring