• 方法一:基于XML的bean定义(需要提供setter方法)

1.首先编写student.java和teacher.java两个类

Student.java:

public class Student {

private String name;

private Teacher teacher;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Teacher getTeacher() {
return teacher;
}

public void setTeacher(Teacher teacher) {
this.teacher = teacher;
}
}

Teacher.java

public class Teacher {

private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

}

2.配置spring.xml基于XML的bean定义(需要提供setter方法)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="student" class="test.Student">
<property name="name" value="张三"/>             /设置属性值
<property name="teacher" ref="teacher"/>
</bean>

<bean id="teacher" class="test.Teacher">
<property name="name" value="李四"/>          //设置属性值
</bean>
</beans>

  • 方法二:基于注解的bean定义(不需要提供setter方法)

    @Component("teacher")
    public class Teacher {

    @Value("李四")
    private String name;

    public String getName() {
    return name;
    }

    }
    @Component("student")
    public class Student {

    @Value("张三")
    private String name;

    @Resource
    private Teacher teacher;

    public String getName() {
    return name;
    }

    public Teacher getTeacher() {
    return teacher;
    }

    <?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:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">

    <!--扫描组件的包目录-->
    <context:component-scan base-package="test"/>

    </beans>

  • 方法三:基于Java类的bean定义(需要提供setter方法)

    @Configuration
    public class BeansConfiguration {

    @Bean
    public Student student(){
    Student student=new Student();
    student.setName("张三");
    student.setTeacher(teacher());
    return student;
    }

    @Bean
    public Teacher teacher(){
    Teacher teacher=new Teacher();
    teacher.setName("李四");
    return teacher;
    }

    }

入口函数为:

public class Main {

public static void main(String args[]){
AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext(BeansConfiguration.class);
Student student= (Student) context.getBean("student");
Teacher teacher= (Teacher) context.getBean("teacher");
System.out.println("学生的姓名:"+student.getName()+"。老师是"+student.getTeacher().getName());
System.out.println("老师的姓名:"+teacher.getName());
}

}

最新文章

  1. asp.net GDI+ 绘制椭圆 ,弧线,扇形
  2. 浅析Ajax跨域原理及JQuery中的实现分析
  3. C语言学习001:让程序跑起来
  4. WebView中实现文件下载功能
  5. web工程spring+ibatis单元测试
  6. nyoj-204
  7. Hibernate &lt;查询缓存&gt;
  8. XE3随笔9:使用不同的数据类型标记数组
  9. Excel中的表单控件和active控件
  10. C++中rapidxml用法及例子
  11. [置顶] DataGridView控件---绑定数据方法
  12. baiduMap 显示所有的marker(在视野里显示所有的)
  13. Python IDLE 快捷键
  14. AngularJs ui-router 路由的介绍
  15. 第27篇 重复造轮子---模拟IIS服务器
  16. Struts+jdbc+分页 实例
  17. 动态规划 POJ3616 Milking Time
  18. 03 ProgressBar 进度条
  19. iOS 10 设备权限问题(相机,相册等)
  20. HBase RegionServer Splitting 流程

热门文章

  1. canal 结合 kafka 入门
  2. 部署一个基于python语言的web发布环境
  3. 关于Bootstrap的入门知识
  4. epoll的LT和ET(转)
  5. Python单元测试unittest【转自https://www.cnblogs.com/feng0815/p/8045850.html】
  6. SocketIO Client
  7. 服务器解析慢,可以安装nscd解决
  8. 大数据入门到精通7--对复合value做reducebykey
  9. Pandas分组
  10. 498. Diagonal Traverse对角线z型traverse