samwellwang

samwellwang

coder
twitter

Annotations in Spring

In fact, I have basically never used annotations in my daily life. But I heard from other classmates that their companies use them quite a lot, and I want to learn a bit. I found that annotations are really amazing... they can completely abandon XML configuration... I hope our company can also use annotations for development...

2020-05-09

In fact, I have basically never used annotations in my daily life. But I heard from other classmates that their companies use them quite a lot, and I want to learn a bit. I found that annotations are really amazing... they can completely abandon XML configuration... I hope our company can also use annotations for development...

Annotations:
@Component: Represents a component
@Autowired: Specifies the injection of a bean of the specified type into this field
@Configuration: Indicates that it is a configuration class
@ComponentScan: Scans the entire package, automatically creating all beans marked as Component and assembling them according to Autowired

Just need to:
Each bean is marked as @Component and @Autowired is used correctly for injection;
Configuration classes are marked as @Configuration and @ComponentScan;
All beans are within the specified package and its sub-packages.

@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE): Creates a prototype bean
@Order: Specifies the order of beans in the list, marking beans as Order(1), Order(2)...
@Autowired(required = false): Skips if the corresponding bean cannot be found
@Bean(“Second”): Creates an alias for the same type of bean
@Primary: Specifies priority

@Autowired(required = false)
@Qualifier(“z”) // Specifies the injection name as "z" for ZoneId

@Value(“classpath:/logo.txt”) Injects Resource

@PropertySource(“app.properties”) Injects configuration file
@Value(“${app.zone}”) Injects the value from the configuration file
String zoneId;

“${app.zone}” means reading the value for the key app.zone; if the key does not exist, an error will occur on startup;
“${app.zone}” means reading the value for the key app.zone, but if the key does not exist, it uses the default value Z.

Injecting a JavaBean that holds all configurations
@Component
public class SmtpConfig {
@Value(“${smtp.host}”)
private String host;

@Value("${smtp.port:25}")
private int port;

public String getHost() {
    return host;
}

public int getPort() {
    return port;
}

}
Reading configuration information from the JavaBean
@Component
public class MailService {
@Value(“#{smtpConfig.host}”)
private String smtpHost;

@Value("#{smtpConfig.port}")
private int smtpPort;

}

@Profile: Indicates different environments native test production

@Conditional(OnSmtpEnvCondition.class): Conditional injection
@ConditionalOnProperty(name=”app.smtp”, havingValue=”true”) Creates if app.smtp=true exists in the configuration file
@ConditionalOnClass(name = “javax.mail.Transport”) Creates if the class javax.mail.Transport exists in the current classpath

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.