第一课 spring的一点理论 2006-04-05

来源:互联网 发布:linux网络编程 pdf 编辑:程序博客网 时间:2024/04/28 12:01
1.         
 优点.
.with a couple of lines of configuration instead of embedded code
.Spring is much simpler than traditional J2EE alternatives. With Spring, you code your applications as plain old Java objects (POJOs)
2.
做法:
1.   A client uses another class that we'll call a service. The client has a property that accepts a service. The service is wrapped in an interface. The client can't see the implementation of the service. But this code is not yet loosely coupled: you still have to create the service somewhere. With dependency injection, a third-party, called the assembler or container, creates both the client and service, and then sets the value of aService (which is a reference to an instance of Service) to satisfy the dependency.
2.       Wrap the service in an interface(ArrayListRentABike implements RentABike  后者就是接口)
3.       Add a property to the client, to refer to the service.(class CommandLineView 里面包含RentABike对象 )
4.       With a third party framework or custom code, build the service and populate the property.(RentABikeAssembler,里面的main方法里面完成,creates both the client and service, and then sets the value of aService ):                                                                                                                                                                 CommandLineView clv = new CommandLineView( );
        RentABike rentaBike = new ArrayListRentABike("Bruce's Bikes");
        clv.setRentaBike(rentaBike);
 
3.the ideas behind the movement are simple. Program using interfaces, and let a third party inject the dependency instead of setting it yourself.You're going to eventually replace the assembler with Spring....
Of course, dependency injection is not the only way to manage dependencies. In fact, it's not the only good way. J2EE users tend to use service locators