import jars:
spring-beans,context,cores,expression,commons-logging,sl4j..
create interface:(dao)
public interface UserInterface:
public void addUser(String name,String pwd);
create implements:(daoImpl)
public class UserInterfaceImpl implements UserInterface:
public void addUser(String name,String pwd){
System.out.println("name="+name+",pwd="+pwd);
}
create manager:(service)
public interface UserManger:
public void addUser(String name,String pwd);
create managerImpl:(serviceImpl)
public class UserManagerImpl implements UserManager:
private UserInterface userInterface;
public void SetUserInterface(UserInterface userInterface){
this.userInterface=userInterface;
}
public void addUser(String name,String pwd){
userInterface.addUser(name,pwd);
System.out.println("service using dao to implement");
}
ApplicationContext.xml:
<bean id="userInterface" class="UserInterface"/>
<bean id="userManager" class="UserManager">
<property name="userInterface" ref="userInterface"/>
</bean>
Test spring:
main method{
BeanFactory bf/ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
UserManager um=(UserManager)ac.getBean("userManager");
um.addUser("name","pwd");
Result:name=name,pwd=pwd;