@Autowired自动装配对象和new对象的区别

@Autowired相当于setter,在注入之前,对象已经实例化,是在这个接口注解的时候实例化的; 
而new只是实例化一个对象,而且new的对象不能调用注入的其他类 
eg: 
1、控制器

@controller
public class BusinessShopShoesController extends BaseController {
 
    @Autowired
    private ShoesService shoesService;//相当于setter,已经实例化
    }

2、业务层@controller

@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是依赖注入的

转载:
https://blog.csdn.net/p358278505/article/details/78404828

猜你喜欢

转载自blog.csdn.net/xfy18317776108/article/details/81746002
今日推荐