概述
- What is IoC
- Inversion of Control
- Why IoC
- Dependency Inversion Principle
- 依赖倒置原则
- 高层不依赖于底层,均依赖抽象
- 实现基础:Java语言对接口,多态的支持;工厂模式
- 依赖抽象的例子
@Resource
AService aService;
public void doSomething() {
aService.doSomenthing();
}
- IoC与DI的关系
- DI是IoC的一种实现
- Dependency Injection:依赖注入
- Dependency Lookup:依赖查找
- 什么是依赖查找
@Stateless
@EJB(name="audit", beanInterface=AuditService.class)
public class DepartmentServiceBean implements DepartmentService {
private AuditService audit;
@PostConstruct
public void init() {
try {
Context ctx = new InitialContext();
audit = (AuditService) ctx.lookup("java:comp/env/audit");
} catch (NamingException e) {
throw new EJBException(e);
}
}
public void performAudit() {
audit.audit();
}
}
网友评论