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&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