loc 是控制反转,是一个概念。当前比较流行的实现方式有两种:一种是依赖查找。第二就是依赖注入;
依赖注入是目前最优秀的解耦方式;
第一个小程序:
所需的jar包:spring -beans 4.2jar spring context 4.2 jar pring -core 4.2jar pring -expression 4.2jar这些都是在
官方sprng relese jar包中
依赖库:spring -framework -3.02.release -dependencies。zip解压目录下\org.apache.commons\com.springsource.org.apache.commons.logging\1.1.1下的com.springsource.org.apache.commons.logging-1.1.1.jar文件
//定义接口和实体类
public interface IStudentservice{
void some();
}
public class SomeServiceImpl implements ISomeService {
// private int a = 5;
// 一个没有成员变量的对象在堆内存中占有8个字节
// Object obj = new Object();
// 动态代码块
/*{
System.out.println("执行动态代码块 a = " + a);
}*/
public SomeServiceImpl() {
System.out.println("执行无参构造器");
}
@Override
public void doSome() {
System.out.println("执行doSome()方法");
}
}
测试类
import org.junit.Test;
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.ClassPathResource;
import com.bjpowernode.service.ISomeService;
import com.bjpowernode.service.SomeServiceImpl;
public class MyTest {
@Test
public void test01() {
ISomeService service = new SomeServiceImpl();
service.doSome();
}
@Test
public void test02() {
// 创建容器对象,加载Spring配置文件
// 会从类路径下查找配置文件
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
ISomeService service = (ISomeService) ac.getBean("myService");
service.doSome();
}
@Test
public void test03() {
// 创建容器对象,加载Spring配置文件
// 会从项目的根下查找配置文件
ApplicationContext ac = new FileSystemXmlApplicationContext("applicationContext.xml");
ISomeService service = (ISomeService) ac.getBean("myService");
service.doSome();
}
@Test
public void test04() {
// 创建容器对象,加载Spring配置文件
// 会从当前文件系统的D盘根目录下查找配置文件
ApplicationContext ac = new FileSystemXmlApplicationContext("d:/applicationContext.xml");
ISomeService service = (ISomeService) ac.getBean("myService");
service.doSome();
}
// ApplicationContext与BeanFactory容器的区别:
// 这两上容器对于其中Bean的创建时机不同:
// 1)ApplicationContext容器在进行初始化时,会将其中的所有Bean(对象)进行创建
// 缺点:占用系统资源(内存、CPU等)
// 优点:响应速度快
// 2)BeanFactory容器中的对象,在容器初始化时并不会被创建,而是在真正获取该对象时才被创建
// 缺点:相对来说,响应速度慢
// 优点:不多占用系统资源
@Test
public void test05() {
// 创建BeanFactory容器
BeanFactory bf = new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));
ISomeService service = (ISomeService) bf.getBean("myService");
service.doSome();
}
}
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">
<!-- 注册Service
SomeServiceImpl myService = new SomeServiceImpl();
-->
<bean id="myService" class="com.bjpowernode.service.SomeServiceImpl"/>
</beans>