Spring defines and assembles beans
I discussed IOC and AOP in Spring in my previous blog. This article uses code to demonstrate how Spring implements IOC. This blog will introduce how to define and load Java Beans in Spring.
Business scenarios
This is also an example of driving a car. First, define a Car interface and two Benz and BMW implementations, and then define a Person class. The Person class depends on the Car interface.
public interface Car { void go();}
public class Benz implements Car { public void go() { System.out.println(benz go......); }}
public class BMW implements Car { public void go() { System.out.println(bmw go......); }}
public class Person { String name = ; Car car = null; public Car getCar() { return car; } public void setCar(Car car) { this.car = car; } public Person(String name) { this.name=name; } public void Drive(){ System.out.println(name+ is driving ...); car.go(); }}
In the Person class, we can see that the car object is the dependent object of this class and needs to be injected into the Person class through the constructor. The above Code does not have the shadow of Spring. Let's take a look at how Spring is injected.
Add Spring dependency
Currently, many projects use maven to manage dependencies. This is also true for this project. I add dependency nodes to pom. xml.
org.springframework
spring-core
2.5.6
org.springframework
spring-beans
2.5.6
org.springframework
spring-context
2.5.6
org.springframework
spring-context-support
2.5.6
Place these dependency nodes under the dependencies node of the pom. xml file. eclipse automatically downloads the related packages to the default location.
Manually define and assemble beans
Create an xml file named bean. xml in the root directory of the project. The content is as follows:
The XML file above defines a bean with id as car and a bean with id as tom. The car serves as the dependency of tom.
Is manually assembled to the car attribute of tom.
The act of creating collaborative relationships between application objects is called assembly, which is also the essence of object injection.
Next, we instantiate an application context object in the main method and obtain the tom node in the bean.
public class App { public static void main( String[] args ) { ApplicationContext context = new ClassPathXmlApplicationContext(bean.xml); Person tom=(Person) context.getBean(tom); tom.Drive(); }}
Run the program and the output result is:
Tom is driving ...bmw go......
Automatic Assembly
Repeat it again. The act of creating a cooperative relationship between application objects is called Assembly rather than the process of instantiating objects. In the preceding xml file
But with the development of applications, xml configuration files will become more and more complex.ref=car
To associate the bean with the id of car. Next I will introduce how Spring can automatically assemble beans.
Automatic Assembly Type
Spring has four Assembly policies. To reduce the complexity of this article, I will introduce two common policies: byName and byType. As the name suggests, the byName method is to check whether the attribute name is the same as the bean id. In this example, the Person class has an attribute named car, if you set this class or modify attributes to automatic assembly and the Assembly policy to byName, Spring will find the bean with the id of car (which must be the same name. The byType method indicates whether the comparison type is the same. In this example, if the car attribute of the Person or Person class is set to automatic assembly and the Assembly policy is byType, because the car attribute is of the Car type, spring will find the bean of the type of Car or its subclass during automatic assembly.
Automatic Assembly using XML
Next I will modify the code and use xml configuration to implement automatic assembly.
The Person class does not need to be modified.
First, I removed
This line of code is manual. Then, I add an attribute to the bean whose id is tom.autowire=byName
Set it to automatically assemble Dependencies by name. When getting the Person object, all the property names of the Person have special meanings. Spring detects another property named car in the Person class and finds a bean with the id of car in the configuration file. Therefore, it automatically assembles the bean to the car attribute of the Person.
Automatic Assembly using annotations
Modify bean. xml as follows:
An attribute of the bean whose id is tomautowire=byName
Delete, and then add an attribute in bean with the nodedefault-autowire=byName
To set the default value of byName for automatic assembly of all beans defined in this file. Then, modify the Person class and add the @ Autowired annotation to the car setter method to notify Spring to automatically assemble the property. Run the code again and the output result remains unchanged.
@Autowiredpublic void setCar(Car car) { this.car = car;}
To implement automatic assembly through annotations, you must first define the default automatic assembly method on the beans root directory, then, you do not need to configure the autowire attribute for every bean that requires automatic assembly in the configuration file. (If the method is different from the default one, you can still configure it separately, to overwrite the default automatic assembly method ). The annotation method is more delicate. It can control the bean to be automatically assembled at the attribute level.
In all the code above, we useorg.zdk.springProj1.BMW
Class As the dependency of the Person class. If the Person class needs to be switched to the Mercedes-Benz without the need for a carriage return, you only need to change the configurationorg.zdk.springProj1.Benz
No need to modify any Java code. This is the power of Spring as an IOC container.