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>
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; ....... }
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
????????????????????????? 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>
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; ....... }
-------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- ----------------------------------------@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>
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 中加入
@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; ....... }
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"/>
備注:JSR250注解有 @PostConstruct,@PreDestroy 。
JSR330注解有 @Inject (代替 @AutoWired)、@Named (代替 @Component)
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
@ComponentScan(basePackages=“×××××”)注解:?jiǎn)⒂媒M件掃描基礎(chǔ)包。
1.在 spring 配置文件中加入
配置完成后,
備注:加入該元素后,就不需要再向 spring 配置文件中注入相關(guān)的類,即不需要再使用如下配置:
<beans> <bean id="testA" class="..../TestA" /> <bean id="testUtil" class="..../TestUtil" /> </beans>
2.
@Component public class TestUtil{ @Autowired private Test testA; ....... } @Component public class TestA{ private int a; private String ss; ... }
備注:(1)需要在兩個(gè)類中都標(biāo)注 @Component 注解。
???????????? (2)該注解是類級(jí)別的,它添加在類之上。
???????????? (3)@Component("id號(hào)")注解:表明該類會(huì)作為組件類,并告知Spring要為這個(gè)類創(chuàng)建bean。若id號(hào)為空,則默認(rèn)使用以小寫開(kāi)頭的類名!若使用自定義id號(hào),則在后續(xù)的引用中需要引入以該id為標(biāo)記。

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)