Spring注入方式 理解

spring注入方式主要有三种,构造器注入,set方法注入,接口注入

举个例子
定义 InjectionDaoImply类

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

public class InjectionDaoImply extends InjectionDao{
	String idString;
	
	public InjectionDaoImply( String idString){
		this.idString = idString;
	}
	
	@Override
	public  void save(){
		System.out.println("我是Daoimply");
	};
	
	public	void say(){		
		System.out.println("我是Daoimply say:"+ idString);
	}
}

传统方法:

package wangxf.com.spring.service;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import wangxf.com.dao.InjectionDaoImply;


public class test {
     public static void main(String[] args) {
    	 InjectionDaoImply injectionDaoImply =  new InjectionDaoImply("123");  	 
    	 injectionDaoImply.say();

     }
}
//我是Daoimply say:123

构造函数注入

我们使用spring容器注入 InjectionDaoImply构造函数
定义 InjectionDaoImply类

package wangxf.com.dao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;


public class InjectionDaoImply extends InjectionDao{
	String idString;

	 InjectionDaoImply(String idString){
		this.idString = idString;
	}

	@Override
	public  void save(){
		System.out.println("我是Daoimply");
	};
	
	public	void say(){
		
		System.out.println("我是Daoimply say:"+ idString);
	}
}

定义spring-mvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd">

    <bean class="wangxf.com.dao.InjectionDaoImply"  id = "injectionDaoImply">
    	 <constructor-arg  name="idString" value="test"> 	</constructor-arg>  
    </bean>  
   
</beans>

test类

package wangxf.com.spring.service;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import wangxf.com.dao.InjectionDaoImply;


public class test {
     public static void main(String[] args) {
    	 ApplicationContext ac = new ClassPathXmlApplicationContext("/spring-mvc.xml");
   	 
    	 InjectionDaoImply injectionDaoImply =   (InjectionDaoImply) ac.getBean("injectionDaoImply");
    	 injectionDaoImply.say();
     }
}

我是Daoimply say:123

现在我们使用自动装配,减少配置
@Autowired

package wangxf.com.dao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class InjectionDaoImply extends InjectionDao{
	String idString;
	
	@Autowired 
//	InjectionDaoImply(@Value("${file.value}") String idString){
//		this.idString = idString;
//	}
	 InjectionDaoImply(@Value("#{'value'}")  String idString){
		this.idString = idString;
	}
	public	void say(){		
		System.out.println("我是Daoimply say:"+ idString);
	}
}

xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd">

  <context:annotation-config/>
  <context:property-placeholder location="classpath:file.properties"/>
    <bean class="wangxf.com.dao.InjectionDaoImply"  id = "injectionDaoImply">
   
    </bean>  
  
</beans>

测试类与之前相同,减少了<constructor-arg >属性的配置,在多个构造函数情况下可以减少许多配置工作。
值得注意的是@value的用法
@value
#{} 直接赋值或者去bean中的值
${} 读取文件值

属性注入

建立InjectionServiceImply 类

package wangxf.com.spring.service;

import wangxf.com.dao.InjectionDaoImply;

public class InjectionServiceImply extends InjectionService {
	
	private InjectionDaoImply  injectiondaoimply;
	 
	public void setInjectiondaoimply(InjectionDaoImply injectiondaoimply) {
		this.injectiondaoimply = injectiondaoimply;
	}

	@Override
	public void save() {
		// TODO Auto-generated method stub
		System.out.println("我是ServiceImply");
		injectiondaoimply.save();
	}

}

xml 添加的bean

    <bean class="wangxf.com.spring.service.InjectionServiceImply"  id = "injectionServiceImply">
                <property name="injectiondaoimply" ref ="injectionDaoImply"/> 
    </bean>

需要注意的是,InjectionServiceImply中的injectiondaoimply属性必须使用set方法,否则注入无法成功。

使用@Autowired 自动装配

package wangxf.com.spring.service;

import org.springframework.beans.factory.annotation.Autowired;

import wangxf.com.dao.InjectionDaoImply;

public class InjectionServiceImply extends InjectionService {
	@Autowired
	private InjectionDaoImply  injectiondaoimply;
	@Override
	public void save() {
		// TODO Auto-generated method stub
		System.out.println("我是ServiceImply");
		injectiondaoimply.save();
	}

}

xml

    <bean class="wangxf.com.spring.service.InjectionServiceImply"  id = "injectionServiceImply">      
    </bean>

Test类

package wangxf.com.spring.service;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import wangxf.com.dao.InjectionDaoImply;


public class test {
     public static void main(String[] args) {

    	 
    	 ApplicationContext ac = new ClassPathXmlApplicationContext("/spring-mvc.xml");
    	 InjectionServiceImply injectionServiceImply =   (InjectionServiceImply) ac.getBean("injectionServiceImply");
    	 injectionServiceImply.save();    	 
     }
}
//我是ServiceImply
//我是Daoimply

接口注入:

接口注入实际就是向上转型的注入,修改InjectionServiceImply
注意我们使用的是InjectionDao 不是其实现类

package wangxf.com.spring.service;

import org.springframework.beans.factory.annotation.Autowired;

import wangxf.com.dao.InjectionDao;
import wangxf.com.dao.InjectionDaoImply;

public class InjectionServiceImply extends InjectionService {
	@Autowired
	private InjectionDao  injectiondao;
	 
	@Override
	public void save() {
		// TODO Auto-generated method stub
		System.out.println("我是ServiceImply");
		injectiondao.save();
	}

}

修改xml

 <bean class="wangxf.com.dao.InjectionDaoImply"  id = "injectionDao">
    </bean>  

运行Test结果与Set注入一致。

我们来对比这几种注入方式
点这里
首先接口注入的方式与Set注入方式相似,但是其必须要有写一个实现类继承,即组件与接口必须关联,有较强的侵略性。
Set注入需要些较多的Set方法,但是使用@Autowired 注解后就十分清晰,Set注入方式逻辑赋值清晰,常用
构造器注入,代码和配置都更加简洁,依赖关系在构造时由容器一次性设定。

bean的作用范围

scope属性定义bean的活动范围

有singleton,prototype,request,session,global
singleton ,Spring IOC容器中只会存在一个共享的bean实例。
prototype,每一次请求(将其注入到另一个bean中,或者以程序的方式调用容器的 getBean()方法)都会产生一个新的bean实例,相当与一个new的操作
request 每次http请求会创建bean,请求结束后会释放
session 每次会话创建bean
gglobal session与session相似

在这里插入图片描述

bean的生命周期

点这里

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值