Java annotation-based configuration of Spring4.X (with the birth of SpringBoot)

Recently, the project uses SpringBoot, and it is amazing that it can run without xml configuration. It has not been investigated in detail before.

When it comes to SpringBoot, I need to mention:

1. The historical version and configuration of

Spring 1. Spring 1.x
is completely xml configuration.
Disadvantages: As the project expands, there are more and more xml, and the xml needs to be classified.

2. Spring 2.x
can use annotations in the era of JDK 1.5 update. Spring 2.X started using annotations.

So the question is: using xml? Or use annotations?
Obtain the best practice:
use xml for the basic configuration of the application, and use annotations for business.

3. Spring 3.x
recommends using java code configuration annotations instead of xml

@Configuration, @Bean

4. Spring 4.x

SpringBoot is born



2.

The difference between Spring 4.x @Component and @Bean https://stackoverflow.com/ questions/10604298/spring-component-versus-bean

@Component is added to the class;
@Bean is added to the method; and you can manually specify which specific instance of the class to generate.


3. The difference between Spring 4.x @Resource and @Autowired

https://stackoverflow.com/questions/4093504/resource-vs-autowired

@Resource is matched by Name, and @Autowired is called when no corresponding name is found.
The name can be specified with the @Resource(name="blah") attribute, the default is to use the variable name.

@Autowired is matched by Type. Often used in conjunction with @Autowired @Qualifier("blah") .

The third way of dependency injection:
@Qualifier
@Retention(RUNTIME)
public @interface YourQualifier {}

@YourQualifier
@Component
public class SomeBean implements Foo { .. }

And then
@Inject @YourQualifier private Foo foo;



@RESOURCE VS @AUTOWIRED VS @INJECT

ANNOTATION PACKAGE SOURCE
@Resource javax.annotation Java
@Inject javax.inject Java
@Qualifier javax.inject Java
@Autowired org.springframework.bean.factory Spring



@Resource
@Qualifier("person")
private Party party;

@Autowired
@Qualifier("person")
private Party party;

@Inject
@Qualifier("person")
private Party party;



https://blogs.sourceallies.com/2011/08/spring-injection-with-resource-and-autowired/



四、Spring 4.x @PropertySources 与 @PropertySource

https://www.mkyong.com/spring/spring-propertysources-example/

Some enhancements on Spring 4.

- Introduces new @PropertySources to support Java 8 and better way to include multiple properties files.
	@Configuration
	@PropertySources({
		@PropertySource("classpath:config.properties"),
		@PropertySource("classpath:db.properties")
	})
	public class AppConfig {
		//...
	}

- Allow @PropertySource to ignore the not found properties file.
	@Configuration
	@PropertySource("classpath:missing.properties")
	public class AppConfig {
		//...
	}

If missing.properties is not found, the system is unable to start and throws FileNotFoundException

Caused by: java.io.FileNotFoundException:
class path resource [missiong.properties] cannot be opened because it does not exist

In Spring 4, you can use ignoreResourceNotFound to ignore the not found properties file

	@Configuration
	@PropertySource(value="classpath:missing.properties", ignoreResourceNotFound=true)
	public class AppConfig {
		//...
	}

@PropertySources({
    @PropertySource(value = "classpath:missing.properties", ignoreResourceNotFound=true),
    @PropertySource("classpath:config.properties")
})





-

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326609963&siteId=291194637