Description

Spring Auto-Wiring BeansIn Spring framework, you can wire beans automatically with auto-wiring feature. To enable it, just define the “autowire” attribute in <bean>. <bean id="customer" class="com.mkyong.common.Customer" autowire="byName" /> In Spring, 5 Auto-wiring modes are supported.      no – Default, no auto wiring, set it manually via “ref” attribute byName – Auto wiring by property name. If the name of a bean is same as the name of other bean property, auto wire it. byType – Auto wiring by property data type. If data type of a bean is compatible with the data type of other bean property, auto wire it. constructor – byType mode in constructor argument. autodetect – If a default constructor is found, use “autowired by constructor”; Otherwise, use “autowire by type”. Examples A Customer and Person object for auto wiring demonstration. package com.mkyong.common; public class Customer { private Person person; public Customer(Person person) { this.person = person; } public void setPerson(Person person) { this.person = person; } //... } package com.mkyong.common; public class Person { //... } Person" /> See full example – Spring Autowiring by Name.common. Auto-Wiring ‘no’ This is the default mode.mkyong.mkyong.mkyong. 4.Person" /> 2.Person" /> See full example – Spring Autowiring by Type.common. so.mkyong. In this case. Auto-Wiring ‘constructor’ Auto-wire a bean by property data type in constructor argument. since the data type of “person” bean is same as the data type of the “customer” bean‟s property (Person object).common.common. Auto-Wiring ‘byType’ Auto-wire a bean by property data type. <bean id="customer" class="com. 3.Customer" autowire="byType" /> <bean id="person" class="com. <bean id="customer" class="com. since the data type of “person” bean is same as the constructor argument data type in “customer” bean‟s .common.Customer" autowire="byName" /> <bean id="person" class="com. since the name of “person” bean is same with the name of the “customer” bean‟s property (“person”).mkyong. <bean id="customer" class="com. Spring will auto wired it via setter method – “setPerson(Person person)“.mkyong.1.common. so. In this case. you need to wire your bean via „ref‟ attribute. Spring will auto wired it via setter method – “setPerson(Person person)“. Auto-Wiring ‘byName’ Auto-wire a bean by property name. In this case.Customer"> <property name="person" ref="person" /> </bean> <bean id="person" class="com. since there is a default constructor in “Customer” class. so.common.property (Person object).common.mkyong.common. so.mkyong. uses “byType”.Person" /> See full example – Spring Autowiring by AutoDetect. Note It‟s always good to combine both „auto-wire‟ and „dependency-check‟ together.common.mkyong.mkyong. Spring auto wired it via constructor method – “public Customer(Person person)“. Spring „auto-wiring‟ make development faster with great costs – it added complexity for the entire bean configuration file. to make sure the property is always auto-wire successfully. <bean id="customer" class="com.mkyong. i rather wire it manually. <bean id="customer" class="com.Person" /> See full example – Spring Autowiring by Constructor. <bean id="customer" class="com. Spring auto wired it via constructor method – “public Customer(Person person)“.Customer" autowire="constructor" /> <bean id="person" class="com.Customer" autowire="autodetect" dependency-check="objects /> <bean id="person" class="com. which is more flexible and recommended. or better uses @Autowired annotation.common.Customer" autowire="autodetect" /> <bean id="person" class="com. uses “constructor”. In practice. Auto-Wiring ‘autodetect’ If a default constructor is found.common. 5.mkyong.Person" /> Conclusion In my view. In this case. and you don‟t even know which bean will auto wired in which bean. . Otherwise. it is always clean and work perfectly. Spring MVC is entirely based on interfaces. You don‟t get pushed to use JSP if you don‟t want to. 3. Spring provides a very clean division between controllers.BENEFITS of Spring MVC over Struts Spring is a powerful Java application framework. 2. and beautifully integrated with other objects managed by Spring. 8. due to the avoidance of forced concrete inheritance and explicit dependence of controllers on the dispatcher servlet. JavaBean models. just about every part of the Spring MVC framework is configurable via plugging in your own interface. More testable code (validation has no dependency on Servlet API) . 6. you can use Velocity. No ActionForms. Spring Controllers are configured via IoC like any other objects. Furthermore. Spring provides an integrated framework for all tiers of your application. Spring uses dependency injection to achieve simplification and increase testability. provides interceptors as well as controllers. Struts and other dedicated web frameworks leave you on your own in implementing your business objects. like WebWork. It provides enterprise services to Plain Old Java Objects (POJOs). Bind directly to domain objects 9. The web tier becomes a thin layer on top of a business object layer. Spring MVC is truly view-agnostic. making it easy to factor out behavior common to the handling of many requests. Spring MVC web tiers are typically easier to test than Struts web tiers. Spring. 1. 7. Unlike Struts. XLST or other view technologies. Of course we also provide convenience classes as an implementation option. used in a wide range of Java applications. your own templating language – you can easily implement the Spring View interface to integrate it. Spring‟s MVC is very flexible. and views. If you want to use a custom view mechanism – for example. This encourages good practice. 4. This makes them easy to test. 5. which forces your Action and Form objects into concrete inheritance (thus taking away your single shot at concrete inheritance in Java). Spring offers better integration with view technologies other than JSP (Velocity / XSLT / FreeMarker / XL etc. Spring doesn‟t force you to do this although there are convenience Controller implementations that you can choose to extend. Spring has a well defined interface to business layer 12.10. Struts imposes dependencies on your Controllers (they must extend a Struts class).) . 11.
Copyright © 2024 DOKUMEN.SITE Inc.