IOC+DI:降低代码之间的耦合度,复用性 强(使用配置文件,配置对象之间的关系)
面向切面AOP:
公共问题 集中处理[在程序运行过程中 动态的添加需要的运行代码];
通知事件:前置 后置 异常 环绕........
案例:
通过 IOC 和 ID 创建 对象?
回顾之前创建对象的方式:
1 通过new的形式创建
2 通过工厂模式创建
3 通过 反射方式创建对象
反射方式实现 有多种?.......
public class HelloWorld {
private Message msg;
public HelloWorld(){
System.out.println("Spring 实例化主调 bean:HelloWorld实例");
}
//依赖注入必要的setter方法
public Message getMsg() {
return msg;
}
public void setMsg(Message msg) {
System.out.println("Spring 执行依赖关系注入");
this.msg = msg;
}
//测试初始化方法
public void init(){
System.out.println("正在执行初始化方法");
msg.say();
}
}
<!-- 配置HelloWorld bean 使用init-method属性指定init方法的执行时机 -->
<bean id="helloworld" class="com.zgq.spbeen.HelloWorld" init-method="init">
<property name="msg" ref="message"></property>
</bean>
<bean id="message" class="com.zgq.spbeen.Message"/>
@Test
public void test3(){
ApplicationContext acf = new ClassPathXmlApplicationContext("applicationContext.xml");
HelloWorld hello = (HelloWorld)acf.getBean("helloworld");
}