小编Ami*_*aha的帖子

spring自动装配有什么好处

自动装配是Spring的优势究竟是什么?

春季自动装配的一个例子是

public class TestClass {
    testMethod() {
        // .....
    };
}

public class MainClass {
    public static void main(String[] args) {
        ApplicationContext ctx = new ClasspathXmlApplicationContext("test.xml");
        TestMethod obj = (TestClass) ctx.getBean("test");
        obj.testMethod();
    }
}
Run Code Online (Sandbox Code Playgroud)

测试文件

<bean id="test" class="TestClass">
Run Code Online (Sandbox Code Playgroud)

可以使用以下方法完成正常操作中的相同操作:

public class MainClass {
    public static void main(String[] args) {
        TestClass obj = new TestClass();
        obj.testMethod();
    } 
}
Run Code Online (Sandbox Code Playgroud)

Spring 的优势是什么,我的意思是我听说过控制反转和依赖注入这两个术语。在这两个示例中,通过newoerator再次通过 Spring XML 使用了一次 TestClass 引用。那么有人可以用简单的术语解释什么是优势。

spring dependency-injection inversion-of-control autowired

4
推荐指数
1
解决办法
4145
查看次数