spring中有4种默认标签:import,alias,bean,beans;以下是各个标签中属性以及子元素配置的作用和用法。
(本人使用的是spring 4.2,若有错误,烦请指正)
bean标签
标签属性
id
id是bean的唯一标识符,在spring容器中不可能同时存在两个相同的id;
class
类的全限定名(包名+类名),用“.”号连接;
name
别名(alias),用法:getBean("name"),支持设置多个别名,之间用英文逗号分割;
abstract
设置bean是否为抽象类,默认abstract="false",如果设为true,将不能被实例化;
autowire-candidate
默认为true,如果为false,那么该bean不能作为其他bean自动装配的候选者。不懂?请看下面的例子:
定义一个UserService,UserServiceImpl继承它:
UserService
@Service
public interface UserService {
public void getUser();
}
UserServiceImpl
public class UserServiceImpl implements UserService{
@Override
public void getUser() {
System.out.println("获得一个user");
}
}
在applicationContext.xml设置UserServiceImpl的属性autowire-candidate为false,此时该bean不能作为UserService自动装配的对象。
<bean id="service" class="com.bcu.service.UserServiceImpl" autowire-candidate="false"></bean>
测试类:
@Autowired
@Qualifier("service")
private UserService service2;
此时将会报以下错误:大概意思是没有一个合适的子类能够自动注入到service2中。
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.bcu.service.UserService com.bcu.test.UnitTets.service2; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.bcu.service.UserService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=service)}
如果将autowire-candidate改为true,此时id="service"的bean实例化后的对象指向service2引用。
autowire
default(默认):采用父级标签beans中的default-autowire属性;
byName:通过属性名称来自动装配,即A类中的B对象名称为name,那么将根据id="name"找到该bean进行装配,A类必须提供setName方法;
byType:根据属性类型来找到和配置文件中配置的class类型一致的bean来自动装配,如果找到多个类型一致的bean,则抛异常,如果一个都没有找到,则不执行装配操作,也不抛出异常。
no:不执行自动装配操作,只能用<ref标签进行装配;
constructor:根据构造器中参数类型来自动装配,如果找到多个类型一致的bean,则抛异常,如果一个都没有找到,则不执行装配操作,但是抛出异常(这是和byType不一样的地方)。
“autodetect”(spring3之前有该值,从spring4开始该值被抛弃):通过Bean类的反省机制(introspection)决定是使用“constructor”还是使用“byType”。
depends-on
它的作用是一个bean实例化的过程需要依赖于另一个bean的初始化,也就是说被依赖的bean将会在需要依赖的bean初始化之前加载。多个依赖bean之间用","号分割;
destroy-method
它的作用是在销毁bean之前可以执行指定的方法。注意:必须满足scope="singleton",并且destroy方法参数个数不能超过1,并且参数类型只能为boolean。
init-method
它的作用是在创建一个bean之后调用该方法,初始化方法必须是一个无参方法。
factory-bean和factory-method
设置了factory-bean属性后,将指定创建bean的工厂类对象,class属性将失效;
设置了factory-method属性后,将指定创建bean的工厂方法;
举例:
applicationContext.xml
<bean id="stud" class="com.bcu.entity.Student">
<constructor-arg index="0"><value>Jack</value></constructor-arg>
<constructor-arg index="1"><value>上海市徐汇区陆家嘴</value></constructor-arg>
</bean>
<!-- 使用factory-bean后,class属性无效 -->
<bean id="student" class="com.bcu.entity.User" abstract="false" scope="singleton" factory-bean="stud"
factory-method="newInstance">
</bean>
Student.java
package com.bcu.entity;
public class Student {
private String name;
private String address;
public Student(String name, String address) {
this.name = name;
this.address = address;
}
public Student newInstance(){
return new Student(name, address);
}
}
lazy-init
设置bean对象是否懒加载,如果设为true,则应用第一次用到bean时才实例化对象,否则在初始化spring容器时加载单例bean对象。(非单例不实例化)
parent
指定bean的父类,class属性失效。
primary
当一个bean出现多个候选者时,设置primary="true"后,则优先使用该bean来自动装配。
scope
bean的作用范围,它包括
singleton:单例,指定该bean在spring容器中只有一个对象,所有通过getBean获得的对象都是同一个对象。
prototype:只要重新获取该bean,都将返回一个不同的对象。
request:在一次http请求中对应一个bean,类似于servlet
session:在一次会话中对应一个bean。
子标签属性
<meta>
<meta key="metaKey" value="metaValue"/>
meta标签可以通过BeanDefinition的getAttribute("metaKey")来获取值。
<lookup-method>
示例:
User:
package com.bcu.entity;
public class User {
public void showMe(){
System.out.println("i am user");
}
}
Teacher继承了User类:
package com.bcu.entity;
public class Teacher extends User{
@Override
public void showMe() {
System.out.println("i am teacher");
}
}
调用方法:
package com.bcu.test;
import com.bcu.entity.User;
public abstract class GetBeanTest {
public void showMe(){
this.getBean().showMe();
}
public abstract User getBean();
}
配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<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"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
<bean id="user" class="com.bcu.entity.User">
<constructor-arg name="name"><value>lisi</value></constructor-arg>
</bean>
<bean id="teacher" class="com.bcu.entity.Teacher">
<constructor-arg name="name"><value>lisi</value></constructor-arg>
</bean>
<bean id="getBeanTest" class="com.bcu.test.GetBeanTest">
<!-- 将getBean的返回值类型设置为"teacher"的bean,实例化 -->
<lookup-method bean="teacher" name="getBean"/>
</bean>
</beans>
测试类:
ApplicationContext ac = new ClassPathXmlApplicationContext("lookup-test.xml");
GetBeanTest test = (GetBeanTest) ac.getBean("getBeanTest");
test.showMe();
测试结果:
首先看配置文件中getBean配置了lookup-method="teacher",它的作用是使得GetBeanTest类中的getBean方法的返回类型指定为id="teacher"的bean类型。然后看一下GetBeanTest的showMe方法,它调用了getBean方法,lookup-method标签会自动重写该方法。
<replaced-method>
示例:
public class ReplacedMethodTest implements MethodReplacer{
@Override
public Object reimplement(Object obj, Method method, Object[] args)
throws Throwable {
System.out.println("我代替了原来的方法");
return null;
}
}
配置文件:
<bean id="user" class="com.bcu.entity.User">
<constructor-arg name="name"><value>lisi</value></constructor-arg>
<replaced-method name="showMe" replacer="replacemethod"></replaced-method>
</bean>
<bean id="replacemethod" class="com.bcu.test.ReplacedMethodTest"></bean>
编写一个实现MethodReplacer接口,并且重写reimplement方法的类,通过配置文件动态地替换原始方法。
<constructor-arg>
构造器参数(顺序注入),此种方式会按照顺序注入构造器的参数。(以LinkedList形式存于BeanDefinition中)
<bean id="address" class="com.bcu.entity.Address">
<constructor-arg name="addressName"><value>上海市徐汇区陆家嘴</value></constructor-arg>
</bean>
下标注入,按照下标顺序注入构造器参数,(以LinkedHashMap形式存于BeanDefinition中)
<bean id="stud" class="com.bcu.entity.Student">
<constructor-arg index="0"><value>Jack</value></constructor-arg>
<constructor-arg index="1"><value>上海市徐汇区陆家嘴</value></constructor-arg>
</bean>
<property>
自动注入jaabean的成员变量:
<bean id="mobile" class="com.bcu.entity.Mobile">
<property name="brand" value="apple"></property>
<property name="size">
<array>
<value>4.7</value>
<value>5.5</value>
<value>5.8</value>
</array>
</property>
<property name="Apps">
<list>
<value>appStore</value>
<value>weChat</value>
<value>camera</value>
</list>
</property>
<property name="price">
<map>
<entry key="4.7" value="5288"></entry>
<entry key="5.5" value="6288"></entry>
<entry key="5.8" value="7288"></entry>
</map>
</property>
</bean>
<qualifier>
指定注入的bean名称,一般不使用这种方法,而使用注解@Qualifier("bean")