java - 注解注入和依赖注入是怎么一回事?
ringa_lee
ringa_lee 2017-04-17 17:51:29
0
4
978

最近开始试着做web项目,从别人的代码看到了漫天的注解。真的很难懂。然后硬着头皮各种补知识。大概了解到Spring的ioc思想,通过控制反转,使程序变得解耦。它好像是通过xml来配置具体的bean实现在使用的时候具体的目标类?它的反转控制大概就是这样的思想吧?但是各种问题也跟着来了。注解注入又是怎么回事啊?就想一个关于使用了Jersey的代码。如下:

public Viewable getItemsView(@QueryParam("limit") @DefaultValue("5") int limit, @QueryParam("start") @DefaultValue("5") int start, @Context HttpServletRequest request) {}
            

在参数前加注解是为了什么?而且看了好多文章都说注解可以实现自动注入?又像如下代码:

public class TodoResource {
    @Context
    UriInfo uriInfo;

    @Context
    HttpRequest request;
    @Context
    HttpResponse response;

    String id;

    public TodoResource(UriInfo uriInfo, Request request, String id) {
        this.uriInfo = uriInfo;
        this.request = request;
        this.id = id;
    }
}

uriInfo和request以及response是怎么做到通过注解就能够为其提供具体的对象引用?还有一个最重要的问题,就是容器如何做到通过注解就能够识别我的具体需要的是哪个参数呢?类装载器自动扫描么?那么又是如何为其赋值的?假如属性的权限是private的。如果直接使用注解对属性进行注入是不是就破坏了封装性?而如果我的属性同时具有Httpresponse 这个类的两个对象,response1和response2,那么又如何确定到底bean容器里哪个才是我需要的对象呢?我大致的理解是这样,同时也带有很多的疑问。不知道有没有能够描述清楚。又或者我的理解本身就是错误的?如果理解本身就是错误的,希望能够详细讲解一下依赖注入与注解注入以及在web项目中注解是如何工作的?如果您有时间的话?不妨在告知一下具体的注解注入实现过程?感激不尽!

ringa_lee
ringa_lee

ringa_lee

reply all(4)
巴扎黑

Let’s take a look at these two articles first:

Notes

Dependency Injection

It can be said that in spring, some dependency injection is achieved through annotation technology!

PHPzhong

Forgot, were annotations introduced in 1.5? Over time, I have almost forgotten the most basic things.
Let’s talk about it roughly. When it comes to annotations, I have to mention:

This is also an annotation, so what is the function of this annotation? It is nothing more than the basic elements for forming a DOC document before the project goes online. It is based on the premise that it will not affect the actual operation of your project code.
As for the annotation implementation details that the questioner is confused about, it is recommended that the questioner can get an overview through the process of customizing annotations and adding custom annotations (see the documentation for more in-depth information).
spring draws on the advantages of annotations and implements dependency injection. How does the spring container manage beans? (You must understand what a singleton is and what a prototype is)
Yes, dependency injection (shielding the private keyword) destroys encapsulation to a certain extent.
In addition, annotations (such as @Autowired) are a way and implementation of injection (the two most basic ways of injection, xml injection and annotation injection). Remember, after adding annotations, you must also scan the xml configuration file for annotations.

If you are not a master, don’t spray

刘奇

Annotation injection greatly simplifies the writing of configuration files

刘奇

Reference http://stackoverflow.com/questions/21588684/spring-aop-annotationannotation

The annotation itself is just a mark, refer to Java's @annotation, and then you configure the scan in the ApplicationContext, and then it sets its value through the marked annotation, such as @service.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template