Spring Bean xml显式装配之构造函数

springframework 专栏收录该内容
14 篇文章 0 订阅

对于喜欢用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.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
		http://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);
	@Test
	public 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
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 程序猿惹谁了 设计师:白松林 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值