---
title: "@Autowired和new的区别"
author: "linsky328"
date: "2018-01-25T10:35:57Z"
tags: []
categories: ["编程语言"]
slug: "autowired-new"
draft: false
lastmod: "2018-01-25T10:35:45Z"
id: 2408994
url: "https://linsky328.iteye.com/blog/2408994"
---

@Autowired相当于setter，在注入之前，对象已经实例化，是在这个接口注解的时候实例化的； 
而new只是实例化一个对象，而且new的对象不能调用注入的其他类 
eg: 
1、控制器

@controller
public class BusinessShopShoesController extends BaseController {

    @Autowired
    private ShoesService shoesService;//相当于setter，已经实例化
    }

2、业务层

@service
public class ShoesService  extends CrudService<ShoesDao, Shoes> {

    @Autowired
    ShoesModelDao shoesModelDao;
    @Transactional(readOnly = false)
    public Shoes get(int id)
    {
        return shoesModelDao.get(id);
    }   
    }

此时如果1 中new一个service，那么就不能调用2 中的Dao了，因为DAO是依赖注入的