1,接口类
package com.songyan.demo1; public interface UserDao { public void say(); }
package com.songyan.demo1; public interface UserService { public void say(); }
2,实现类
package com.songyan.demo1; public class UserDaoImpl implements UserDao{ public void say() { System.out.println("UsdeDao say helloWord"); } }
package com.songyan.demo1; public class UserServiceImpl implements UserService{ private UserDao userDao; public void setUserDao(UserDao userDao) { this.userDao=userDao; } public void say() { this.userDao.say(); System.out.println("hello"); } }
3,配置文档
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="userDao" class="com.songyan.demo1.UserDaoImpl" ></bean> <bean id="userService" class="com.songyan.demo1.UserServiceImpl"> <property name="userDao" ref="userDao"></property></bean> </beans>
3,测试类
package com.songyan.demo1; import javax.xml.registry.infomodel.User; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test { public static void main(String[] args) { //初始化容器,加载xml ApplicationContext application =new ClassPathXmlApplicationContext("applicationContext.xml"); //通过容器获取javabean对象实例 UserDao userDao=(UserDao)application.getBean("userDao"); //调用实例的方法 userDao.say(); //通过容器获取UserService对象 UserService userService=(UserService)application.getBean("userService"); userService.say(); } }