以下实例说明了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; } }