zoukankan      html  css  js  c++  java
  • 三种定义bean的方式

      

    • 方法一:基于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());
    }

    }

     

    没有停止的脚步,只有倒下去的脚步
  • 相关阅读:
    Docker简单的使用命令
    iPad 3g版完美实现打电话功能(phoneitipad破解)
    提供一个免费的CSDN下载账号
    2011年一次面试的实际记录
    VC皮肤库SkinSharp 1.0.6.6的使用
    oracle 数组
    从网页抓取数据的一般方法
    IJ:Idea 常用代码
    un-公司-Continental AG(大陆集团):百科
    un-公司-维珍集团:百科
  • 原文地址:https://www.cnblogs.com/hkMblogs/p/10793174.html
Copyright © 2011-2022 走看看