ioc 依赖注入

前端之家收集整理的这篇文章主要介绍了ioc 依赖注入前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

package com.ssh;

public class LoginBiz implements BaseBiz{

private BaseDao loginDao;

public void setLoginDao(BaseDao loginDao){
this.loginDao = loginDao;
}

public void login(){
loginDao.login();
}
}

package com.ssh;

public interface BaseBiz {

public void login();
}

package com.ssh;

public class LoginDao implements BaseDao{

public void login(){
System.out.println("验证");
}
}

package com.ssh;

public interface BaseDao {

public void login();
}

package com.ssh;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.ioc.Print;

public class Test {

/**
* @param args
*/
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
BaseBiz biz = (LoginBiz)context.getBean("loginBiz");
biz.login();
}

}

<bean id="loginDao" class="com.ssh.LoginDao"></bean> <bean id="loginBiz" class="com.ssh.LoginBiz"> <property name="loginDao" ref="loginDao"/> </bean>

猜你在找的设计模式相关文章