spring艳 2020-06-13
IoC:Inversion of Control,控制反转、依赖注入
把一个java bean交给spring管理,有三步骤:
(1)创建xml格式的配置文件
(2)将一个Bean交由Spring创建并管理
(3)获取Spring上下文
<?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"> <!--将一个Bean交由Spring创建并管理--> <bean id="bean" class="com.huawei.spring.ioc.Bean" /> </beans>
import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class JunitTestDemo { @Test public void test() { ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml"); Bean bean = context.getBean("bean", Bean.class); System.out.println("bean = " + bean); } }