Author |
Topic: 3 Ways to Define Spring Bean |
|
WebSpider member offline |
|
posts: |
147 |
joined: |
06/29/2006 |
from: |
Seattle, WA |
|
|
|
|
|
3 Ways to Define Spring Bean |
Like Java bean, a Spring bean is just POJO class which can be instantiated by Spring IoC Container. There are three ways to define Spring bean: XML configuration based bean Java configuration based bean Annotation based bean
|
|
|
|
|
|
|
WebSpider member offline |
|
posts: |
147 |
joined: |
06/29/2006 |
from: |
Seattle, WA |
|
|
|
|
|
Way#1 Spring Bean by XML Configuration |
The POJO class
package com.example.spring.model;
public class Name_XML_Bean {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
The XML configuration file app-config.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<bean name="myXmlBean" class="com.example.spring.model.Name_XML_Bean"></bean>
</beans>
|
|
|
|
|
|
|
WebSpider member offline |
|
posts: |
147 |
joined: |
06/29/2006 |
from: |
Seattle, WA |
|
|
|
|
|
Way#2 Spring Bean by Java Configuration |
The POJO class
package com.example.spring.model;
public class Address_Configure_Bean {
private String address;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
The Java configuration
package com.example.spring.model;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class BeanConfig {
@Bean
public Address_Configure_Bean getConfigBasedBean(){
return new Address_Configure_Bean();
}
}
|
|
|
|
|
|
|
WebSpider member offline |
|
posts: |
147 |
joined: |
06/29/2006 |
from: |
Seattle, WA |
|
|
|
|
|
Way#3 Spring Bean by Annotation |
The POJO class
package com.example.spring.model;
import org.springframework.stereotype.Component;
@Component
public class Sex_Annotation_Bean {
private String sex;
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
The auto-detect configuration component-scan
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<bean name="myXmlBean" class="com.example.spring.model.Name_XML_Bean"></bean>
<context:component-scan base-package="com.example.spring" />
</beans>
|
|
|
|
|
|
|
WebSpider member offline |
|
posts: |
147 |
joined: |
06/29/2006 |
from: |
Seattle, WA |
|
|
|
|
|
2 Ways to Use Spring Bean |
The life cycle (from instantiation to garbage collection) of Spring beans are controller by Spring IoC Container, but those beans are always standby and ready for use. There are two way to use Spring beans:
External Use -- Retrieve from the IoC Container by application Internal Use -- Fulfill the dependency need for another bean from the IoC container.
|
|
|
|
|
|
|
WebSpider member offline |
|
posts: |
147 |
joined: |
06/29/2006 |
from: |
Seattle, WA |
|
|
|
|
|
Way#1 Retrieve bean from the IoC Container by application |
First, the IoC Container context should be generated via the corresponding configuration: For XML configuration based bean -- context = new ClassPathXmlApplicationContext("app-config.xml"); For Java configuration based bean -- context = new AnnotationConfigApplicationContext(BeanConfig.class); For annotation based bean -- context = new FileSystemXmlApplicationContext("/path/app-config.xml");
Secondly, retrieve bean from the IoC Container via the context
Name_XML_Bean bean = context.getBean(Name_XML_Bean.class);
|
|
|
|
|
|
|
WebSpider member offline |
|
posts: |
147 |
joined: |
06/29/2006 |
from: |
Seattle, WA |
|
|
|
|
|
Way#2 Fulfill the dependency need for another bean from the IoC container |
package com.example.spring.model;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class Employee {
private int id;
@Autowired
private Name_XML_Bean name;
@Autowired
private Address_Configure_Bean address;
@Autowired
private Sex_Annotation_Bean sex;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name.setName(name);
}
public void setAddress(String address) {
this.address.setAddress(address);
}
public void setSex(String sex) {
this.sex.setSex(sex);
}
@Override
public String toString() {
return "Employee [id=" + id +
", name=" + name.getName() +
", address=" + address.getAddress() +
", sex=" + sex.getSex() +
"]";
}
}
Note: There are three ways to do dependency injection (DI) via @Autowired: constructor/setter/field -- here @Autowired field is used. Usually Name_XML_Bean, Address_Configure_Bean and Sex_Annotation_Bean are interfaces without knowing the concrete implementation until run-time. The owner (this class -- Employee) must be a bean (@Component, @Controller, @Service or @Repository) inside IoC Container as well; otherwise the DI via @Autowired is doing nothing.
|
|
|
|
|
|
|