//person接口
package org.person.dao;
public interface IPerson {
public String sayhello();
}
//teacher类
package org.person.model;
import org.person.dao.IPerson;
import org.springframework.stereotype.Service;
@Service
public class Teacher implements IPerson {
@Override
public String sayhello() {
// TODO Auto-generated method stub
return "我是一个老师!!";
}
}
//student类
package org.person.model;
import org.person.dao.IPerson;
import org.springframework.stereotype.Service;
@Service
public class Student implements IPerson{
@Override
public String sayhello() {
// TODO Auto-generated method stub
return "我是一个学生!!";
}
}
//oneclass类
package org.person.model;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service("oneclass")
public class OneClass {
private Student student;
private Teacher teacher;
@Autowired
public void setStudent(Student student) {
this.student = student;
}
@Autowired
public void setTeacher(Teacher teacher) {
this.teacher = teacher;
}
public void say(){
System.out.println("the student say:" + student.sayhello() + " the teacher say:"+teacher.sayhello());
}
}
@Service注释表示定义一个bean,自动根据bean的类名实例化一个首写字母为小写的bean,例如Student实例化为studnet,Teacher实例化为teacher,如果需要自己改名字则:@Service("你自己改的bean名")。
beans.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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config />
<context:component-scan base-package="*" /><!-- *表示所有类 -->
</beans>
测试类:
package org.person.test;
import org.person.model.OneClass;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
@org.junit.Test
public void say(){
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
OneClass oneclass = (OneClass)context.getBean("oneclass");
oneclass.say();
}
}
结果:
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@4b035d: startup date [Fri May 18 10:42:47 CST 2012]; root of context hierarchy
2012-5-18 10:42:47 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [beans.xml]
2012-5-18 10:42:48 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@5d391d: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,oneclass,student,teacher,org.springframework.context.annotation.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor#0]; root of factory hierarchy
the student say:我是一个学生!! the teacher say:我是一个老师!!