spring对象属性及对象关联

Spring 设计的核心是 org.springframework.beans 包,它的设计目标是与 JavaBean 组件一起使用。这个包通常不是由用户直接使用,而是由服务器将其用作其他多数功能的底层中介。下一个最高级抽象是 BeanFactory 接口,它是工厂设计模式的实现,允许通过名称创建和检索对象。BeanFactory 也可以管理对象之间的关系。

一、对象创建
1.1、创建一个user对象

User.java

package com.testfan.spring.ioc;

import java.util.List;
import java.util.Map;
import java.util.Set;

public class User {

    private String name;
    private int age;

    private Car car;

    private List<Car> carList;

    private Set<Car> setlist;

    private Map<String, Object> map;


    public Map<String, Object> getMap() {
        return map;
    }

    public void setMap(Map<String, Object> map) {
        this.map = map;
    }

    public Set<Car> getSetlist() {
        return setlist;
    }

    public void setSetlist(Set<Car> setlist) {
        this.setlist = setlist;
    }

    public List<Car> getCarList() {
        return carList;
    }

    public void setCarList(List<Car> carList) {
        this.carList = carList;
    }

    public User() {
        System.out.println(" create ");
    }

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

    public Car getCar() {
        return car;
    }
    public void setCar(Car car) {
        this.car = car;
    }
    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;
    }
    @Override
    public String toString() {
        return "User [name=" + name + ", age=" + age + "]";
    }


    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + age;
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        User other = (User) obj;
        if (age != other.age)
            return false;
        if (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        return true;
    }
    public static void main(String[] args) {
        try {
            Class clz= Class.forName("com.testfan.spring.ioc.User");
            User  object =(User) clz.newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
1.2 BeanFactory 支持两个对象模型。
1.2.2 单态

模型提供了具有特定名称的对象的共享实例,可以在查询时对其进行检索。Singleton 是默认的也是最常用的对象模型。对于无状态服务对象很理想。

xml文件(ioc.xml)

<?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="user" class="com.testfan.spring.ioc.User">
     </bean>
</beans>

IocTest.java

package com.testfan.spring.ioc;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class IocTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("ioc.xml");
        //byname/id
        User user = (User) context.getBean("user");
        System.out.println(user);
        //bytype
        User user2 = context.getBean(User.class);
        System.out.println(user2);

        System.out.println(user==user2);
    }
}

输出如下:user=user2
这里写图片描述

1.2.3 原型

模型确保每次检索都会创建单独的对象。在每个用户都需要自己的对象时,原型模型最适合。

xml文件(ioc.xml)

<?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="user" class="com.testfan.spring.ioc.User"  scope="prototype">
          <property name="name" value="zhangsan"></property>
          <property name="age" value="20"></property>
     </bean>
</beans>

2.2 对象关联(set、list、map的管理)

ref:引用其他bean对象
property:调用set方法
constructor-arg:调用构造方法

Car.java(创建一个car对象)

package com.testfan.spring.ioc;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component("cartest")
public class Car {
    private String name;
    private double price;

    @Resource(name="user")
    private User user2;


    public User getUser2() {
        return user2;
    }

    public void setUser2(User user2) {
        this.user2 = user2;
    }

    public Car(String name, double price) {
        super();
        this.name = name;
        this.price = price;
        System.out.println(" 我有参数 ");
    }

    public Car() {

    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public double getPrice() {
        return price;
    }
    public void setPrice(double price) {
        this.price = price;
    }
    @Override
    public String toString() {
        return "Car [name=" + name + ", price=" + price + "]";
    }
}

ioc.xml

<?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="user" class="com.testfan.spring.ioc.User"  scope="prototype">
          <!-- 配置注入属性 -->
          <property name="name" value="zhangsan"></property>
          <property name="age" value="20"></property>
          <property name="car" ref="cartest"></property>
     </bean>

    <bean class="com.testfan.spring.ioc.Car">
        <!-- 配置注入属性 -->
        <constructor-arg index="0" value="奥拓"></constructor-arg>
        <constructor-arg index="1" value="1000"></constructor-arg>
    </bean>
</beans>

IocTest.java

//对象关联
System.out.println(user.getCar());

2.2复杂对像管理
List

<list>
    <ref bean="bean1"/>
    <bean></bean>
</list>

例:

<property name="carList">
    <list>
        <ref bean="cartest" />
        <ref bean="cartest" />  //User拥有多辆车,引用其他bean对象
        <bean class="com.testfan.spring.ioc.Car">  //创建一个新的bean
            <constructor-arg index="0" value="奥拓"></constructor-arg>
            <constructor-arg index="1" value="1000"></constructor-arg>
        </bean>
    </list>
</property>

set:
set和list最大的区别,set会去重

<set>
    <ref bean:bean1/>
    <bean></bean>
</set>

例:

<property name="setlist">
    <set>
        <ref bean="cartest" />
        <ref bean="cartest" />
        <bean class="com.testfan.spring.ioc.Car">
             <constructor-arg index="0" value="奥拓"></constructor-arg>
             <constructor-arg index="1" value="1000"></constructor-arg>
        </bean>
    </set>
</property>

Map:

<map>
  <entry   key:key1  value:value1></entry>
  <entry   key:key2>
       <bean></bean>
  </entry>
</map>

例:

<property name="map">
    <map>
        <entry key="zhangsan" value="111"></entry>  //普通的字符串
        <entry key="car" value-ref="cartest"></entry>  //引用bean对象
        <entry key="car2">   //自定义的bean对象
            <bean class="com.testfan.spring.ioc.Car">
                <constructor-arg index="0" value="奥拓"></constructor-arg>
                <constructor-arg index="1" value="1000"></constructor-arg>
            </bean>
        </entry>
    </map>

代码地址:https://github.com/HopeWing1286/SpringMvc.git

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值