Spring Bean xml显式装配之构造函数
来源:互联网 发布:ubuntu怎么看硬盘大小 编辑:程序博客网 时间:2024/05/22 02:26
对于喜欢用xml配置文件来说,xml装配bean
例子还是老师注入学生bean
1.老师和学生接口
package com.jack.bean.chatpter2.autowiredBeanJava;public interface Student {void learn();}
package com.jack.bean.chatpter2.autowiredBeanJava;public interface Teacher {void teach();}
2.老师和学生实现类
package com.jack.bean.chatpter2.autowiredBeanJava;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.stereotype.Component;public class StudentXiaoMing implements Student{Logger logger = LoggerFactory.getLogger(StudentXiaoMing.class);public void learn() {logger.info("小明同学正在学习");}}
package com.jack.bean.chatpter2.autowiredBeanJava;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.stereotype.Component;public class TeacherWang implements Teacher {private Student student;public TeacherWang(Student student){this.student = student;}Logger logger = LoggerFactory.getLogger(TeacherWang.class);public void teach() {logger.info("王老师在教学");}public Student getStudent() {return student;}public void setStudent(Student student) {this.student = student;}}
3.关键配置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"xmlns:c="http://www.springframework.org/schema/c"xmlns:util="http://www.springframework.org/schema/util"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:context="http://www.springframework.org/schema/context"xmlns:ehcache="http://www.springmodules.org/schema/ehcache"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsdhttp://www.springmodules.org/schema/ehcache http://www.springmodules.org/schema/cache/springmodules-ehcache.xsd"><bean id="student" class="com.jack.bean.chatpter2.autowiredBeanJava.StudentXiaoMing"></bean>
<!--方式一 构造函数参数名一--><!-- <bean id="teacher" class="com.jack.bean.chatpter2.autowiredBeanJava.TeacherWang" c:student-ref="student"></bean> -->
<!--方式二 构造函数参数的索引从0开始一--><!-- <bean id="teacher" class="com.jack.bean.chatpter2.autowiredBeanJava.TeacherWang" c:_0-ref="student"></bean> --><!--方式三 构造函数加子标签一--><bean id="teacher" class="com.jack.bean.chatpter2.autowiredBeanJava.TeacherWang"> <constructor-arg ref="student"></constructor-arg> </bean> </beans>4.测试类
package chapter2;import org.junit.Test;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.jack.bean.chatpter2.autowiredBeanJava.Student;import com.jack.bean.chatpter2.autowiredBeanJava.TeacherWang;public class ContructorTest {Logger logger = LoggerFactory.getLogger(ContructorTest.class);@Testpublic void contructor(){ApplicationContext context = new ClassPathXmlApplicationContext("spring/chapter2/constructor.xml");TeacherWang teacher = (TeacherWang) context.getBean("teacher");Student student = teacher.getStudent();student.learn();}}5.效果
阅读全文
0 0
- Spring Bean xml显式装配之构造函数
- Spring的《XML显式装配bean》之通过构造器注入Bean
- Spring的《XML显式装配bean》- XML自动装配
- Spring的《XML显式装配bean》- 默认自动装配
- Spring的《XML显式装配bean》之设置bean的作用域
- Spring的《XML显式装配bean》之Bean的创建与销毁操作
- Spring的《XML显式装配bean》之通过属性property注入bean
- Spring实例解析之XML装配Bean
- Spring之通过XML装配bean(三)
- Spring学习笔记(四) --- 装配Bean之通过XML装配
- Spring的《XML显式装配bean》- 注入Map
- Spring之装配Bean
- Spring笔记 - Bean xml装配
- spring装配Bean(基于xml)
- Spring 装配Bean 基于XML
- spring装配Bean(基于xml)
- Spring Bean装配之自动装配
- Spring装配Bean——通过配置XML装配bean
- hibernate学习笔记04----检索对象的方式
- innodb buffer pool管理--LRU调整
- 银行家算法c++课程设计
- linux 下线程的创建
- 前端优化之-css与js阻塞问题
- Spring Bean xml显式装配之构造函数
- python 字典用法
- SecureCRT安装
- Python中if __name__ == "__main__": 的作用
- 通过style实现从屏幕底下弹出dialog动画效果
- ElasticSearch搜索引擎java客户端接口详解
- Notepad++配色
- C语言const:禁止修改变量的值
- 使用python进行数据分析介绍及部分教程