【Spring】Spring注解注入

一.Spring组件扫描机制

Spring能在Classpath下自动扫描被某些注解标识的类,并把它们放到IoC容器中,作用和使用XML配置是一样的,但是效率要高很多。
特定的组件有如下几个(不止):
1. Autowired是自动注入,自动从spring的上下文找到合适的bean来注入
2. Resource用来指定名称注入
3. Qualifier和Autowired配合使用,指定bean的名称
4. Service,Controller,Repository分别标记类是Service层类,Controller层类,数据存储层的类,spring扫描注解配置时,会标记这些类要生成bean。
5. Component是一种泛指,标记类是组件,spring扫描注解配置时,会标记这些类要生成bean。
(每种注解详细介绍参考:Spring框架注解学习
上面的Autowired和Resource是用来修饰字段,构造函数,或者设置方法,并做注入的。而Service,Controller,Repository,Component则是用来修饰类,标记这些类要生成bean。

二. 通过实例项目来看下spring注解注入的使用。

首先新建一个maven项目,并在pom中添加spring相关的依赖。
然后新建CarDao类,给它添加@Repository注解,如下代码:

package cn.outofmemory.helloannotation;
import org.springframework.stereotype.Repository;
@Repository
public class CarDao {
    public void insertCar(String car) {
        String insertMsg = String.format("inserting car %s", car);
        System.out.println(insertMsg);
    }
}

新建CarService类,并给该类标注@Service注解,在这个类中定义CarDao的字段,并通过Autowired来修饰此字段,这样上面定义的CarDao类的实例就会自动注入到CarService的实例中了。

package cn.outofmemory.helloannotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class CarService {
    @Autowired
    private CarDao carDao;
    public void addCar(String car) {
        this.carDao.insertCar(car);
    }
}

注意:Autowired注解有一个可以为空的属性required,可以用来指定字段是否是必须的,如果是必需的,则在找不到合适的实例注入时会抛出异常。
下面我们在App.java中使用上面测试下注解注入:

package cn.outofmemory.helloannotation;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class App {
    public static void main( String[] args )
    {
        ApplicationContext appContext = new AnnotationConfigApplicationContext("cn.outofmemory.helloannotation");
        CarService service = appContext.getBean(CarService.class);
        service.addCar("宝马");
    }
}

在上面的main方法中首先我们初始化了appContext,他是AnnotationConfigApplicationContext,它的构造函数接受一个package的名称,来限定要扫描的package。然后就可以通过appContext的getBean方法获得CarService的实例了。
上面的例子非常简单,单纯的使用AnnotationConfigApplicationContext就可以了,但是在实际项目中情况往往没有这么简单,还是需要spring配置文件的。在spring配置文件中也可以通过下面的配置让spring自动扫描注解配置。

 <!-- bean annotation driven -->
    <context:annotation-config />
    <context:component-scan base-package="cn.outofmemory.helloannotation" >
    </context:component-scan>

下面我们看下混合使用spring配置和注解的例子,首先在项目中添加source folder,src/main/resources,并添加spring.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-3.0.xsd
 http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context-3.0.xsd ">

    <!-- bean annotation driven -->
    <context:annotation-config />
    <context:component-scan base-package="cn.outofmemory.helloannotation" >
    </context:component-scan>
    <bean id="sqliteCarDao" class="cn.outofmemory.helloannotation.CarDao" >
        <constructor-arg name="driver" value="sqlite"/>
    </bean> 
 </beans>

在上面的配置文件中,我们通过context:annotation-config和context:component-sacn节点来指定要扫描注解注入,然后又定义了一个id为sqliteCarDao的bean,它的构造函数的driver值为sqlite。
我们修改下App.java使用xml配置文件,再运行下App看下会怎样。

package cn.outofmemory.helloannotation;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App {
    public static void main( String[] args )
    {
        //ApplicationContext appContext = new AnnotationConfigApplicationContext("cn.outofmemory.helloannotation");
        ApplicationContext appContext = new ClassPathXmlApplicationContext("/spring.xml");
        CarService service = appContext.getBean(CarService.class);
        service.addCar("宝马");
    }
}

运行程序发现输出为:inserting car 宝马 into mysql,显然CarService自动注入的CarDao使用了默认构造函数构造的实例。是否可以通过注解指定使用spring.xml中配置的sqliteCarDao呢?
我们可以修改下CarService类,通过Qualifier注解来指定要使用的bean的名字。
如下,在指定Autowired注解时,同时指定Qualifier注解指定bean的名字

    @Autowired
    @Qualifier("sqliteCarDao")
    private CarDao carDao;

重新运行下App.java 这次输出的是inserting car 宝马 into sqlite,这次使用了spring.xml中配置的bean了。
在文中开头我们还提到了Resouce注解,这个注解可以指定名字注入,我们再次修改下CarService类:

@Resource(name="sqliteCarDao") 
private CarDao carDao;

javax.annotation.Resource注解实现的效果和@Autowired+@Qualifier的效果是一样的。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值