1.1 Bean基本语法
id: bean的唯一标识,类似于身份证号
name: bean的名字,类似于人名,可以有重复的名字。
当出现id和name相同时,Ioc容器会自动消除重复。
为什么id和name同时存在?都是标识,只使用一个呢??
—使用id的好处就是可以很方便的使用XML的id属性提前得知bean是否存在以及是否重名。
—那直接使用id就好了,为什么还要使用name属性呢?
alias:别名
import:引入其他bean
singleton 与 non-singleton:
默认的bean是singleton的模式,即只有一个共享的实例存在,即对所有该bean请求返回这个唯一的实例。
如果为non-singleton,每次对bean的请求都会创建一个新的bean实例。即 new 操作。
depends-on
depends-on属性用来指定初始化该bean之前,先初始化依赖的bean。在销毁该bean之前先销毁依赖的bean。
1.2 Ioc如何实例化Bean
传统的应用程序实例化bean时利用 new 或 反射机制来实现。
Ioc容器则利用Bean中声明的类名通过反射机制来进行实例化。
大多数框架会使用反射。
Ioc实现三种实例化Bean的方式:
通过构造器实例化bean
- create a class include default constructor and other constrctor
package com.myspring.helloworld;
public class HelloConstrustor implements HelloApi{
private String message;
public HelloConstrustor(){
this.message = "hello default construstor!";
}
public HelloConstrustor(String message){
this.message = message;
}
@Override
public void sayHelo() {
System.out.println(message);
}
}
- create xml
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- id 表示你这个组件的名字,class表示组件类-->
<!-- 使用默认的构造器-->
<bean name="default_constructor" class="com.myspring.helloworld.HelloConstrustor"> </bean>
<!-- 使用带参数的构造器-->
<bean name="argu_constructor" class="com.myspring.helloworld.HelloConstrustor">
<!--指定构造器的参数-->
<constructor-arg index="0" value = "hello argu construstor"/>
</bean>
</beans>
- create test class
BeanFactory factory = new ClassPathXmlApplicationContext("helloconstructor.xml");
//面向接口编程
HelloApi helloapi = factory.getBean("default_constructor", HelloApi.class);
helloapi.sayHelo();
HelloApi helloapi1 = factory.getBean("argu_constructor", HelloApi.class);
helloapi1.sayHelo();`
通过静态工厂的方式实例化bean
- create static factory
package com.myspring.helloworld;
public class HelloStaticFactory {
public static HelloApi newInstance(String message){
return new HelloConstrustor(message);
}
}
- create xml
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- id 表示你这个组件的名字,class表示组件类-->
<!-- 使用静态工厂方式-->
<bean id="static_factory" class="com.myspring.helloworld.HelloStaticFactory" factory-method="newInstance">
<!--指定构造器的参数-->
<constructor-arg index="0" value = "hello static factory construstor"/>
</bean>
</beans>
- create test class
BeanFactory factory = new ClassPathXmlApplicationContext("hellostaticfactory.xml");
//面向接口编程
HelloApi helloapi = factory.getBean("static_factory", HelloApi.class);
helloapi.sayHelo();
通过实例工厂的方式实例化bean
- create instance factory class
package com.myspring.helloworld;
public class HelloInstanceFactory {
public HelloApi newInstance(String message){
return new HelloConstrustor(message);
}
}
- create xml
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!—1、定义实例工厂Bean -->
<bean id="beanInstanceFactory" class="com.myspring.helloworld.HelloInstanceFactory"/> </bean>
<!—2、使用实例工厂Bean创建Bean -->
<bean id="instance_factory" factory-bean="beanInstanceFactory" factory-method="newInstance">
<constructor-arg index="0" value="Hello instance factory!"></constructor-arg>
</bean>
</beans>
- create test class
BeanFactory factory = new ClassPathXmlApplicationContext("helloinstancefactory.xml");
//面向接口编程
HelloApi helloapi = factory.getBean("instance_factory", HelloApi.class);
helloapi.sayHelo();