| ��վ��ҳ | ҵ������ | С�� | ���� | �˲� | ����Ƶ�� | ���� | ������ | ���߱��� | ������̳
���� 2489 �˹�ע������
���⣺[ԭ��]Spring���Ʒ�ת(IoC)������
ֻ��¥�� �����ղ�
lyn_gemini
Rank: 1
�ȡ�����������·
�����ӣ�103
ר�ҷ֣�3
ע���᣺2004-9-15
�ղ�
 ����������0 �ظ�������0 
[ԭ��]Spring���Ʒ�ת(IoC)������

Spring���ܵĺ��ľ��ǿ��Ʒ�ת(Inversion of Control)������ע��(Dependency Injection)��ͨ������������ʵ�������ϡ�

ʹ��IoC�������DZ����Ľ��������࣬�������Լ�������ȥ�ҡ�������ʵ������ʱ������������������ע�������������������⣺���Ʒ�ת����������Ȩת�Ƶ��ӿ��ϣ�����ע��ͨ��xml�����ļ�����ʵ����ʱ����������ע�롣ͨ��������ʵ�����𲽵����⣺


���ȼ�����һ����������Business��Ҫ������Dependency�ķ���f()�������ճ����������õ������Ĵ��룺
//**��Dependency**
public class Dependency {
public void f() {};
}
//**��Business**
public class Business {
Dependency d;
public Business() {
d = new Dependency();
}
public void doSth() {
d.f();
}
}


������ʵ�����������޸ģ�
���ȣ���Business����Dependencyʵ���Ļ��ø�Ϊsetter��ʽ�����Σ���Dependency����Ϊij���ӿڵ�ʵ�֡��ʿ��Եõ������µĴ��룺
//**�ӿ�IDependency**
public interface IDependency {
void f();
}
//**��Dependency**
public class Dependency {
public void f() {};
}
//**��Business**
public class Business {
IDependency d;
public Business() {}
public void doSth() {
d.f();
}
public void setDependency(IDependency d) {
this.d = d;
}
}


���µĴ����У�����Business�ı���d���Խ����κ�IDependency��ʵ�������⣬Dependency��ʵ������ͨ��Business�����ã�����ͨ��setter(Ҳ�����ù�����)�����ⲿ�����������ƺ������������Ĵ���ûʲô��ͬ�������Ѿ���һ�����õ����ơ��ؼ�����Dependency��ʵ�����δ��ⲿע����Business�أ�
����Ҫͨ��xml��ʵ���ˡ�

����һ��SpringFirst.xml�����м򵥵����ã�
<beans>
<bean id = "dependency" class = "aopfirst.business.Dependency" />
<bean
id = "business"
class = "aopfirst.business.Business"
>
<property name = "dependency">
<ref bean = "dependency" />
</property>
</bean>
</beans>
���������ļ��ォDependency����Business�����룬����Dependency��ΪBusiness��һ��������


����������xml�ļ�������������Ҫһ�������������ظ�xml�ļ���spring�ṩ���ֳɵ�API���ڼ���������xml��ʱ�򣬾ͽ��������¹�����ʵ����Dependency�࣬ʵ����Business�࣬����Dependency��ʵ����Ϊ����������Businessʵ����
setDependency()�����������Ǹò��Գ�����


public class StartServer {
public static void main(String [] args) {
ClassPathResource cr = new ClassPathResource("SpringFirst.xml");
BeanFactory factory = new XmlBeanFactory(cr);
Business b = (Business)factory.getBean("business");
b.doSth();
}
}


�����ij���������xml�Ժ󣬻���idΪ"business"��bean����Business����ʵ��������������doSth()�������ɴ˿ɼ���Business��������Dependency��ͨ��xml��ע���ģ�����Business��ͨ���ӿ�IDependency������Dependencyʵ�������ˣ������������µ�IDependency��ʵ��ʱ��ֻ��Ҫ�޸�xml�ļ����ɣ����Գ���ֻ��Ҫ����xml����idֵ��������Ҫ�IJ�����

�ܽ����������ӣ��Կ��Ʒ�ת������ע���Ѿ��������ˡ�������(Dependency)��ͨ���ⲿ(xml)��ע���ģ���������ʹ��������(Business)���Լ����죬������������ע�롣��һ���棬Business����Dependency������ת�Ƶ��Խӿ�IDependency������������Ȩ����ת�Ƶ��˽ӿڣ�����"ʵ��"ת�Ƶ�"����"�С������ǿ��Ʒ�ת��

����������������������: IoC��Spring����ת��
2006-08-09 10:13
���ٻظ���[ԭ��]Spring���Ʒ�ת(IoC)������
���ݼ�����...
 
������



�������� | �������� | �����й� | ����Cookies | TOP | �ֻ���

�����й� ��Ȩ���У�����������Ȩ����
Powered by Discuz, Processed in 0.015930 second(s), 7 queries.
Copyright©2004-2025, BC-CN.NET, All Rights Reserved