autowire byType according to Spring 4.1.6 to Documentation -
allows property autowired if 1 bean of property type exists in container. if more 1 exists, fatal exception thrown, indicates may not use bytype autowiring bean. if there no matching beans, nothing happens; property not set. - spring reference guid 4.1.6
<bean id="person" class="autowire.person" autowire="bytype" /> <bean id="invisible" class="autowire.ability" > <property name="skill" value="invisible" /> </bean> <bean id="invisible2" class="autowire.ability" > <property name="skill" value="invisible" /> </bean>
class definitions:
package autowire; public class person { private ability ability; //... } package autowire; public class ability { private string skill; public string getskill() { return skill; } public void setskill(string skill) { this.skill = skill; } }
i able define 2 beans of same type of class "autowire.ability". didnt fatal exception. understanding correct ?
you're there (to error). need tell spring class attributes need autowired. annotate person.ability
@autowired , should error.
import org.springframework.beans.factory.annotation.autowired; public class person { @autowired private ability ability; //... }
or better create constructor , autowire it, injecting attributes considered bad practice.
public class person { private ability ability; @autowired public person(ability ability) { this.ability = ability; } //... }
spring doesn't assume attributes need injected (autowired) need specify ones want inject.
Comments
Post a Comment