1.
2.Test.java
package com.inspur; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub ApplicationContext application = new ClassPathXmlApplicationContext("applicationContext.xml"); Userdao userDao =(Userdao) application.getBean("userId"); userDao.say(); UserService userService = (UserService)application.getBean("userService"); userService.say(); } }
3.Userdao.java
package com.inspur; public interface Userdao { public void say(); }
4.UserDaoImpI.java
package com.inspur; public class UserDaoImpI implements Userdao { public void say() { // TODO Auto-generated method stub System.out.println("Spring say hello world"); } }
5.UserService.java
package com.inspur; public interface UserService { public void say(); }
5.UserServiceImpl.java
package com.inspur; public class UserServiceImpL implements UserService { private Userdao userDao; public void setUserDao(Userdao userDao) { this.userDao = userDao; } public void say(){ System.out.println("UserServiceImpL say Hello world"); } }
6.applicationContext.xml
<?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="userId" class="com.inspur.UserDaoImpI"> </bean> <bean id="userService" class="com.inspur.UserServiceImpL"> <property name="userDao" ref="userId"></property> </bean> </beans>
name=“userDao"是UserServiceImpl的
ref="userId"是