Sring-bean

Sring-bean

Bean的常见属性和子元素

标签描述
idid编号
namename可以作为多个Bean指定名称,每个名称之间用逗号隔开
class完整的类名
scope作用域:singleTon,prototype,request,session,global Session,application和websocket
constructor-arg构造,index,ref,value
propertyref, 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类型在进行装配
Resourcename 或者 type
Qualifier按照Bean的name类型在进行装配

自动装配

类中对象的引用需要提供set 方法,类本身的调用都需要在xml中配置

Spring的元素包含的一个autowire属性,通过设置autowire属性值来自动装配bean。

属性值说明
default
byName根据属性的名称自动装配
ByType根据属性的数据类型自动装配
constructor根据构造函数参数的数据类型,通过byType进行装配

beans default-autowire=“byName”> |
| byName | 根据属性的名称自动装配 |
| ByType | 根据属性的数据类型自动装配 |
| constructor | 根据构造函数参数的数据类型,通过byType进行装配 |


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_Java123

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值