美文网首页
spring对象属性及对象关联

spring对象属性及对象关联

作者: Hope_wing | 来源:发表于2018-08-13 23:32 被阅读0次

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 image.png
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">
    </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
原因:prototype 每次创建新的对象

二、对象赋值

property属性注入
2.1 基本属性
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 对象关联

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最大的区别,setlist会去重

<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

相关文章

  • spring对象属性及对象关联

    Spring 设计的核心是 org.springframework.beans 包,它的设计目标是与 JavaBe...

  • 2018-08-26

    使用spring如何给对象注入Class类型的属性 使用spring如何给对象注入Class类型属性 POJO对象...

  • iOS开发_运行时的简单使用

    动态添加属性/*产生关联,让某个对象(name)与当前对象的属性(name)产生关联参数1: id object ...

  • iOS Objective-C 关联对象

    iOS Objective-C 关联对象 1. 关联对象简介 对于关联对象,我们熟悉它的地方就是给分类添加属性。虽...

  • 第 10 条:在既有类中使用关联对象存放自定义数据

    在某个对象中存放相关信息。 管理关联对象 关联对象对应的属性分别为: 管理关联对象的方法有下面三个: 用法举例 展...

  • Runtime #2

    Category关联属性 为一个对象设置关联对象,实现为其添加属性的效果,使用方法 Method Swizzlin...

  • iOS原理篇:关联对象

    前言 Swift中不能再extension中为类添加存储属性,如何利用关联对象变相添加属性呢? 关联对象相关API...

  • 关联对象

    在分类中不能添加属性,但是却可以使用 关联对象的方式,给类添加变量。主要重点是: 关联对象的实现方式。 关联对象散...

  • 关联对象

    在分类中不能添加属性,但是却可以使用 关联对象的方式,给类添加变量。主要重点是: 关联对象的实现方式。 关联对象散...

  • 为什么使用Runtime又可以添加属性?

    使用Runtime技术中的关联对象可以为类别添加属性。其原因是:关联对象都由AssociationsManager...

网友评论

      本文标题:spring对象属性及对象关联

      本文链接:https://www.haomeiwen.com/subject/olhxmftx.html