spring实战-显式装配bean

以下实例说明了spring中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" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context.xsd
	http://www.springframework.org/schema/util
	http://www.springframework.org/schema/util/spring-util.xsd"
	default-init-method="init" default-destroy-method="destroy">
	<!-- 以上配置默认初始化和销毁函数 -->
	
	<!-- 基于无参构造函数的bean实例化 -->
	<bean id="proxy1" class="com.halfworlders.proxy.ProxyInfo">
	</bean>

	<!-- 基于带参构造函数的bean实例化 -->
	<bean id="connection1" class="com.halfworlders.web.Connection">
		<constructor-arg ref="5"></constructor-arg>
		<constructor-arg ref="proxy1"></constructor-arg>
	</bean>

	<!-- 基于无参工厂方法的bean实例化 -->
	<bean id="connection2" class="com.halfworlders.web.ConnectionFactory"
		factory-method="newConnection">
	</bean>

	<!-- 基于有参工厂方法的bean实例化 -->
	<bean id="connection3" class="com.halfworlders.web.ConnectionFactory"
		factory-method="newConnection">
		<constructor-arg ref="proxy1"></constructor-arg>
	</bean>

	<!-- 默认情况下,bean都是singleton,如果需要每次获取bean都创建一个新实例,需要设置 scope="prototype" -->
	<!-- 作用域包括singleton,prototype,request,session,global-session -->
	<bean id="newProxy" class="com.halfworlders.proxy.ProxyInfo"
		scope="prototype">
	</bean>

	<!-- 为bean提供初始化,和销毁前方法 -->
	<!-- 也可以通过实现InitializingBean接口和DisposableBean接口,来实现bean的初始化和销毁 -->
	<bean id="connection4" class="com.halfworlders.web.Connection"
		init-method="init" destroy-method="destroy">
	</bean>

	<!-- 基本类型的属性装配 -->
	<bean id="proxy2" class="com.halfworlders.proxy.ProxyInfo">
		<property name="ip" value="127.0.0.1"></property>
		<property name="port" value="8080"></property>
	</bean>

	<!-- 引用类型的属性装配 -->
	<bean id="service1" class="com.halfworlders.web.Service">
		<property name="connection" ref="connection4"></property>
	</bean>

	<!-- 注入内部Bean -->
	<bean id="service2" class="com.halfworlders.web.Service">
		<property name="connection">
			<bean class="com.halfworlders.web.Connection" />
		</property>
	</bean>

	<!-- spring集合包括<list><set><map><props> -->
	
	<!-- 注入list属性 -->
	<!-- <list>成员包括<value><bean><null/>,还可以包含另外一个<list -->
	<bean id="service3" class="com.halfworlders.web.Service">
		<property name="urls">
			<list>
				<value>https://www.baidu.com/</value>
				<value>http://www.csdn.net</value>
			</list>
		</property>
	</bean>
	
	<!-- 注入map属性 -->
	<!-- <map>中<entity>键和值可以是key,key-ref,value,value-ref -->
	<bean id="service4" class="com.halfworlders.web.Service">
		<property name="response">
			<map>
				<entry key="name" value="halfworlder"></entry>
				<entry key="age" value="1024"></entry>
			</map>
		</property>
	</bean>

</beans>

ProxyInfo

public class ProxyInfo {
	private String ip;
	private int port;
	
	public String getIp() {
		return ip;
	}
	public void setIp(String ip) {
		this.ip = ip;
	}
	public int getPort() {
		return port;
	}
	public void setPort(int port) {
		this.port = port;
	}
}
Connection

public class Connection {
	private ProxyInfo proxyInfo;
	private int timeOut;
	public Connection(){
	}
	
	public Connection(ProxyInfo proxy){
		this.proxyInfo = proxy;
	}
	
	public Connection(int timeout, ProxyInfo proxy){
		this.timeOut = timeout;
		this.proxyInfo = proxy;
	}
	
	public void init(){
		System.out.println("init");
	}
	
	public void destroy(){
		System.out.println("destroy");
	}
}
ConnectionFactory
public class ConnectionFactory {

	public static Connection newConnection(){
		return new Connection();
	}
	
	public static Connection newConnection(ProxyInfo proxy){
		return new Connection(10, proxy);
	}
}
Service

public class Service {
	private Connection connection;
	private List<String> urls;
	private Map<String, String> response;
	public Service() {
		
	}
	
	public Service(Connection connection) {
		this.connection = connection;
	}
	
	public Connection getConnection() {
		return connection;
	}

	public void setConnection(Connection connection) {
		this.connection = connection;
	}

	public List<String> getUrls() {
		return urls;
	}

	public void setUrls(List<String> urls) {
		this.urls = urls;
	}

	public Map<String, String> getResponse() {
		return response;
	}

	public void setResponse(Map<String, String> response) {
		this.response = response;
	}
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值