(笔记)IOC Spring 初学逻辑整理(Inversion of control)
(MyEclipse)基本构架
--> MyEclipse-->Add Spring Capabilities
在SRC文件夹出现ApplicationContext.xml,在此处Compile.
(Contents)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean name="apple" ref="apple2"></property>
</bean>
</beans>
--> Create new package,Apple
The file (AppleDao,AppleDaoImpl,Apples) must be exist.
AppleDao
AppleDaoImpl --> implements AppleDao --> Apple
AppleDaoImple --> implements AppleDao --> Apple2
Apples
package Apple;
public class Apples {
AppleDao apple;
public AppleDao getApple() {
return apple;
}
// set method is needed here....
public void setApple(AppleDao apple) {
this.apple = apple;
}
public void add(){
apple.addApple(1, "", "", 102);
apple.deleteApple(1);
}
}
create Main to realize the function:
package Apple;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
BeanFactory beanFactory = new ClassPathXmlApplicationContext("applicationContext.xml");
Apples apple=(Apples)beanFactory.getBean("apples");
apple.add();
}
}