Spring中的@Autowired依赖注入

@Autowired注解 注解能够作用于构建器、属性、方法。这里的方法不局限于设值方法,即setter方法,常见的各种方法都可以应用这一注解

@Autowired(required=false) 可以避免找不到bean时的抛出异常,每一个类只能有一个构造器被声明为required=false

@Autowired 是ByType的

下面举一个用于属性的实例:

第一步:完成xml与DAO类和其接口、Servicer类及其接口

核心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"
    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" >
        <context:component-scan base-package="com.imooc.beanannotation"></context:component-scan>    
 </beans>

我们让Spring扫描 com.imooc.beanannotation

DAO类

package com.imooc.beanannotation;
import org.springframework.stereotype.Repository;
@Repository
public class InjectionDAOimpl implements InjectionDAO {
 public void save(String arg) {
  // TODO Auto-generated method stub
  System.out.println("保存数据:"+arg);
 }
}

这里@Repository是关键,因为spring是通过这个注解认为这个类是DAO类的,@Repository 将 DAO 类声明为 Bean

如果没有这个注解@Repository,那么我们需要自己在Spring的配置文件中配置这个bean

DAO接口

package com.imooc.beanannotation;
import org.springframework.stereotype.Repository;
public interface InjectionDAO {
 public void save(String arg);
}

Service类

package com.imooc.beanannotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class InjectionServiceimpl implements InjectionService {
     @Autowired
 private InjectionDAO injectionDAO;
 public void save(String arg) {
  // TODO Auto-generated method stub
  System.out.println("接受数据"+arg);
  arg=arg+":"+this.hashCode();
  injectionDAO.save(arg);
 }
}

这里@Service注解 和@Autowired 值得注意,@Service将该类标记为服务层,并加入bean,@Autowired为我们注入InjectionDAO接口

Service类接口

package com.imooc.beanannotation;
public interface InjectionService {   
 public void save(String arg);
}

测试类:

package com.imooc.beanannotation;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestInjection {
    public static void main(String[] args) {
  ApplicationContext ctx=new ClassPathXmlApplicationContext("spring-beanannotation.xml");
  InjectionService service=(InjectionService) ctx.getBean("injectionServiceimpl");
  service.save("this is a autowired");
 }
}

可以看见   其实我们只需要getbean(接口(小写即默认名字))就可获得实现类的实例,这个例子证明我们可以用

@Autowired装配类以获取实例

这是@AutoWired用于构造器的例子

只需要修改Service类

package com.imooc.beanannotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class InjectionServiceimpl implements InjectionService {
    // @Autowired
 private InjectionDAO injectionDAO;
 @Autowired
    public InjectionServiceimpl(InjectionDAO injectionDAO)
     {
      this.injectionDAO=injectionDAO;
     }
 public void save(String arg) {
  // TODO Auto-generated method stub
  System.out.println("接受数据"+arg);
  arg=arg+":"+this.hashCode();
  injectionDAO.save(arg);
 }
}

请注意,原来我们在是在属性上用的@Autowired,现在用在构造器上。其实就相当于在构造方法中将InjectionDAO实例化了

注意下面的例子,这是一个很容易犯的错误

package com.imooc.beanannotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class InjectionServiceimpl implements InjectionService {
     @Autowired
 private InjectionDAO injectionDAO;
    public InjectionServiceimpl()
     {
      this.injectionDAO=injectionDAO;
     }
 public void save(String arg) {
  // TODO Auto-generated method stub
  System.out.println("接受数据"+arg);
  arg=arg+":"+this.hashCode();
  injectionDAO.save(arg);
 }
}
这个时候运行是会出错的,这是因为程序会先运行构造方法,再装载InjectionDAO





 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值