Home > 类库下载 > java类库 > spring annotation

spring annotation

高洛峰
Release: 2016-10-17 09:19:12
Original
1522 people have browsed it

Is having more choices a good thing?

After more than ten years of rapid development and updates, Spring has brought a large number of fans with its unique innovation and more choices in terms of choices! For example, the assembly of spring beans (explicit configuration in XML, explicit configuration in Java, implicit bean discovery mechanism and automatic assembly), diversification of annotations (basic annotations, jsr250, jsr330) and so on!

And are so many repeated choices really a good thing? Does this also mean that the cost of learning and mastering has increased, making it more difficult to master?
An analogy: you can also save the country. A psychopath called spring tells you that you can save the country by practicing medicine (xml explicit configuration). When you are studying medicine, it tells you that you can also join the army to save the country (Java display configuration) , when you are about to succeed in practicing capture and shooting, it will tell you that there is actually a simpler way to educate and save the country (automatic assembly of beans)!
As a beginner, I personally think that from learning servlet to jsp to struts to now spring, the pits are getting bigger and bigger!

Without further ado, let’s introduce a few simple annotations:

@Autowired annotation: realize automatic assembly.
1. The following two classes need to be injected into the spring xml configuration file:

    <beans>
        <bean id="testA" class="..../TestA" />
        <bean id="testUtil" class="..../TestUtil" />
    </beans>
Copy after login
Copy after login

2. If you want to assemble the testA class in the testUtil class, you can use the @Autowired annotation in the testUtil class;
If you want to inject TestA In TestUtil, the code is as follows:

    public class TestUtil{
        @Autowired
        private Test testA;
        .......
    }
Copy after login

Simply put, autowiring is a way for Spring to automatically satisfy bean dependencies. In the process of satisfying dependencies, it will search for other beans in the Spring application context that match the requirements of a certain bean. beans.
@Autowired annotation can be used not only on constructors, but also on methods and properties. If there is no matching bean, Spring will throw an exception when the application context is created. To avoid exceptions, you can set the required attribute of @Autowired to false: However, you need to be careful when setting the required attribute to false. If there is no null check in your code, this unassembled property may cause a NullPointerException.
@Autowired is a spring-specific annotation. If you are unwilling to use Spring-specific annotations everywhere in your code to complete automatic assembly tasks, then you can consider replacing it with @Inject.
In automatic assembly, Spring supports both @Inject (JSR330 annotation) and @Autowired. Although there are some subtle differences between @Inject and @Autowired, they are interchangeable in most scenarios.
Note: 1. It only provides automatic assembly and does not provide injection; injection is a prerequisite.
            2. To achieve automatic assembly, you need to import support
                          or you can also use                                                                                                                                                                                                              ​-------------------------------------------------- -------------------------------------------------- -------------------------------------------------- ------------------

@Qualifier annotation: Limit the conditions for dependency injection.
If the @Autowired annotated dependency can only find one instance corresponding to it in the container, everything is OK, but if there are multiple object instances of the same type, the @Qualifier annotation will take effect.
If the Test class has two implementations, TestA and TestB, the configuration code in the spring configuration file is as follows:

    <beans>
        <bean id="testA" class="..../TestA" />
        <bean id="testB" class="..../TestB" />
        <bean id="testUtil" class="..../TestUtil" />
    </beans>
Copy after login

If you want to inject TestA into TestUtil, the code is as follows:

    public class TestUtil{
        @Autowired
        @Qualifier("testA")
        private Test testA;
        private Test testB;
        .......
    }
Copy after login

-------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- ----------------------------------------

@Resource(name="") annotation: Implementation Injection of beans. Its function is similar to the @Autowired annotation.

1. Since this annotation was introduced by JSR250, you need to import the support of jsr250 related jar package before using it. Add the following content to the pom.xml file:

        <!-- jsr250注解包的引入 -->
        <dependency>
            <groupId>javax.annotation</groupId>
            <artifactId>jsr250-api</artifactId>
            <version>1.0</version>
        </dependency>
Copy after login

2. Inject related classes in the spring configuration file :

If the testA class needs to be introduced into the testUtil class, the configuration code in the spring configuration file is as follows:


    <beans>
        <bean id="testA" class="..../TestA" />
        <bean id="testUtil" class="..../TestUtil" />
    </beans>
3.在 testUtil 中加入
Copy after login

@Resource (name="") Note:

If you want to inject TestA into TestUtil, the code is as follows:


    public class TestUtil{
        @Resource(name=“testA”)
        private Test testA;
        private Test testB;
        .......
    }
Copy after login

Note: The name in @Resource (name="") specifies the object to be injected, and testA is the id of the bean defined in the spring configuration file.

4. At least one of the following two packages with explanation annotations must be introduced into the spring configuration file. Based on the principle of laziness, I personally like the context element.


<context:annotation-config />    
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
Copy after login


备注:JSR250注解有 @PostConstruct,@PreDestroy 。
JSR330注解有 @Inject (代替 @AutoWired)、@Named (代替 @Component)

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

@ComponentScan(basePackages=“×××××”)注解:启用组件扫描基础包。
1.在 spring 配置文件中加入 实现。

配置完成后,会扫描 com.lh 路径下的所有标注了的相应注解的类,并添加到IOC容器中,实现依赖注入。
备注:加入该元素后,就不需要再向 spring 配置文件中注入相关的类,即不需要再使用如下配置:

    <beans>
        <bean id="testA" class="..../TestA" />
        <bean id="testUtil" class="..../TestUtil" />
    </beans>
Copy after login
Copy after login

2. 默认扫描的注解类型是 @Component ,所以需要在相应的类中应用这个注解进行标注。

    @Component
    public class TestUtil{
            @Autowired
            private Test testA;
            .......
        }
            @Component
    public class TestA{
    private int a;
    private String ss;
    ...
    }
Copy after login


备注:(1)需要在两个类中都标注 @Component 注解。
             (2)该注解是类级别的,它添加在类之上。
             (3)@Component("id号")注解:表明该类会作为组件类,并告知Spring要为这个类创建bean。若id号为空,则默认使用以小写开头的类名!若使用自定义id号,则在后续的引用中需要引入以该id为标记。


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template