创建Bean的几种方法
- XML配置,使用
<bean />
创建Bean - 注解配置,使用
@Configuration
和@Bean
创建Bean - 使用注解创建,使用
@Component、@Repository、@Service、@Controller
- 使用静态工厂
- 使用实例工厂
- 使用FactoryBean
其中第1和第 2已在第3章中实现,在此不在赘述。
使用注解创建Bean
@Component
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Component {
/**
* The value may indicate a suggestion for a logical component name,
* to be turned into a Spring bean in case of an autodetected component.
* @return the suggested component name, if any
*/
String value() default "";
}
参数Value用于定义Bean在Spring容器中的唯一ID。
在第3章种有使用,不再赘述
@Repository
@Repository用于标注数据访问组件,即DAO组件。使用方式同@Component。
@service
@Service用于标注业务层组件,即Service层。使用方式同@Component。
@Controller
@Controller用于标注控制层组件,使用方式同@Component。在SpringMVC章节中会详细讲解。
@Component、@Repository、@Service、@Controller
都只有一个Value参数,用于指定Bean在Spring容器中的唯一ID。
下面的例子中使用的Bean代码如下
package com.codestd.springstudy.bean.create;
public class People {
public void sleep(){
System.out.println("睡觉了!");
}
}
使用静态工厂
静态工厂代码如下
package com.codestd.springstudy.bean.create;
public class PeopleFactory {
public static People getPeople(){
return new People();
}
}
代码中提供一个静态方法来获取People的实例。
配置如下
<bean id="people" class="com.codestd.springstudy.bean.create.PeopleFactory" factory-method="getPeople" />
测试类
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:bean/create/applicationContext.xml"})
public class PeopleFactoryTest {
@Autowired
private People people;
@Test
public void testGetPeople() {
this.people.sleep();
}
}
使用实例工厂创建
实例工厂代码如下,实例工厂中提供一个获取Bean的方法,但是不是静态的。工厂需要先实例化然后才能通过方法获取Bean。
package com.codestd.springstudy.bean.create;
public class PeopleFactory {
public People getPeople(){
return new People();
}
}
配置方式如下
<bean id="peopleFactory" class="com.codestd.springstudy.bean.create.PeopleFactory" />
<bean id="people" factory-bean="peopleFactory" factory-method="getPeople" />
测试方法同上。
使用FactoryBean创建Bean
FactoryBean.class源码(已省略注释部分)
package org.springframework.beans.factory;
public interface FactoryBean<T> {
//获取Bean实例
T getObject() throws Exception;
//获取Bean类型
Class<?> getObjectType();
//是否使用单例模式
boolean isSingleton();
}
实现如下
package com.codestd.springstudy.bean.create;
import org.springframework.beans.factory.FactoryBean;
public class PeopleFactoryBean implements FactoryBean<People> {
@Override
public People getObject() throws Exception {
return new People();
}
@Override
public Class<?> getObjectType() {
return People.class;
}
@Override
public boolean isSingleton() {
return true;
}
}
配置如下
<bean id="people" class="com.codestd.springstudy.bean.create.PeopleFactoryBean" />
如果Bean创建的时候带有属性,可以将属性写入到FactoryBean类,然后在创建Bean时,注入属性。如下:
//在People中加入name属性
private String name;
/**************************************/
//修改PeopleFactoryBean中创建实例部分,其他不变
private String name;
public void setName(String name) {
this.name = name;
}
@Override
public People getObject() throws Exception {
People p = new People();
p.setName(name);
return p;
}
Bean配置的代码如下
<bean id="people" class="com.codestd.springstudy.bean.create.PeopleFactoryBean" >
<property name="name" value="codestd" />
</bean>
测试代码如下
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:bean/create/applicationContext.xml"})
public class PeopleFactoryTest {
@Autowired
private People people;
@Test
public void testGetPeople() {
assertEquals("codestd", people.getName());
}
}