- IoC--Inversion of Control 控制反转
控制:传统应用程序会主动去获取所需要的资源,譬如A new了一个B,是A主动创建的
反转:IoC有个专门的容器来创建对象,并由IoC容器来控制,所以传统应用程序由主动获取变为被动接受容器分配的对象称为反转
有优点:降低了类与类之间的耦合便于测试和重复利用
- DI--Dependency Injection依赖注入
依赖:传统应用程序A new B,即A依赖B
注入:由IoC为应用程序注入依赖,包括某个对象所需要的资源,包括对象、resource、常量
依赖注入和控制反转描述的是同一件事情
- IoC容器--BeanFactory
Spring的BeanFactory代表的就是IoC容器
IoC容器通过
读取配置文件中的配置元数据,通过元数据对应中的各个对象进行实例化以及装配
配置方式一般使用xml,也可以使用注解、java文件
- HelloWorld
配置JUnit:点击add Library>>JUnit>>next>>选择JUnit 4
必要jar:commons-logging.jar
HelloDao.java
package com.hello.dao;
public interface HelloDao {
public void sayHello();
}
HelloImpl.java
package com.hello.dao.Impl;
import com.hello.dao.HelloDao;
public class HelloImpl implements HelloDao{
@Override
public void sayHello() {
System.out.println("hello world");
}
}
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"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!--id为组件名 class为组件类 -->
<bean id="hello" class="com.hello.dao.Impl.HelloImpl">
</bean>
</beans>
HelloTest.java
package com.test;
import junit.framework.TestCase;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.hello.dao.HelloDao;
public class HelloTest extends TestCase {
public void testHelloWorld(){
//实例化IoC容器
ApplicationContext context =new ClassPathXmlApplicationContext("applicationContext.xml");
//获取bean 这里是实现接口
HelloDao helloDao=context.getBean("hello",HelloDao.class);
//执行业务
helloDao.sayHello();
}
}
目录结构
- ApplicationContext
IoC容器指的是org.springframework.beans.factory的BeanFactory接口
ApplicationContext是org.springframework.context下的接口,其扩展了BeanFactory,还提供了AOP集成、国际化处理、事件传播以及不同层次的context实现
ApplicationContext完全继承了BeanFactory,所以BeanFactory所有的语义适用于ApplicationContext
BeanFactory实现:classpath resource或者 file resource中获取资源
1)File file=new file ("fileResource.xml");
Resource resource=new fileSystemResource(file);
BeanFactory beanFactory=new XmlBeanFactory(resource);
2)Resource resource=new ClassPathResource("classpathResource.xml");
BeanFcctory beanFactory=new XmlBeanFactory(resource);
ApplicationContext实现:
1)BeanFactory beanFactory =new FileSystemXmlApplicationContext("fileSystemResource.xml");
2)BeanFactory beanFactory =new ClassPathXmlApplicationContext("classpathRescource.xml");
- ApplicationContext 获取bean的方法
Object getBean(String name) 根据名称返回一个Bean,客户端需要自己进行类型转换;
T getBean(String name, Class<T> requiredType) 根据名称和指定的类型返回一个Bean,客户端无需自己进行类型转换,如果类型转换失败,容器抛出异常;
T getBean(Class<T> requiredType) 根据指定的类型返回一个Bean,客户端无需自己进行类型转换,如果没有或有多于一个Bean存在容器将抛出异常;
Map<String, T> getBeansOfType(Class<T> type) 根据指定的类型返回一个键值为名字和值为Bean对象的 Map,如果没有Bean对象存在则返回空的Map。
package com.hello.dao;
public interface HelloDao {
public void sayHello();
}
HelloImpl.java
package com.hello.dao.Impl;
import com.hello.dao.HelloDao;
public class HelloImpl implements HelloDao{
@Override
public void sayHello() {
System.out.println("hello world");
}
}
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"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!--id为组件名 class为组件类 -->
<bean id="hello" class="com.hello.dao.Impl.HelloImpl">
</bean>
</beans>
HelloTest.java
package com.test;
import junit.framework.TestCase;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.hello.dao.HelloDao;
public class HelloTest extends TestCase {
public void testHelloWorld(){
//实例化IoC容器
ApplicationContext context =new ClassPathXmlApplicationContext("applicationContext.xml");
//获取bean 这里是实现接口
HelloDao helloDao=context.getBean("hello",HelloDao.class);
//执行业务
helloDao.sayHello();
}
}
目录结构
- ApplicationContext
IoC容器指的是org.springframework.beans.factory的BeanFactory接口
ApplicationContext是org.springframework.context下的接口,其扩展了BeanFactory,还提供了AOP集成、国际化处理、事件传播以及不同层次的context实现
ApplicationContext完全继承了BeanFactory,所以BeanFactory所有的语义适用于ApplicationContext
BeanFactory实现:classpath resource或者 file resource中获取资源
1)File file=new file ("fileResource.xml");
Resource resource=new fileSystemResource(file);
BeanFactory beanFactory=new XmlBeanFactory(resource);
2)Resource resource=new ClassPathResource("classpathResource.xml");
BeanFcctory beanFactory=new XmlBeanFactory(resource);
- ApplicationContext 获取bean的方法
Object getBean(String name) 根据名称返回一个Bean,客户端需要自己进行类型转换;
T getBean(String name, Class<T> requiredType) 根据名称和指定的类型返回一个Bean,客户端无需自己进行类型转换,如果类型转换失败,容器抛出异常;
T getBean(Class<T> requiredType) 根据指定的类型返回一个Bean,客户端无需自己进行类型转换,如果没有或有多于一个Bean存在容器将抛出异常;
Map<String, T> getBeansOfType(Class<T> type) 根据指定的类型返回一个键值为名字和值为Bean对象的 Map,如果没有Bean对象存在则返回空的Map。
T getBean(String name, Class<T> requiredType) 根据名称和指定的类型返回一个Bean,客户端无需自己进行类型转换,如果类型转换失败,容器抛出异常;
T getBean(Class<T> requiredType) 根据指定的类型返回一个Bean,客户端无需自己进行类型转换,如果没有或有多于一个Bean存在容器将抛出异常;
Map<String, T> getBeansOfType(Class<T> type) 根据指定的类型返回一个键值为名字和值为Bean对象的 Map,如果没有Bean对象存在则返回空的Map。