spring实战-显式装配bean
来源:互联网 发布:淘宝详情页设计多少钱 编辑:程序博客网 时间:2024/04/30 19:30
以下实例说明了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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/utilhttp://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
- spring实战-显式装配bean
- Spring实战-装配bean
- spring实战-自动装配bean
- spring实战-注解装配bean
- spring实战-JavaConfig装配bean
- spring实战-混合装配bean
- spring实战-条件装配bean
- spring实战-注解装配bean
- Spring实战(装配Bean)
- Spring的《XML显式装配bean》- XML自动装配
- Spring的《XML显式装配bean》- 默认自动装配
- Spring实战笔记2---Bean的装配
- Spring实战读书笔记 第二章 装配Bean
- [学习笔记]spring实战-装配Bean
- spring实战-声明,装配Bean(一)
- Spring实战笔记——装配Bean
- Spring实战之二:装配Bean
- Spring实战--(二)装配Bean
- Children of the Candy Corn(搜索方向变化问题)(bfs)(dfs)
- ionic(一)初始化项目
- 七月小记
- 睡不着
- C语言单向链表的实现
- spring实战-显式装配bean
- 莫比乌斯反演
- 注解,泛型,反射的小应用:生成增删改查SQL的语句
- Prime Path 变素数问题(bfs)
- Android 开发遇坑备忘录(二)
- 【反向代理】跨域解决方式之一——反向代理
- 树莓派做web服务器(nginx、Apache)
- 初识Linux操作系统
- asdasdasd