jan*_*ith 5 java spring inversion-of-control
我理解IOC概念,我们可以使用布线混合和匹配不同的类.通过将布线/关系处理委托给基础xml(上下文xml),每个类都可以摆脱硬代码的依赖性.
这是我的问题,为什么我们使用xml?我们可以使用java类简单地连接所有组件.代替
<bean id="helloWorld" class="com.vaannila.HelloWorld">
<property name="message" value="HelloWorld"></property>
</bean>
public static void main(String[] args)
{
// TODO Auto-generated method stub
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
HelloWorld helloWorld = (HelloWorld) context.getBean("helloWorld");
helloWorld.display();
}
Run Code Online (Sandbox Code Playgroud)
我们可以用它们重写它们
HelloWorld helloWorld = new HelloWorld();
helloWorld.setMessage("HelloWorld");
helloWorld.display();
Run Code Online (Sandbox Code Playgroud)