1,复制项目Spring402 ,改名为Spring402-02。建包:com.cruise.entity .建类:People类,定义属性 id name age,get()和 set()方法,
package com.cruise.entity;

public class People {

    private int id;
    private String name;
    private int age;
    
    public int getId() {
       return id;
    }
    public void setId(int id) {
       this.id = id;
    }
    public String getName() {
       return name;
    }
    public void setName(String name) {
       this.name = name;
    }
    public int getAge() {
       return age;
    }
    public void setAge(int age) {
       this.age = age;
    }
}
2,在beans.xml中,定义一个bean。
<?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="people" class="com.cruise.entity.People"></bean>
  
</beans>
3. 删除没用的包:com.cruise.service包,删除没有的类:在com.cruise.test包下,只要一个test2类即可,其他的类都删除掉。
第三讲:3.1 spring 如何装配bean
4. 修改com.cruise.test包下Test2类,运行-测试
package com.cruise.test;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.cruise.entity.People;

public class Test2 {

    public static void main(String[] args) {
       ClassPathXmlApplicationContext CA = newClassPathXmlApplicationContext("beans.xml");
       People people = (People)CA.getBean("people");//people与beans.xml中的id一致
       System.out.println(people);
    }
}

相关文章:

  • 2022-01-21
  • 2022-01-11
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-03
  • 2021-06-14
猜你喜欢
  • 2021-07-10
  • 2022-12-23
  • 2021-09-25
  • 2021-09-16
  • 2021-08-29
  • 2021-04-05
相关资源
相似解决方案