近段时间对spring源码加深了一下了解,真的要彻底搞懂spring的框架,从IOC到AOP,再到MVC和Mybatis,怕是还有很长的路要走。今年以来,自己跟着一个大佬的视频学习Spring,的确很有效果,看源码是需要技巧的,同时,希望以后的工作中能够吸取源码设计的精髓,在实际开发中举一反三,融会贯通。这是学习spring的第一篇博客,那么先从初识Spring IOC开始吧!
Spring IOC的常见使用
装配bean的三种方式
显示装配的方式
通过xml的方式装配bean通过java代码装配bean 隐式装配的方式
自动化装配bean
通过XML方式装配bean
创建一个简单的bean
<bean id="petStore" class="com.focus.spring.service.v1.impl.PetStoreServiceImpl"/>
借助构造器注入初始化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"/>
借助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
Configuration注解: 表明这个类是一个配置类,该类应该包含在spring应用上下文中如何创建bean的细节。
代码示例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());
}
}
代码示例2:构造函数通过参数值进行赋值
@Bean(name="petStore")
public PetStoreServiceImpl getPetStoreServiceImpl(AccountDao accountDao,ItemDao itemDao) {
return new PetStoreServiceImpl(accountDao, itemDao);
}
代码示例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
包含的两个方面
组件扫描,Component注解:
Spring会自动发现应用上下文中所创建的bean 自动装配,Autowired注解:
Spring会自动满足bean之间的依赖 注意⚠️
采用注解扫描注入的方式时,必须指明扫描包的路径,代码如下所示
@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 {
}
通过XML启动组件扫描(在文件头加入context)
<context:component-scan base-package="com.focus.spring.dao.v4,com.focus.spring.service.v4.impl"/>
这是我结合《Spring实战》一起学习整理的。接下来,我想通过以上几个功能点,对代码采取静态阅读加上动态调试的方式,学习spring的IOC详细框架。未完待续。。。