注解注入属性

注解注入顾名思义就是通过注解来实现注入,Spring和注入相关的常见注解有Autowired、Resource、Qualifier、Service、Controller、Repository、Component。
  • Autowired是自动注入,自动从spring的上下文找到合适的bean来注入
  • Resource用来指定名称注入
  • Qualifier和Autowired配合使用,指定bean的名称
  • Service,Controller,Repository分别标记类是Service层类,Controller层类,数据存储层的类,spring扫描注解配置时,会标记这些类要生成bean。
  • Component是一种泛指,标记类是组件,spring扫描注解配置时,会标记这些类要生成bean。

上面的Autowired和Resource是用来修饰字段,构造函数,或者设置方法,并做注入的。
Service,Controller,Repository,Component则是用来修饰类,标记这些类要生成bean。

carDao类

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);
    }

}
caarService类

package cn.outofmemory.helloannotation;

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

@Service
public class CarService {

    @Autowired(required=“true”)
    private CarDao carDao;

    public void addCar(String car) {
        this.carDao.insertCar(car);
    }
}

测试
    
package cn.outofmemory.helloannotation;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
        ApplicationContext appContext = new AnnotationConfigApplicationContext("cn.outofmemory.helloannotation");
        CarService service = appContext.getBean(CarService.class);
        service.addCar("宝马");
    }
}

配置文件
    
<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;

/**
 * Hello world!
 *
 */
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")//name为对象名
    private CarDao carDao;

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


1.创建service类,创建dao类,在service得到到对象
    注入属性第一个注解@Autowired


    (1)创建dao和service对象
 
@Component(value=“userDao”)
public class UserDao{
......
}
@Service(value="userService")
public class UserService{
     @Autowired //在service类类里面定义dao类型属性

    private void add(){
        userDao.add();
    }

}

    

        

                      
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值