Saturday, 20 July 2019

AutoWiring Modes in spring

The Spring container can auto wire relation between collaborating beans . you can allow spring to resolve collaborators(associations) automatically for your bean by inspecting the contents of the application context.

Advantages:

Significantly reduce the need to specify the properties or constructor arguments.

1.No Autowiring:

No Auto wiring bean reference must be defined via a reference element.

2.ByName:

Auto wiring by property name spring looks for a bean with the same name as the property that needs to be autowired.

package com.searchendeca.application;

public class GreetingName {

private String name;

public String getName() {
return "Hello::"+name;
}

public void setName(String name) {
this.name = name;
}


}

package com.searchendeca.application;

public class HelloWorld {

private GreetingName greet;

public GreetingName getGreet() {
return greet;
}

public void setGreet(GreetingName greet) {
this.greet = greet;
}
static HelloWorld  clientService = new HelloWorld();

private String mMessage;

public String getMessage() {
return mMessage;
}

public void setMessage(String pMessage) {
this.mMessage = pMessage;
}


public HelloWorld hello() {
return clientService;
}
}


<bean id="helloWorldService"
class="com.searchendeca.application.HelloWorld" autowire = "byName"> // here the value greet been autowired.
<property name="Message" value="Welcome to Search Endeca" />
<!--  <property name="greet" ref="helloGreeting" />-->
</bean>

<bean id="greet" class="com.searchendeca.application.GreetingName">
<property name="name" value="Syed" />
</bean>


public class Hello {
@SuppressWarnings("resource")
public static void main(String[] args) {
// loading the definitions from the given XML file
//ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
ApplicationContext context = new FileSystemXmlApplicationContext("/src/main/resources/applicationContext.xml");
//PropertiesSample service = (PropertiesSample) context.getBean("configBean");
HelloWorld service = (HelloWorld) context.getBean("helloWorldService");

// Properties message = service.getProperties();
/*System.out.println(service.getNameList());
System.out.println(service.getNameSet());
System.out.println(service.getPersonMap());

System.out.println(service.getProperties());*/
System.out.println(service.getGreet().getName());

//System.out.println(message);
//set a new name
//service.setMessage("Spring");
//message = service.getMessage();
//System.out.println(message);
}
}

Output: Hello::Syed

3.By Type:

Allows a property to be autowired if exactly one of the bean property type exists in the configuration file.

<bean id="helloWorldService"
class="com.searchendeca.application.HelloWorld" autowire = "byType">
<property name="Message" value="Welcome to Search Endeca" />
<!--  <property name="greet" ref="helloGreeting" />-->
</bean>

<bean id="greet" class="com.searchendeca.application.GreetingName">
<property name="name" value="Syed" />
</bean>


4.By Constructor:

Analogous to byType, but applies to constructor arguments. It then tries to match and wire its constructor's argument with exactly one of the beans name in the configuration file. If matches are found, it will inject those beans. Otherwise, bean(s) will not be wired.

<bean id="helloWorldService"
class="com.searchendeca.application.HelloWorld" autowire = "constructor">
<constructor-arg value="Welcome to Search Endeca" />
<!--  <property name="greet" ref="helloGreeting" />-->
</bean>

<bean id="greet" class="com.searchendeca.application.GreetingName">
<property name="name" value="Syed" />
</bean>


package com.searchendeca.application;

public class HelloWorld {



public HelloWorld(GreetingName greet, String mMessage) {
super();
this.greet = greet;
this.mMessage = mMessage;
}
private GreetingName greet;

public GreetingName getGreet() {
return greet;
}

public void setGreet(GreetingName greet) {
this.greet = greet;
}
//HelloWorld  clientService = new HelloWorld();

private String mMessage;

public String getMessage() {
return mMessage;
}

public void setMessage(String pMessage) {
this.mMessage = pMessage;
}


/*public HelloWorld hello() {
return clientService;
}*/
}

Limitations of Autowiring.

Autowiring works best when it is used consistently across a project. If autowiring is not used in general, it might be confusing for developers to use it to wire only one or two bean definitions.

It also has the chances of overiding the autowiring,cannot autowire the primitives such as string and classes.

Excluding a bean from autowiring

In Spring’s XML format, set the autowire-candidate attribute of the <bean/> element to false;for excluding a bean from autowiring.

<bean id="greet" class="com.searchendeca.application.GreetingName" autowire-candidate="false">
<property name="name" value="Syed" />
</bean>

No comments:
Write comments