0
点赞
收藏
分享

微信扫一扫

IOC创建对象的方式

Sophia的玲珑阁 2022-01-05 阅读 61
javaspring

创建pojo

package com.web.pojo;

public class Blog {
    private String title;
    private int views;
    private String author;

    public Blog() {
        System.out.println("调用了Blog无参构造");
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public void setViews(int views) {
        this.views = views;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    @Override
    public String toString() {
        return "Blog{" +
                "title='" + title + '\'' +
                ", views=" + views +
                ", author='" + author + '\'' +
                '}';
    }
}

注册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="blog" class="com.web.pojo.Blog">
        <property name="author" value="清华出版社"/>
        <property name="title" value="深入理解JVM"/>
        <property name="views" value="6571"/>
    </bean>
</beans>

测试

package com.web.test;

import com.web.pojo.Blog;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.testng.annotations.Test;

public class BlogTest {
    @Test
    public void blogTest(){
        ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
        Blog blog = context.getBean(Blog.class);
        System.out.println(blog);
    }
}
调用了Blog无参构造
Blog{title='深入理解JVM', views=6571, author='清华出版社'}

===============================================
Default Suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================

结论:Bean实例化时默认调用无参构造器
———————————————————————————————————————————
问题:如果没有空参构造器怎么办?

创建pojo类

package com.web.pojo;

public class Phone {
    private String phoneName;
    private String color;
    private String price;

    public Phone(String phoneName, String color, String price) {
        this.phoneName = phoneName;
        this.color = color;
        this.price = price;
    }

    public void setPhoneName(String phoneName) {
        this.phoneName = phoneName;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public void setPrice(String price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Phone{" +
                "phoneName='" + phoneName + '\'' +
                ", color='" + color + '\'' +
                ", price='" + price + '\'' +
                '}';
    }
}

Spring为我们提供了3种构造器注入方式,这样可以解决没有空参构造器的问题

  1. name(构造器参数名) value(属性值)
<bean id="phone" class="com.web.pojo.Phone">
     <constructor-arg name="phoneName" value="小米12"/>
     <constructor-arg name="color" value="白色"/>
     <constructor-arg name="price" value="5699"/>
</bean>
  1. type(构造器参数类型) value(属性值)
<bean id="phone" class="com.web.pojo.Phone">
      <constructor-arg type="java.lang.String" value="iPhone13"/>
      <constructor-arg type="java.lang.String" value="黑色"/>
      <constructor-arg type="java.lang.String" value="5999"/>
</bean>
  1. index(构造器参数的索引) value(属性值)
<bean id="phone" class="com.web.pojo.Phone">
      <constructor-arg index="0" value="华为mate40"/>
      <constructor-arg index="1" value="蓝色"/>
      <constructor-arg index="2" value="6499"/>
</bean>

运行结果

Phone{phoneName='小米12', color='白色', price='5699'}

===============================================
Default Suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================
Phone{phoneName='iPhone13', color='黑色', price='5999'}

===============================================
Default Suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================
Phone{phoneName='华为mate40', color='蓝色', price='6499'}

===============================================
Default Suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================
举报

相关推荐

0 条评论