Sring-bean
Bean的常见属性和子元素
标签 描述 id id编号 name name可以作为多个Bean指定名称,每个名称之间用逗号隔开 class 完整的类名 scope 作用域:singleTon,prototype,request,session,global Session,application和websocket constructor-arg 构造,index,ref,value property ref, value
实例化Bean的三种方式
构造器实例化
public class Scope {
public Scope() {
super();
// TODO Auto-generated constructor stub
System.out.println("scope 构造方法...");
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd">
<bean id="scope" class="com.ruanyuan.scope.Scope" scope="singleton"></bean>
<bean id="scope1" class="com.ruanyuan.scope.Scope" scope="prototype"></bean>
</beans>
public class ScopeTest {
public static void main(String[] args) {
//1.初始化spring容器,加载配置文件
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("com/ruanyuan/scope/bean.xml");
//2.通过容器获取实例
Scope scope = (Scope)applicationContext.getBean("scope1");
System.out.println(scope);
Scope scope1 = (Scope)applicationContext.getBean("scope1");
System.out.println(scope1);
}
}
静态工厂方法实例化
public class Static_facory {
public static Bean createBean() {
System.out.println("静态工厂方法实例化...");
return new Bean();
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd">
<bean id="static_facory_Createbean" class="com.ruanyuan.static_facory.Static_facory" factory-method="createBean"></bean>
</beans>
实例工厂方法实例化
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd">
<bean id="bean" class="com.ruanyuan.factory.Bean"></bean>
<bean id="getTest" factory-bean="bean" factory-method="getTest"></bean>
</beans>
public class Bean {
public Bean() {
super();
System.out.println("构造方法...");
}
public Factory getTest() {
System.out.println("测试方法...");
return new Factory();
}
}
Bean的作用域和生命周期
作用域 (bean中默认的作用域是单例)
作用域名称 描述 singleton 单例 prototype 原型 request 在一次HTTP,容器会返回Bean同一实例。不同的HTTP,容器产生新的Bean session 在HTTP Session内有效 globalSession 在portlet 上下文有效
Bean的生命周期
Bean的三种装配方式
XMl装配
<bean id="user" class="com.ruanyuan.After07.User">
<property name="id" value="001"></property>
<property name="username" value="MIKE"></property>
<property name="address" value="河北石家庄"></property>
<property name="orderList">
<list>
<ref bean="order"/>
<ref bean="order1"/>
</list>
</property>
<property name="map">
<map>
<entry key="1" value-ref="order"></entry>
<entry key="2" value-ref="order1"></entry>
</map>
</property>
<property name="properties">
<props>
<prop key="1">MyBatis</prop>
<prop key="2">Spring</prop>
</props>
</property>
</bean>
public class Order {
private int id;
private String number;
private User user;
}
public class User {
private int id;
private String username;
private String address;
private List<Order> orderList;
private Map<Integer,Order> map;
private Properties properties;
}
Annotation装配
@注解 配置 Commonent 泛化概念,代表一个组件Bean Repository 数据访问层 Service 业务层 Controller 控制层 Autowired 按照Bean的type类型在进行装配 Resource name 或者 type Qualifier 按照Bean的name类型在进行装配
自动装配
类中对象的引用需要提供set 方法,类本身的调用都需要在xml中配置
Spring的元素包含的一个autowire属性,通过设置autowire属性值来自动装配bean。
属性值 说明 default byName 根据属性的名称自动装配 ByType 根据属性的数据类型自动装配 constructor 根据构造函数参数的数据类型,通过byType进行装配
beans default-autowire=“byName”> | | byName | 根据属性的名称自动装配 | | ByType | 根据属性的数据类型自动装配 | | constructor | 根据构造函数参数的数据类型,通过byType进行装配 |