1、IOC是什么?
IOC (inverse of controll)控制反转:所谓控制反转就是把创建对象(bean),和维护对象(bean)的关系的权利从程序中转移到spring的容器(applicationContext.xml)
2、DI是什么?
Di(dependency injection)依赖注入:实际上DI和IOC是同一个概念,spring设计者认为DI更能准确表示spring的核心技术
笔者认为:学习框架最重要的就是学习各种配置
3、传统的方法和使用spring的方法区别
使用spring,没有new对象,我们把new对象的任务交给spring框架
4、第二个spring项目
结构层
源码:UserService.java
package UserService;
import ByService.ByService;
public class UserService {
private String name;
private ByService byservice;
public void setByservice(ByService byservice) {
this.byservice = byservice;
}
public void setName(String name) {
this.name = name;
}
public void say() {
System.out.println("你好:" + name);
byservice.sayBye();
}
}
源码:ByService.java
package ByService;
public class ByService {
private String name;
public void setName(String name) {
this.name = name;
}
public void sayBye()
{
System.out.println("Bye"+name);
}
}
源码:applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
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 id="UserService" class="UserService.UserService">
<property name="name" value="小明" />
<property name="byservice" ref="ByService" />
</bean>
<bean id="ByService" class="ByService.ByService">
<property name="name" value="小紅"/>
</bean>
</beans>
源码:Test.java
package Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import UserService.UserService;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
UserService u=(UserService)context.getBean("UserService");
u.say();
}
}
5、项目总结:
spring实际上是一个容器框架,可以配置各种bean(action/service/domain/dao),并且可以维护bean与bean的关系,当我们需要使用某个bean的时候,我们只要getBean(id)即可。
浙公网安备 33010602011771号