其他分享
首页 > 其他分享> > spring

spring

作者:互联网

1.spring中bean的细节

1.Spring中bean的细节

1.1 三种创建Bean对象的方式

1.第一种方式:使用默认构造函数创建。

在spring的配置文件中使用bean标签,配以id和class属性之后,且没有其他属性和标签时,采用的就是默认构造函数创建bean对象,此时如果类中没有默认构造函数,则对象无法创建。


<bean id="accountService" class="com.test.service.impl.AccountServiceImpl"></bean>

 

public static void main(String[] args) {
    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
    IAccountService accountService = (IAccountService) applicationContext.getBean("accountService");
    accountService.saveAccount();
}

 

2.第二种方式:

使用普通工厂中的方法创建对象(使用某个类中的方法创建对象,并存入spring容器)

public class InstanceFactory {

    public IAccountService getAccountService() {
        return new AccountServiceImpl();
    }
}

 

<bean id="instanceFactory" class="com.test.factory.InstanceFactory"></bean>
<bean id="accountService" factory-bean="instanceFactory" factory-method="getAccountService"></bean>

 

3.第三种方法:

使用静态工厂中的静态方法创建对象(使用某个类中的静态方法创建对象,并存入spring容器)

public class StaticFactory {

    public static IAccountService getAccountService() {
        return new AccountServiceImpl();
    }
}

 


<bean id="accountService" class="com.test.factory.StaticFactory" factory-method="getAccountService"></bean>

 

1.2 bean的作用范围

Bean标签的scope属性用来指定bean的作用范围,scope取值:singleton,prototype,request,session,global-session

  1. Singleton

单例的(默认值)

  1. Prototype

多例的

  1. Request

作用于web应用的请求范围

  1. Session

作用于web应用的会话范围

  1. global-session

作用于集群环境的会话范围(全局会话范围),当不是集群环境时,就是session

 

1.3 bean对象的生命周期

1.单例对象:

出生:当容器创建时对象出生

活着:只要容器还在,对象一直活着

死亡:容器销毁,对象消亡

总结:单例对象的生命周期和容器相同

2.多例对象

出生:当我们使用对象时spring框架为我们创建

活着:对象只要在使用过程中就一直存在

死亡:当对象长时间不用且没有别的对象引用时,由java的垃圾回收器回收

2.spring的依赖注入

依赖注入:Dependncy Injection

作用:降低程序间的耦合(依赖关系)

能注入的数据有三类:

  1. 基本类型和String
  2. 其他bean类型(在配置文件中或者注解配置过的bean)
  3. 复杂类型/集合类型

注入的方式有三种:

  1. 使用构造函数提供
  2. 使用set方法提供
  3. 使用注解提供

2.1 构造函数注入

使用的标签:<constructor-arg>

标签出现的位置:bean标签内部

标签中的属性:

type:

index:用于指定要注入的数据给构造函数中指定索引位置的参数赋值,索引的位置是从0开始的

name:用于指定给构造函数中指定名称的参数赋值

value:用于提供基本数据类型和String类型

ref:用于指定其他的bean类型数据,指的就是在spring的核心容器中出现过的bean对象

public class AccountServiceImpl implements IAccountService {

    private String name;
    private Integer age;
    private Date birthday;

    public AccountServiceImpl(String name, int age, Date birthday) {
        this.age = age;
        this.birthday = birthday;
        this.name = name;
    }

    public void saveAccount() {
        System.out.println("保存账户成功"+ "name:" + name + "-----age:" + age + "-----birghday:" + birthday);
    }

}

 

<bean id="accountService" class="com.test.service.impl.AccountServiceImpl" >
    <constructor-arg index="0" name="name" value="test"/>
    <constructor-arg index="1" name="age" value="18"/>
    <constructor-arg index="2" name="birthday" ref="now"/>
</bean>

<bean id="now" class="java.util.Date"></bean>

优势:在获取bean对象时,注入数据是必须的操作,否则对象无法创建成功。

弊端:改变了bean对象的实例化方式,是我们在创建对象时,如果用不到这些数据,也必须提供。

2.2 set方法注入(常用)

涉及的标签:property

出现的位置:bean标签的内部

标签的属性:

name:用于指定注入时所调用的set方法名称

value:用于提供基本类型和String类型的数据

ref:用于指定其他的bean类型数据。他指的就是spring的Ioc核心容器中出现过的bean对象

2.2.1 简单类型的注入  

public class AccountServiceImpl implements IAccountService {

    private String name;
    private Integer age;
    private Date birthday;

    public void setName(String name) {
        this.name = name;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public void saveAccount() {
        System.out.println("保存账户成功"+ "name:" + name + "-----age:" + age + "-----birghday:" + birthday);
    }

}

 

<bean id="accountService2" class="com.test.service.impl.AccountServiceImpl">
    <property name="name" value="test"></property>
    <property name="age" value="21"></property>
    <property name="birthday" ref="now"></property>
 </bean>

<bean id="now" class="java.util.Date"></bean>

 

优势:创建对象时没有明确的限制,可以直接使用默认构造函数

弊端:如果有某个成员必须有值,则set方法无法保证一定注入,因为set方法可能没有执行

2.2.2 复杂类型的注入

用于给list结构集合注入的标签:list,array,set

用于给Map结构集合注入的标签:map,props

结构相同,标签可以互换

2.3 注解提供

<bean id="accountService" class="com.test.service.impl.AccountServiceImpl" scope="singleton" init-method="saveAccount" destroy-method="saveAccount">
    <property name="name" value="test"></property>
</bean>

 

注解分类:

2.3.1用于创建对象的注解

作用等同于bean标签

@Component

@Controller

@Service

@Repository

 

2.3.2用于注入数据的注解

作用等同于<property>标签

@Autowired(按照类型注入)

2.3.3用于改变作用范围的注解

作用等同于scope标签

2.3.4和生命周期相关的注解

作用等同于init-method

标签:name,spring,age,bean,birthday,标签,public
来源: https://www.cnblogs.com/ldddd/p/16035908.html