Spring IOC的常见使用(一)

近段时间对spring源码加深了一下了解,真的要彻底搞懂spring的框架,从IOC到AOP,再到MVC和Mybatis,怕是还有很长的路要走。今年以来,自己跟着一个大佬的视频学习Spring,的确很有效果,看源码是需要技巧的,同时,希望以后的工作中能够吸取源码设计的精髓,在实际开发中举一反三,融会贯通。这是学习spring的第一篇博客,那么先从初识Spring IOC开始吧!

Spring IOC的常见使用

装配bean的三种方式
  1. 显示装配的方式
    1. 通过xml的方式装配bean
    2. 通过java代码装配bean
  2. 隐式装配的方式
    1. 自动化装配bean
通过XML方式装配bean
  1. 创建一个简单的bean
    <bean id="petStore" class="com.focus.spring.service.v1.impl.PetStoreServiceImpl"/>
  1. 借助构造器注入初始化bean
	<bean id="petStore" class="com.focus.spring.service.v3.impl.PetStoreServiceImpl">
        <constructor-arg ref="accountDao"/>
        <constructor-arg ref="itemDao"/>
        <constructor-arg value="1"/>
    </bean>

    <bean id="accountDao" class="com.focus.spring.dao.v2.AccountDao"/>
    <bean id="itemDao" class="com.focus.spring.dao.v2.ItemDao"/>
  1. 借助property元素设置属性值
   <bean id="petStore" class="com.focus.spring.service.v2.impl.PetStoreServiceImpl">
        <property name="accountDao" ref="accountDao"/>
        <property name="itemDao" ref="itemDao"/>
        <property name="owner" value="alongso"/>
        <property name="version" value="2"/>
        <property name="flag" value="yes"/>
    </bean>

    <bean id="accountDao" class="com.focus.spring.dao.v2.AccountDao"/>
    <bean id="itemDao" class="com.focus.spring.dao.v2.ItemDao"/>
通过java代码装配bean
  1. Configuration注解
    表明这个类是一个配置类,该类应该包含在spring应用上下文中如何创建bean的细节。

  2. 代码示例1:构造函数通过调用get方法注入属性值

@Configuration
public class JavaConfig {

    @Bean
    public ItemDao getItemDao() {
        return new ItemDao();
    }

    @Bean
    public AccountDao getAccountDao() {
        return new AccountDao();
    }

    @Bean(name="petStore")
    public PetStoreServiceImpl getPetStoreServiceImpl() {
        return new PetStoreServiceImpl(getAccountDao(), getItemDao());
    }
}

  1. 代码示例2:构造函数通过参数值进行赋值
    @Bean(name="petStore")
    public PetStoreServiceImpl getPetStoreServiceImpl(AccountDao accountDao,ItemDao itemDao) {
        return new PetStoreServiceImpl(accountDao, itemDao);
    }
  1. 代码示例3:通过先new一个实例,再set属性值赋值
    @Bean(name = "petStore")
    public PetStoreServiceImpl getPetStoreServiceImpl(AccountDao accountDao, ItemDao itemDao) {
        PetStoreServiceImpl petStoreService = new PetStoreServiceImpl(accountDao, itemDao);
        petStoreService.setAccountDao(accountDao);
        petStoreService.setItemDao(itemDao);
        return petStoreService;
    }
自动化装配bean
  1. 包含的两个方面
    1. 组件扫描,Component注解
      1. Spring会自动发现应用上下文中所创建的bean
    2. 自动装配,Autowired注解
      1. Spring会自动满足bean之间的依赖
    3. 注意⚠️
      1. 采用注解扫描注入的方式时,必须指明扫描包的路径,代码如下所示
@Component
public class AccountDao {
}
@Component(value = "petStore")
public class PetStoreServiceImpl implements IPetStoreService {

    @Autowired
    private AccountDao accountDao;

    @Autowired
    private ItemDao itemDao;
}
/**
 * 采用注解的方式对包进行扫描,配置扫描包的信息在配置类上,
 * 配置类必须有@Configuration@ComponentScan标识
 * 在我们实际开发过程中,我们有使用到@RunWith和@ContextConfiguration这两个注解
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = ComponentConfig.class)
public class ApplicationContextV4JunitTest {

    @Autowired
    PetStoreServiceImpl petStore;

    @Test
    public void testGetBean() {
        //验证是否实例化成功
        Assert.assertNotNull(petStore.getAccountDao());
        Assert.assertNotNull(petStore.getItemDao());
    }
}
package com.focus.spring.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan(basePackages = {"com.focus.spring.dao.v4","com.focus.spring.service.v4.impl"})
public class ComponentConfig {
}
  1. 通过XML启动组件扫描(在文件头加入context)
    <context:component-scan base-package="com.focus.spring.dao.v4,com.focus.spring.service.v4.impl"/>

这是我结合《Spring实战》一起学习整理的。接下来,我想通过以上几个功能点,对代码采取静态阅读加上动态调试的方式,学习spring的IOC详细框架。未完待续。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值