天天看点

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

对于喜欢用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.效果

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