![在这里插入图片描述](https://img-blog.csdnimg.cn/20200906095245776.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQzMTQxNjEx,size_16,color_FFFFFF,t_70#pic_center)
TestDao
package dao;
public interface TestDao {
public void sayHello();
}
TestDaoImpl
package dao;
public class TestDaoImpl implements TestDao {
@Override
public void sayHello() {
System.out.println("Hello, Study Hard!");
}
}
applicationContext.xml
<?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">
<!--将指定类TestDaoImpl配置给Spring,让Spring创建其实例-->
<bean id="test" class="dao.TestDaoImpl">
<!-- collaborators and configuration for this bean go here -->
</bean>
</beans>
Test(使用ClassPathXmlApplicationContext)
package test;
import dao.TestDao;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
TestDao testDao = (TestDao) applicationContext.getBean("test");
testDao.sayHello();
}
}
Test(使用BeanFactory)
package test;
import dao.TestDao;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;
public class Test {
public static void main(String[] args) {
BeanFactory beanFactory = new XmlBeanFactory(new FileSystemResource("C:/Users/SunLionAhh/IdeaProjects/SSM-learn/spring001/src/applicationContext.xml"));
TestDao testDao = (TestDao) beanFactory.getBean("test");
testDao.sayHello();
}
}
Test(使用FileSystemXmlApplicationContext)
package test;
import dao.TestDao;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.FileSystemResource;
public class Test {
public static void main(String[] args) {
ApplicationContext applicationContext = new FileSystemXmlApplicationContext("C:\\Users\\SunLionAhh\\IdeaProjects\\SSM-learn\\spring001\\src\\applicationContext.xml");
TestDao testDao = (TestDao) applicationContext.getBean("test");
testDao.sayHello();
}
}
通过Web服务器实例化ApplicationContext容器,以下是web.xml(此方法本人还没怎么弄明白)
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<context-param>
<!--加载src目录下的applicationContext.xml-->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!--指定以ContextLoaderListener方式启动Spring容器-->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
</web-app>
![在这里插入图片描述](https://img-blog.csdnimg.cn/2020090620583059.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQzMTQxNjEx,size_16,color_FFFFFF,t_70#pic_center)