Bean的依赖注入方式--set方法

package ioc1.it.dao;

public class userDaoImpl implements userDao {
    public userDaoImpl() {
        System.out.println("无参");
    }

    @Override
    public void save() {
        System.out.println("kachaq");
    }
}

 

private userDao userDao;

    public void setUserDao(ioc1.it.dao.userDao userDao) {
        this.userDao = userDao;
    }
    public void save(){
        userDao.save();
    }

 

        <bean id="userDao" class="ioc1.it.dao.userDaoImpl"></bean>
        <bean id="service" class="factory.service">
                <property name="userDao" ref="userDao"></property>
        </bean>

 

    public static void main(String[] args) {
        ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
        service service = (service)app.getBean("service");
        service.save();

    }

 

上一篇:ctf特训营-学习笔记


下一篇:SpringBoot集成MyBatis底层原理及简易实现