About Spring DI ① Is a continuation of
Mainly deals with Configuration
** Bean definition file **
--It acts as a bridge between Bean and DI container, which defines [which instance] and [which initial value] to pass to DI container. --May be close to the mapping file in ORM ――It can be said that it is the [external] itself of the basic function of DI, which is [having object information externally]. --Maintenance is easier by putting the state of objects together in these files instead of putting them separately in the source code.
--There are four types: Java classes, XML files, and two combinations of annotations.
** Java-based Configuration **
JavaConfig.java
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class HogeConfig {
@Bean
HogeService hogeService() {
return new HogeService():
}
@Bean
AAABean aAABean() {
return new AAABean(hogeService());
}
@Bean(name = "bBean")
BBBBean bBBBean() {
return new bBBBean();
}
}
--Explicitly add @Configuration annotation to Configuration class --Multiple annotations can be added --You can refer to other beans by adding arguments to each method. --You can also explicitly define the bean name (name = "hoge") Try it when getting an instance from a DI container
** XML-based Configuration **
xmlConfig.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http;//www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="hogeService" class="jp.co.hogeProgram.HogeService" />
<bean id="aAABean" class="jp.co.hogeProgram.AAABean">
<constructor-arg ref="hogeService"/>
</bean>
<bean id="bBean" class="jp.co.hogeProgram.BBBBean>
<constructor-arg value="aaa"/>
</bean>
</beans>
--Define a bean inside the bean element --You can also pass other beans and scalar values with constoructor-arg
Since it is necessary to write the definition for each of the above two as many as the number of beans, it takes time and effort, so it is common to perform DI by combining the following annotation bases.
** Annotation-based Configuration **
Bean
Hoge.java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class Hoge {
Auto wiring
By describing this annotation, FugaService will automatically fetch the bean from the DI container.
@Autowired
public Hoge(FugaService fuga);
Describe the process
}
Config class (xml omitted) In this case, look for a class under jp.co.hoge
AppCongig.java
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("jp.co.hoge")
public class AppConfig {
}
--The class with @Component annotation can be automatically set in the DI container by "Component scan". This is called "auto wiring"
--There are multiple annotations depending on the role of the class such as @Service as well as @Component, but it is basically the same as @Component.
--To enable component scan, @ComponentScan ("package") in the Config class or XML, or describe the package in the context: component-scan element and look for the annotated class that exists under the package.
--The wider the scan range package, the longer it will take, so you need to set an appropriate range.
I plan to do bean association
Recommended Posts