Spring Bean 初级装配
来源:互联网 发布:淘宝网折800佛格女装 编辑:程序博客网 时间:2024/05/16 06:27
ApplicationCotext.xml
<?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:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"><!-- 注入Bean属性 --><bean id="mybean" class="com.zxiaoyao.beans.MyBean"><property name="name" value="张三" /><property name="age" value="100" /></bean><!-- 通过构造函数注入 --><bean id="mybean2" class="com.zxiaoyao.beans.MyBean2"><constructor-arg value="李四" /><constructor-arg value="200" /></bean><!-- 注入其他Bean --><bean id="mybean3" class="com.zxiaoyao.beans.MyBean3"><property name="my2" ref="mybean2" /></bean><!-- 注入内部 Bean --><bean id="mybean5" class="com.zxiaoyao.beans.MyBean5"><property name="my4"><bean class="com.zxiaoyao.beans.MyBean4" /></property></bean><!-- 此配置 可 适用 list 或 Array --><bean id="mybean6" class="com.zxiaoyao.beans.MyBean6"><property name="mys"><list><ref bean="mybean" /><ref bean="mybean2" /><ref bean="mybean3" /><ref bean="mybean5" /></list></property></bean><!-- 集合装配 --><!-- set --><bean id="mybean7" class="com.zxiaoyao.beans.MyBean6"><property name="mys"><set><ref bean="mybean" /><ref bean="mybean2" /><ref bean="mybean3" /><ref bean="mybean5" /></set></property></bean><!-- map --><bean id="mybean8" class="com.zxiaoyao.beans.MyBean6"><property name="mymap"><map><entry key="m" value-ref="mybean" /><entry key="m2" value-ref="mybean2" /><entry key="m3" value-ref="mybean3" /><entry key="m5" value-ref="mybean5" /></map></property></bean><!-- properties --><bean id="mybean9" class="com.zxiaoyao.beans.MyBean7" > <property name="properties"><props><prop key="s1">天下第一2人</prop><prop key="s2">天下第二2人</prop><prop key="s3">天下第三2人</prop><prop key="s4">天下第四2人</prop></props></property></bean><!-- 自动装配 此法和在 beans 节点的 default-autowire="" 属性设置 ByName、byType、constructor、 autodetect 进行全局设置 --><bean id="mybean10" class="com.zxiaoyao.beans.MyBean8" autowire="byName"></bean><bean id="mybean11" class="com.zxiaoyao.beans.MyBean8" autowire="byType"></bean><bean id="mybean12" class="com.zxiaoyao.beans.MyBean8" autowire="constructor"></bean><bean id="mybean13" class="com.zxiaoyao.beans.MyBean8" autowire="autodetect" scope="singleton" ><!-- scope属性 --><!-- singleton 定义Bean的范围为每个Spring容器一个实例 --><!-- prototype定义Bean可以被多次实例化(使用一次就创建一个实例) --><!-- request定义Bean的范围是Http请求。只有使用有Web能力的Spring上下文(例如SpringMVC)时才有效 --><!-- session定义Bean的范围是Http会话。只有使用有Web能力的Spring上下文(例如SpringMVC)时才有效 --><!-- global-session定义Bean的范围是全局Http会话。只有在portlet --></bean><!-- 工厂方法创建Bean --><bean id="methodInstance" class="com.zxiaoyao.beans.SingletonBean" factory-method="createInstance"></bean><!-- 初始化 和销毁Bean --><!-- 在beans节点可设置 调用全局初始化和销毁 --><bean id="mybean14" class="com.zxiaoyao.beans.MyBean9" init-method="initMethod" destroy-method="cleanMethod"></bean><!-- 该bean 实现 InitializingBean,DisposableBean 接口 实现 其afterPropertiesSet destroy 方法 实现 初始化和 销毁bean --><bean id="mybean15" class="com.zxiaoyao.beans.MyBean10"></bean></beans>
所需Bean代码片段 去掉了get set, toString 方法
public class MyBean implements MyBeanI{private String name;private int age;//-------------------------------public class MyBean2 implements MyBeanI{private String name;private int age;//---------------------------------------public class MyBean3 implements MyBeanI{private String name;private int age;private MyBean2 my2;//----------------------------------------public class MyBean4 implements MyBeanI{private String name;private int age;//---------------------------------------public class MyBean5 implements MyBeanI{private String name;private int age;private MyBean4 my4;//---------------------------------------public class MyBean6 implements MyBeanI {private String name;private int age;private MyBean my;private Collection<MyBeanI> mys;private Map<String, MyBeanI> mymap;//------------------------------------------------public class MyBean7 implements MyBeanI{private Properties properties;@Overridepublic String toString() {StringBuilder sb250 = new StringBuilder();for(Iterator<Object> it = properties.keySet().iterator(); it.hasNext();){String key = (String)it.next();sb250.append("["+key+" : "+properties.getProperty(key)+"]").append("\n");}return sb250.toString();}//---------------------------------------------public class MyBean8 implements MyBeanI {private MyBean2 mybean2;//--------------------------------------------public class MyBean9 implements MyBeanI {private String name;private int age;public void initMethod(){System.out.println("init anyting....");}public void cleanMethod(){System.out.println("clean all........");}//---------------------------------------------public class MyBean10 implements MyBeanI,InitializingBean,DisposableBean {private String name;private int age;public void initMethod(){System.out.println("init anyting. 10...");}public void cleanMethod(){System.out.println("clean all....10....");}public void afterPropertiesSet() throws Exception {initMethod();}public void destroy() throws Exception {cleanMethod();}//----------------------------------------------public interface MyBeanI {}//----------------------------------------------public class SingletonBean {private String name;private int age;private SingletonBean() {this.name = "the张三";this.age = 101;}private static class InnerSingletonClass {static SingletonBean instance = new SingletonBean();}public static SingletonBean createInstance() {return InnerSingletonClass.instance;}
Test 类
public class Mytest {public static void main(String[] args) throws InterruptedException {test1();}public static void test1(){ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");MyBean myb = (MyBean)context.getBean("mybean");System.out.println(myb.getName());MyBean2 myb2 = (MyBean2)context.getBean("mybean2");System.out.println(myb2);MyBean3 myb3 = (MyBean3) context.getBean("mybean3");System.out.println(myb3.getMy2().toString());MyBean5 myb5 = (MyBean5) context.getBean("mybean5");System.out.println(myb5.getMy4().toString());MyBean6 myb6 = (MyBean6) context.getBean("mybean6");System.out.println(myb6.getMys());MyBean6 myb7 = (MyBean6) context.getBean("mybean7");System.out.println(myb7.getMys());MyBean6 myb8 =(MyBean6) context.getBean("mybean8");System.out.println(myb8.getMymap().get("m2"));MyBean7 myb9 = (MyBean7)context.getBean("mybean9");System.out.println(myb9.toString());MyBean8 myb10 = (MyBean8)context.getBean("mybean10");System.out.println(myb10.getMybean2());MyBean8 myb11 = (MyBean8)context.getBean("mybean11");System.out.println(myb11.getMybean2());MyBean8 myb12 = (MyBean8)context.getBean("mybean12");System.out.println(myb12.getMybean2());MyBean8 myb13 = (MyBean8)context.getBean("mybean13");System.out.println(myb13.getMybean2());SingletonBean sb = (SingletonBean) context.getBean("methodInstance");System.out.println(sb);DisposableBean db = (DisposableBean)context;try {db.destroy();} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}}
- Spring Bean 初级装配
- Spring Bean装配-自动装配
- spring 基本Bean装配
- Spring Bean 高级装配
- Spring中装配bean
- Spring bean装配
- spring装配bean
- Spring笔记---装配Bean
- Spring 装配Bean
- Spring配置文件 装配Bean
- Spring-Bean的装配
- Spring之装配Bean
- Spring装配bean
- Spring Bean装配-resouce
- spring(2)装配Bean
- Spring学习-装配Bean
- spring装配Bean
- spring IOC装配bean
- PLSQL 连接Oracle 数据集乱码
- StringTemplate详谈
- struts2.2 引入json与jquery交互
- WINDOWS8 CONSUMERPREVIEW 虚拟机安装与体验
- GNU C 、ANSI C、标准C、标准c++的区别和联系
- Spring Bean 初级装配
- 第一篇---CM3骨架show
- tableview cell
- Ubuntu 11.04 切换到经典桌面GNOME
- FreeCommander中设置Cygwin起始目录
- Hibernate之update(2)——报错query must begin with SELECT or FROM
- 关于/dev/null及用途
- C语言复习之排序方法
- PowerDesigner + 反向工程 + 数据字典