天天看點

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顯式裝配之構造函數