spring core container ApplicationContext

//bean.xml profile
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="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">
<!--把对象的创建交给spring管理-->
<bean id="accountService" class="com.hope.service.impl.AccountServiceImpl"/>
<bean id="accountDao" class="com.hope.dao.impl.AccountDaoImpl"/>

</beans>


package com.hope.ui;


import com.hope.dao.IAccountDao;
import com.hope.service.IAccountService;
import org.springframework.context.ApplicationContext;
org.springframework.context.support.ClassPathXmlApplicationContext Import ;

/ **
* @author newcityman
* @date 2019/11/18 - 14:23
* simulate a presentation layer frame
* /
public class Client {
/ **
* Get the ioc spring containers, obtained according to the object id
* three interface implementation class ApplicationContext
* 1, ClassPathXmlApplicationContext: it can load the configuration file class path, requires the class file must be arranged below the path. Not the case, could not be loaded
* 2, FileSystemXmlApplicationContext: it can load the configuration file in the disk any path (must have access to)
* 3, AnnotationConfigApplicationContext: it is to read the annotation to create the container method
* @param args * / public static void main (String [] args) { //. 1, obtaining the container object core ApplicationContext ac =



new ClassPathXmlApplicationContext("bean.xml");
//2、根据id获取bean对象
IAccountService as=(IAccountService)ac.getBean("accountService");
IAccountDao ad = ac.getBean("accountDao",IAccountDao.class);
System.out.println(as);
System.out.println(ad);

}
}

Guess you like

Origin www.cnblogs.com/newcityboy/p/11886485.html