Java-based configuration @configuration and @bean usage in spring

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.