由浅入深Spring|第一篇:基本使用

1.理解IOC和DI

  • IOC(控制反转)
    指项目中对象的创建由传统的人为new对象转变为Spring框架创建对象

  • DI(依赖注入)
    指Spring框架维护对象间关系,例A对象依赖B对象,那么在创建对象A时,框架会先创建对象B,从而实现对象的依赖注入

    public class A{
    
    	@resource
    	private B b;
    }
    

2.Spring编程风格

  • xml方式(对象A依赖B)

    (1) 构造方法注入

    public class A{
    	private B b;
    	
    	public A(B b){
    		this.b = b
    	}
    }
    
    <!--对象A-->
    <bean id="A" class = "com.study.spring.A"> 
    	<constructor-arg name = "b" ref = "B"/>
    </bean>
    
    <!--对象B-->
    <bean id ="B" class = "com.study.spring.B">
    </bean> 
    

    注意:
    1.在配置<constructor-argname = "b" ref="B">,其中name="b"为构造函数中传入的参数名
    2.验证:将构造函数名改为b1,则配置<constructor-argname="b1" ref="B">

    (2) set方法注入

    public class A{
    	private B b;
    }
    public void setB(B b){
    	this.b = b;
    }
    
    <!--对象A-->
    <bean id="A" class = "com.study.spring.A"> 
    	<property name = "b" ref = "B"/>
    </bean>
    
    <!--对象B-->
    <bean id ="B" class = "com.study.spring.B">
    </bean> 
    

    注意:
    1.在配置<property name = "b" ref="B">,其中name="b"为set方法名setB中去除set部份
    2.验证:将setB改为setB1,则配置<property name="b1" ref="B">

  • springConfig方式
    使用SpringConfig配置需配合@componentScan注解扫描对象,同时交由Spring管理的对象应该配置注解@component@Serivce@Repository等注解以保证被管理对象能被扫描

    (1)配置

    @Configuration
    @ComponentScan("com.study.spring.demo02")
    public class SpringConfig(){
    }
    

    (2)对象A

    @Service
    public class A(){
    	@Resource
    	private B b;
    }
    

3.自动装配

  • byName

    (1) 配置自动装配类型

    default-autowire="byName"
    

    (2) 配置自动扫描

    <context:component-scan base-package="com.study.spring.demo02"/>
    

    (3) 配置对象

    public class A{
    	private B b;
    }
    
    <!--对象A-->
    <bean id="a" class = "com.study.spring.A"> 
    </bean>
    
    <!--对象B-->
    <bean id ="b" class = "com.study.spring.B">
    </bean> 
    

    注意:
    1.使用xml配置自动装配时,需手动配置对像A中依赖的对象B是使用构造函数set方法进行注入
    2.byName注入指配置文件xml中配置的对象名,在此例中对象B名为b,则set方法名必须为setB或构造方法中参数名必须为b

  • byType
    根据对象进行自动装配,在配置时不能有多个相同对象,否则无法注入成功

4.@Autowired与@Resource区别

  • @Autowired默认通过byType方式进行对象注入,如果通过byType找不到,则通过byName进行注入

  • @Resource通过byName方式进行对象注入

5.bean生命周期存在的问题

        bean对象的生命周期使用最多的有SingletonPrototype两个,当Singleton中注入作用域为Prototype类型对象时,Prototype作用失效,由于Singleton只会初始化一次,当Singleton对象中注入prototype对象时同样只会被初化一次。

解决办法:

(1)方式1(ApplicationContextAware)

@Component
@Scope("singleton")
public class A implements ApplicationContextAware {

  @Autowired
  private B b;

  private ApplicationContext applicationContext;

  public void userTest() {
    System.out.println("b:"+this.applicationContext.getBean("b",B.class).hashCode());
    System.out.println("a:"+this.getClass().hashCode())
  }

  public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    this.applicationContext = applicationContext;
  }
}

(2)方式2(lookup)

@Service
@Scope("singleton")
public abstract class A{

  public void aTest() {
    System.out.println("b:"+setB().hashCode());
    System.out.println("a:"+this.getClass().hashCode());
  }
  
  @lookup
  protected abstract B setB();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值