使用Spring进行依赖注入

来源:互联网 发布:淘宝手机店铺模版 编辑:程序博客网 时间:2024/04/30 15:02

使用Spring进行依赖注入

1. Spring IoC容器

职责:对应用程序的对象进行实例化、初始化、装配以及在对象的整个生命周期中提供其他Spring功能。

配置元数据

Spring容器首先获取应用程序中编写的类并与配置元数据结合在一起,然后创建和装配Bean。

配置元数据的格式:基于xml、基于注解和基于Java
(Spring容器独立于配置元数据格式)

  • 基于Java

使用一个类来说明bean,并使用@Configuration和@Bean两个注解。@Configuration告诉Spring,该类是一个Bean并且包含配置元数据。@Bean方法名就是Bean的名称。

@Configurationpublic class Ch2BeanConfiguration {    @Bean    public AccountService accountService(){        AccountServiceImpl bean=new AccountServiceImpl();        ...        return bean;    }}
  • 基于xml

在maven中,xml文件的位置是基于resources目录的

<bean class="beginningspring.AccountServiceImpl" id="accountService>    <property name="accountDao" ref="accountDao"/></bean><bean class="beginningspring.AccountDaoInMemoryImpl" id="accountDao></bean>
  • 基于注解

使用@Autowired、@Service和@Repository,@Service将一个类定义为一个Bean,@Repository能够启用与Spring数据访问相关联的其他功能。默认bean的名称派生自简单的类名,但首字母小写。@Autowired是自动装配。的作用是扫描路径中的类,通过关联的注解创建Bean并注入其依赖项。Spring通过扫描路径中存在的类,尝试在启动期间识别带有@Component的类及其派生类。

@Servicepublic class AccountServiceImpl implements AccountService {    //数据库接口    private AccountDao accountDao;    ...    @Autowired    public void setAccountDao(AccountDao accountDao) {        this.accountDao = accountDao;    }}@Repositorypublic class AccountDaoInMemoryImpl implements AccountDao {}<context:component-scan base-package="beginningspring"/>

2. 依赖注入

两种类型的依赖注入方法
- Setter注入
Setter注入式在Bean实例创建完毕之后执行
<property name="" ref="/>
- 构造函数注入
构造函数注入在组件创建期间被执行
<constructor-arg ref=""/>

依赖解析过程

  1. 容器处理配置元数据并建立元数据中存在的Bean的定义,在该过程中还会对这些Bean定义进行验证。
  2. 首先完成Bean的创建,然后完成依赖注入。在容器启动期间,仅创建无状态作用域的Bean。一个Bean在被完全创建且自己的依赖项被注入之前是不会作为一个依赖项被注入到其他Bean中去的。

重写Bean的定义

  • 重写形式发生在Bean配置元数据文件级别。根据给定顺序,第二个Bean定义重写第一个Bean定义。
  • Bean重写发生在容器级别。Spring提供子ApplicationContext中定义的Bean

depends-on特性

  • 指定明确的创建顺序

自动装配

让Spring容器自动地向Bean中注入依赖项。自动装配仅适用于对其他Bean的依赖,而不适用纯粹值。自动装配有三种模式:byType、byName和constructor

  • byType

Spring首先通过Java反射查看Bean定义中的类,以便研究定义中的属性,然后尝试将容器中可用的Bean注入与其类型相匹配的属性。该注入操作通过调用这些属性的Setter方法来完成。

  • byName

容器将属性名与Bean名称进行匹配,并注入匹配的Bean。默认值为null。

  • constructor

类似于byType模式,但是在使用constructor模式时,Spring容器尝试找到那些类型与构造函数参数想匹配的Bean。

3. Spring管理的Bean

被Spring创建和管理的Java对象被称为Bean。Bean通过名称进行区分,每个Bean至少有一个名称。@Bean(name={“”})

创建Bean的方法

  • 调用相关类中一个可用的构造函数
  • 调用可用的静态或工厂方法
  • 使用Spring自带的FactoryBean接口

Bean的作用域

由Spring容器创建的Bean的生存期被称为Bean作用域。默认是Singleton.@Scope(“”)

作用域名称 作用域定义 singleton 仅创建一个实例 prototype 每次创建一个新的实例 request 每一web请求都会创建一个新实例 session 不同的http会话创建新实例 globalSession 仅适用portlet

延迟初始化

  • xml lazy-init=”true”
  • 注解 @Lazy(true)

生命周期回调

调用顺序 注解>接口>xml
- xml init-method destroy-method
- 注解 @PostConstruct @PreDestroy
- 接口 InitializingBean DispoableBean

定义配置文件


  1. <import resource="classpath:/dataSource-${targetPlatform}.xml"/>

    ${targetPlatform}既可以从操作系统的环境变量中解析,也可以从JVM的系统属性中解析。

2.

<beans profile="dev">    <bean>    ...    </bean></beans>

使用@Profile注解

环境

管理应用程序所使用的配置文件和属性信息

ConfigurableEnvironment environment = applicationContext.getEnvironment();environment.setActiveProfiles("dev");

为了激活占位符解析机制

@Beanpublic static PropertySourcesPlaceholderConfigurer propertyPlaceHolderConfigurer() {    return new PropertySourcesPlaceholderConfigurer();}
0 0
原创粉丝点击