Spring - Bean

“A bean is an object that is instantiated, assembled, and otherwise managed by a Spring IoC container. These beans are created with the configuration metadata that you supply to the container, for example, in the form of XML definitions.”

Different types of Spring Beans

spring-bean-types

Properties

Inner Bean

Bean inside bean, object inside object

In the example, PointA is an inner bean of square referencing to zeroPoint. PointB is an inner bean defined inside square.

e.g. applicationContext.xml

<bean id="square" class="com.chennanni.learnspring.Square">
	<property name="pointA" ref="zeroPoint" />
	<property name="pointB">
		<bean class="com.chennanni.learnspring.Point">
			<property name="x" value="1" />
			<property name="y" value="1" />
		</bean>
	</property>
</bean>

<bean id="zeroPoint" class="com.chennanni.learnspring.Point">
	<property name="x" value="0" />
	<property name="y" value="0" />
</bean>

Bean Scope

Basic

Web-aware Context

Syntax

<bean ... scope="singleton">
     ...
</bean>

Bean Autowiring

When to use it

to autowire an inner bean

比如说有一个class Person,里面有一个inner class Test, 在做DI的时候,会把PersonTest都配置好,但是怎么把这两者关联起来呢? 这里就需要把Testwire到Person上。

class Person {
	int id;
	String name;
	
	@Autowired
	Test test;
}

Annotation

Autowired on Properties

@Autowired
private Person person;

Autowired on Setter Methods

@Autowired
public void setPerson(Person person){
   this.person = person;
}

Autowired on Constructors

@Autowired
public Trip(Person person){
	this.person = person;
}

Autowired with (required=false) option

@Autowired(required=false)

if can not perform autowire for a property, set it to default value(null)

Configuration

autowire = “byName”: class variable’ name is the same as the name of bean

<bean ... autowire = "byName">
     ...
</bean>

autowiring = “byType”: class variables’ type is the same as the type of bean

<bean ... autowire = "byType">
     ...
</bean>

autowire = “constructor”: similar to byType, but type applies to constructor arguments

Note: in the constructor the bean needs to be initiated

<bean ... autowire = "constructor">
     ...
</bean>
public class Foo {
	private Moo moo;
	public Foo(Moo moo) {
		this.moo = moo;
	}
}

Bean Life Cycle

we can define what should be done after the bean is initialized/destroied

(Take initialization for example, destruction is just the same.)

Two Steps:

Register a shut down hook

AbstractApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
context.registerShutdownHook();

Write the init method

The first way: implement InitializingBean and overwrite its afterPropertiesSet()

public class ExampleBean implements InitializingBean {
   public void afterPropertiesSet() {
      // do some initialization work
   }
}

The second way(recommeded): specify the init-method attribute in the XML configuratoin file to map it to your own init method

<bean id="exampleBean"
         class="examples.ExampleBean" init-method="init"/>

public class ExampleBean {
   public void init() {
      // do some initialization work
   }
}

Steps to Create and Use a Spring Bean

Fork me on GitHub