import com.hw.dao.UserDao; import com.hw.service.UserService; public class UserServiceImpl implements UserService { /* 使用构造方法注入的方式:添加constructor */ private UserDao userDao; private int num; private String name; public UserServiceImpl(UserDao userDao, int num, String name) { this.userDao = userDao; this.num = num; this.name = name; } @Override public void save() { System.out.println("userServiceImpl save runing..." + num + " " + name); userDao.save(); } }
<?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 https://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 配置spring控制的资源 --> <!-- 2、将需要注入的资源声明为bean --> <bean id="userDao" class="com.hw.dao.impl.UserDaoImpl"/> <bean id="userService" class="com.hw.service.impl.UserServiceImpl"> <constructor-arg name="userDao" ref="userDao" /> <constructor-arg name="num" value="123" /> <constructor-arg name="name" value="zs" /> </bean> </beans>
标签:userDao,name,构造方法,private,num,save,Ioc,注入 From: https://www.cnblogs.com/weiduaini/p/17288997.html