package cn.itcast.ioc;
public interface UserService {
public void addUser();
}
package cn.itcast.ioc;
public class UserServiceImpl implements UserService{
private UserDao userDao;
//设值注入
public void setUserDao(UserDao userDao){
this.userDao=userDao;
}
//构造器注入
public UserServiceImpl(UserDao userDao){
this.userDao=userDao;
}
public void addUser(){
this.userDao.save();
System.out.println("spring : hello user Service");
}
}
package cn.itcast.ioc;
public interface UserDao {
public void save();
}
package cn.itcast.ioc;
public class UserDaoImpl implements UserDao{
public void save(){
System.out.println("spring : hello user dao");
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean id="userDao" class="cn.itcast.ioc.UserDaoImpl"></bean>
<!-- 设值注入 -->
<!-- <bean id="userService" class="cn.itcast.ioc.UserServiceImpl">
<property name="userDao" ref="userDao"/>
</bean> -->
<!-- 构造器注入 -->
<bean id="userService" class="cn.itcast.ioc.UserServiceImpl">
<constructor-arg name="userDao" ref="userDao"/>
</bean>
</beans>