展开
拓展 关闭
订阅号推广码
GitHub
视频
公告栏 关闭

spring5入门(三):基于xml配置管理bean,创建对象和属性注入

前言

  • Bean管理操作有两种方式
(1)基于 xml 配置文件方式实现
(2)基于注解方式实现
  • 基于xml配置管理对象
# 编写实体类

# 在src路径下编写bean.xml
<bean id="user" class="com.ychen.spring.User"></bean>

# 使用 bean 标签,标签里面添加对应属性,就可以实现对象创建
# 在 bean 标签有很多属性
  * id 属性:唯一标识
  * class 属性:类全路径

# 创建对象时,默认执行无参数构造方法完成对象创建

# 最后通过如下方式获取对象
ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
User user = context.getBean("user", User.class);
  • 拓展
# 在实体类中编写有参构造方法
public class User {

    private String name;

    public User(String name) {
        this.name = name;
    }

    public void add() {
        System.out.println("add......");
    }

}


# xml配置
<bean id="user" class="com.ychen.spring.model.User"></bean>

# 执行test方法
    @Test
    public void testAdd() {
        ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
        User user = context.getBean("user", User.class);
        System.out.println(user);
        user.add();
    }

# 控制台报错
4月 17, 2022 9:41:50 上午 org.springframework.context.support.AbstractApplicationContext refresh
警告: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'user' defined in class path resource [bean1.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.ychen.spring.model.User]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.ychen.spring.model.User.<init>()

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'user' defined in class path resource [bean1.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.ychen.spring.model.User]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.ychen.spring.model.User.<init>()

# 错误原因:实体类中是有参构造方法,而在bean.xml中只能通过无参构造方法创建对象

使用set方法注入

  • 基于xml注入属性
DI:依赖注入,就是注入属性
  • 在实体类中使用set方法注入
# 新建1个实体类Book
public class Book {

    private String bname;

    private String bauthor;

    public void setBname(String bname) {
        this.bname = bname;
    }

    public void setBauthor(String bauthor) {
        this.bauthor = bauthor;
    }

    // main方法中测试注入属性
    public static void main(String[] args) {
        Book book = new Book();
        book.setBauthor("goudan");
        System.out.println(book);
    }

}

# main方法中测试
com.ychen.spring.model.Book@5b464ce8

Process finished with exit code 0
  • 基于xml配置注入属性
# 编写实体类
public class Book {

    private String bname;

    private String bauthor;

    private String address;

    public void setBname(String bname) {
        this.bname = bname;
    }
    public void setBauthor(String bauthor) {
        this.bauthor = bauthor;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public void testDemo() {
        System.out.println(bname+"::"+bauthor+"::"+address);
    }

}

# 编写bean.xml
    <bean id="book" class="com.ychen.spring.model.Book">
        <property name="bname" value="易筋经"></property>
        <property name="bauthor" value="达摩老祖"></property>
    </bean>

# 编写测试方法
    @Test
    public void testBook1() {
        ApplicationContext context =
                new ClassPathXmlApplicationContext("bean1.xml");
        Book book = context.getBean("book", Book.class);
        System.out.println(book);
        book.testDemo();
    }

# 测试
com.ychen.spring.model.Book@2cb4893b
易筋经::达摩老祖::null

使用构造方法注入

  • 使用有参构造方法注入属性
# 编写实体类
public class Orders {

    private String oname;

    private String address;

    public Orders(String oname,String address) {
        this.oname = oname;
        this.address = address;
    }

    public static void main(String[] args) {
        Orders orders = new Orders("aaa", "bbb");
        System.out.println(orders);
    }

}

# 测试结果
com.ychen.spring.model.Orders@57829d67

Process finished with exit code 0
  • 基于xml配置注入属性
# 编写实体类
public class Orders {

    private String oname;

    private String address;

    public Orders(String oname,String address) {
        this.oname = oname;
        this.address = address;
    }

    public void ordersTest() {
        System.out.println(oname+"::"+address);
    }

}

# 配置bean.xml
    <bean id="orders" class="com.ychen.spring.model.Orders">
        <constructor-arg name="oname" value="电脑"></constructor-arg>
        <constructor-arg name="address" value="China"></constructor-arg>
    </bean>

# 编写测试类
    @Test
    public void testOrders() {
        ApplicationContext context =
                new ClassPathXmlApplicationContext("bean1.xml");
        Orders orders = context.getBean("orders", Orders.class);
        System.out.println(orders);
        orders.ordersTest();
    }

# 测试
Connected to the target VM, address: '127.0.0.1:58847', transport: 'socket'
com.ychen.spring.model.Orders@6b695b06
电脑::China
Disconnected from the target VM, address: '127.0.0.1:58847', transport: 'socket'

Process finished with exit code 0

# 注意:这里的实体类中虽然写的是有参构造方法,创建对象时没有报错
# 这是因为在使用有参构造注入属性时,是使用有参构造创建对象的

set方法注入属性简写:p名称空间注入

# 实体类
public class Book {

    private String bname;

    private String bauthor;

    private String address;

    public void setBname(String bname) {
        this.bname = bname;
    }
    public void setBauthor(String bauthor) {
        this.bauthor = bauthor;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public void testDemo() {
        System.out.println(bname+"::"+bauthor+"::"+address);
    }

}

# beans标签中添加如下
xmlns:p="http://www.springframework.org/schema/p"

# bean.xml中编写如下
<bean id="book" class="com.ychen.spring.model.Book" p:bname="九阳神功" p:bauthor="无名氏">
    </bean>

# 测试类
    @Test
    public void testBook1() {
        ApplicationContext context =
                new ClassPathXmlApplicationContext("bean1.xml");
        Book book = context.getBean("book", Book.class);
        System.out.println(book);
        book.testDemo();
    }

# 测试
com.ychen.spring.model.Book@25be7b63
九阳神功::无名氏::null
posted @ 2022-04-17 09:06  DogLeftover  阅读(93)  评论(0编辑  收藏  举报