Spring依赖注入

<span style="font-size:18px;">xmlns:context="http://www.springframework.org/schema/context"</span>

1.依赖注入的理解:控制反转又名依赖注入,原来当生产一个类的对象的时候需要我们主动来创建(NEW),当引入Spring后当我们需要一个类时不再需要我们来主动创建(new)而是由Spring为我们创建;这样我们对对象创建的控制关系就转为Spring对对象创建的控制,因此称作控制反转


2.Spring的几种注入方式

1>使用属性的setter方法注入

2>使用构造器注入

3>使用注解注入


一、使用属性的setter方法注入

接口

<span style="font-size:18px;">public interface PersonService {
	public void  eat();
}
</span>
实现类
<span style="font-size:18px;">public class PersonServiceImpl  implements PersonService{
	private String food;
	
   @Override
   public void eat(){
	   System.out.println(food);
   }

public String getFood() {
	return food;
}

public void setFood(String food) {
	this.food = food;
}</span>
XML配置
<span style="font-size:18px;"><?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns="http://www.springframework.org/schema/beans"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
  <bean id="person" class="org.mary.bean.PersonServiceImpl">
  <property name="food" value="iceCream"></property>
  </bean>
</beans></span>
测试
<span style="font-size:18px;">public class Test {
  public  static void main(String []args){
	  ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
	  PersonService ps=ctx.getBean("person", PersonServiceImpl.class);
	  ps.eat();
  }
}
</span>
二.构造器注入
<span style="font-size:18px;">public interface BaseDao {
   public void save();
}
</span>

<span style="font-size:18px;">public class BaseDaoImpl implements BaseDao {
  
	@Override
	public void save(){
		System.out.println("save");
	}
}
</span>

<span style="font-size:18px;">public interface BaseService {
   
	public void save();
}</span>

<span style="font-size:18px;">public class BaseServiceImpl implements BaseService {
     private String name;
     private BaseDao baseDao;
     
     
     public BaseServiceImpl(BaseDao baseDao,String name){
    	 this.baseDao=baseDao;
    	 this.name=name;
     }

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public BaseDao getBaseDao() {
		return baseDao;
	}

	public void setBaseDao(BaseDao baseDao) {
		this.baseDao = baseDao;
	}
     
	@Override
    public void save(){
   	  baseDao.save();
   	  System.out.println(name);
    } 
	
}
</span>

XML配置
<span style="font-size:18px;"><?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns="http://www.springframework.org/schema/beans"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
  <bean id="baseDao" class="org.mary.bean.BaseDaoImpl"/>
  <bean id="baseSerivce" class="org.mary.bean.BaseServiceImpl">
    <constructor-arg index="0"  ref="baseDao"/>
    <constructor-arg index="1" value="猪"/>
  </bean>
</beans></span>

Test
<span style="font-size:18px;">public class Test {
		public static void main(String []args){
			ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
			BaseService bs=ctx.getBean("baseSerivce", BaseServiceImpl.class);
			bs.save();
		}
}
</span>

三.注解方式

@Resource的作用相当于@Autowired,只不过@Autowired按byType自动注入,而@Resource默认按 byName自动注入罢了。@Resource有两个属性是比较重要的,分是name和type,Spring将@Resource注解的name属性解析为bean的名字,而type属性则解析为bean的类型。所以如果使用name属性,则使用byName的自动注入策略,而使用type属性时则使用byType自动注入策略。如果既不指定name也不指定type属性,这时将通过反射机制使用byName自动注入策略。
  @Resource装配顺序
  1. 如果同时指定了name和type,则从Spring上下文中找到唯一匹配的bean进行装配,找不到则抛出异常
  2. 如果指定了name,则从上下文中查找名称(id)匹配的bean进行装配,找不到则抛出异常
  3. 如果指定了type,则从上下文中找到类型匹配的唯一bean进行装配,找不到或者找到多个,都会抛出异常
  4. 如果既没有指定name,又没有指定type,则自动按照byName方式进行装配;如果没有匹配,则回退为一个原始类型进行匹配,如果匹配则自动装配; 
这里仅仅使用@Resource进行举例:

代码和上面的相同,仅仅是PersonServiceBean.java变了:

<span style="font-size:18px;">public interface BaseDao {
	public void save();
}
</span>

<span style="font-size:18px;">public class BaseDaoImpl implements BaseDao{
	
	@Override
	public void save(){
		System.out.println("save");
	}
}</span>

<span style="font-size:18px;">public interface BaseService {
		
	public void save();
}
</span>

<span style="font-size:18px;">public class BaseServiceImpl implements BaseService{
	
	@Resource(name="baseDao")private BaseDao baseDao;
	public void BaseServiceImpl(BaseDao baseDao){
		this.baseDao=baseDao;
	}
	@Override
	public void save(){
		baseDao.save();
	}
}</span>

Xml配置
<span style="font-size:18px;"><?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns="http://www.springframework.org/schema/beans"
      xmlns:context="http://www.springframework.org/schema/context"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
      http://www.springframework.org/schema/context      
      http://www.springframework.org/schema/context/spring-context-3.0.xsd">
   <context:annotation-config/>
   <bean id="baseDao"  class="org.mary.bean.BaseDaoImpl"/>
   <bean id="baseService" class="org.mary.bean.BaseServiceImpl"/>
</beans></span>
XML中增加的内容如下
xmlns:context="http://www.springframework.org/schema/context"
http://www.springframework.org/schema/context      
      http://www.springframework.org/schema/context/spring-context-3.0.xsd
<context:annotation-config/>开启注解扫描




Test类
<span style="font-size:18px;">public class Test {
	public static void main(String []args){
		
		ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
		BaseService bs=ctx.getBean("baseService", BaseServiceImpl.class);
		bs.save();
	}
}
</span>








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值