May 14, 2021 Spring
byType
This pattern specifies automatic assembly by the property type.
Spring
container is
beans
and the
autowire
beans
in the
XML
profile is
byType
T
hen, if its
type
exactly one of
beans
names in the profile, it tries to match and connect its properties.
If a match is found, it injects these
beans
otherwise it throws an exception.
For example, in a configuration file, if
bean
definition is set
byType
and it
spellChecker
Spring
the
SpellChecker
SpellChecker
and uses it to set that property.
bean
Y
ou can still connect the
property
using the .lt; property and tags.
The following example will illustrate this concept and you will find that there is no difference with the example above, except that
XML
profile has been changed.
Let's use the
Eclipse IDE
right place, and then follow these steps to create a
Spring
application:
Steps | Describe |
---|---|
1 |
Create a project named
SpringExample
and create a package
com.tutorialspoint
in the
src
folder of the project you have created.
|
2 |
Use
Add External JARs
to add the Spring
Spring
you want,
Spring Hello World Example
section.
|
3 |
Create
com.tutorialspoint
TextEditor
MainApp in the
Java
SpellChecker
MainApp
|
4 |
Create
src
profile Beans in
Beans
Beans.xml
|
5 |
The final step is to create the
Java
of
Bean
profiles and run the application, as explained below.
|
Here is
TextEditor.java
file:
package com.tutorialspoint;
public class TextEditor {
private SpellChecker spellChecker;
private String name;
public void setSpellChecker( SpellChecker spellChecker ) {
this.spellChecker = spellChecker;
}
public SpellChecker getSpellChecker() {
return spellChecker;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void spellCheck() {
spellChecker.checkSpelling();
}
}
Here's another class-dependent
SpellChecker.java
content:
package com.tutorialspoint;
public class SpellChecker {
public SpellChecker(){
System.out.println("Inside SpellChecker constructor." );
}
public void checkSpelling() {
System.out.println("Inside checkSpelling." );
}
}
MainApp.java
files are:
package com.tutorialspoint;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("Beans.xml");
TextEditor te = (TextEditor) context.getBean("textEditor");
te.spellCheck();
}
}
Here is the profile Beans file
Beans.xml
normal:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- Definition for textEditor bean -->
<bean id="textEditor" class="com.tutorialspoint.TextEditor">
<property name="spellChecker" ref="spellChecker" />
<property name="name" value="Generic Text Editor" />
</bean>
<!-- Definition for spellChecker bean -->
<bean id="spellChecker" class="com.tutorialspoint.SpellChecker">
</bean>
</beans>
However, if you want to use
“byType”
then your
XML
will become as follows:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- Definition for textEditor bean -->
<bean id="textEditor" class="com.tutorialspoint.TextEditor"
autowire="byType">
<property name="name" value="Generic Text Editor" />
</bean>
<!-- Definition for spellChecker bean -->
<bean id="SpellChecker" class="com.tutorialspoint.SpellChecker">
</bean>
</beans>
Once you're done creating the source
bean
profile, we can run the application.
If your application is all right, it will print the following message:
Inside SpellChecker constructor.
Inside checkSpelling.