The bean that invokes the XML file

Source: Internet
Author: User

Actest.class
 Packagecom.zyz.db;ImportCom.zyz.dao.PersonDao;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;ImportOrg.springframework.context.support.FileSystemXmlApplicationContext;ImportOrg.springframework.jdbc.datasource.DriverManagerDataSource;/*** Created by Zyz on 2016-8-5.*/ Public classActest { Public Static voidMain (string[] args) {//Method 1: Use FilesystemxmlapplicationcontextApplicationContext ac1=NewFilesystemxmlapplicationcontext ("Src/main/webapp/web-inf/applicationcontext-jdbc.xml"); Drivermanagerdatasource DataSource= (Drivermanagerdatasource) ac1.getbean ("DataSource"); System.out.println (Datasource.getusername ());//Method 2: Use ClasspathxmlapplicationcontextApplicationContext ac2=NewClasspathxmlapplicationcontext ("Beans.xml"); Persondao Persondao= (Persondao) ac2.getbean ("Persondao");
System.out.println (Persondao.getversion ());
System.out.println (Persondao.getpersons (). Get (0). GetName ());} }

Method 1: Simply specify the relative position of the XML file in the project

Method 2: Read the XML file from the Classpath location, where the compiled class file is stored.
You can put the XML file into the Resources folder and then add the Resources folder to Classpath, in idea:
"File", "Project Structure", the dialog box appears, add Resources folder, the system will copy the XML file into the classes folder after compiling and running

Applicationcontext-jdbc.xml
<?XML version= "1.0" encoding= "UTF-8"?>    <Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:jdbc= "Http://www.springframework.org/schema/jdbc"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd Http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd ">        <!--Configuring the MySQL database -        <BeanID= "DataSource"class= "Org.springframework.jdbc.datasource.DriverManagerDataSource">            < Propertyname= "Driverclassname">                <value>Com.mysql.jdbc.Driver</value>            </ Property>            < Propertyname= "url">                <value>Jdbc:mysql://localhost:3306/school?useunicode=true&amp;Characterencoding=utf-8</value>            </ Property>            < Propertyname= "username">                <value>Root</value>            </ Property>            < Propertyname= "Password">                <value>Root</value>            </ Property>        </Bean>        <!--JdbcTemplate and Database Association -        <BeanID= "JdbcTemplate"class= "Org.springframework.jdbc.core.JdbcTemplate">            < Propertyname= "DataSource"ref= "DataSource"/>        </Bean>    </Beans>

Beans.xml
<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" >    <BeanID= "Persondao"class= "Com.zyz.dao.PersonDao">        < Propertyname= "Version">            <value>3.85</value>        </ Property>    </Bean></Beans>

The bean that invokes the XML file

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.