The behavior of creating collaborative relationships between application objects is called assembly (wiring), which is also the nature of Di.
How to assemble beans in spring
Spring provides three ways to assemble beans.
- Implicit bean discovery mechanism and automatic assembly
- Java Config
- XML Config
Spring can choose one or more combinations of three ways. However, it is recommended to use automatic assembly to avoid the creation of complex XML configuration files and Java code, followed by the use of type-safe Java Config, if you do not meet the requirements of the final consideration of XML Config.
Automatic configuration
With the help of the following two aspects spring can implement an automated bean configuration.
- Component Scan (component scanning): Automatically discovers the bean created by the context type
- Auto-Provisioning (autowiring) automatically creates inter-object dependencies
One. Component Scan:
@Component
The Java class for the annotation markup is used as the component class. @Component("beanid")
parameter is the name of the bean, and the default is the lowercase class name. @Component
equivalent @Named
annotations;
corresponding XML configuration node
@ComponentScan
Used to start a component scan, the default scan scope is the package that contains the class for the annotation tag. The packet scope of the @ComponentScan(basePackages={"basePackage1","basePackage2"})
scan can be specified with the parameter, @ComponentScan(basePackageClasses={ClassNameA.class,InterfaceB.class})
and the package containing the parameters of these classes or interfaces will serve as the base package scope of the scan, which provides type safety (refactoring-friendly) in comparison to the underlying package of the specified string;
The corresponding XML configuration node is
Two. Automatic assembly
@AutoWired
Automatic assembly capability is provided. This annotation can be used by both constructor injection and setter injection, and in fact it can work on any method. @AutoWired(required=false)
Set to False to not throw an exception when there is no match to the bean. @AutoWired
equivalent to@Inject
Java Config
Components that cannot be used @Conmponent
and @Autowired
annotated, such as third-party class libraries, can be assembled using Java CONFIG. At the same time, Javaconfig is also Java code, they should not contain any business code, in fact, the two should be completely separated, As a result, Java Config code is typically placed in a separate package.
@Configuration
The annotation identification class is a configuration class.
@Bean
The annotated method is declared as a return bean. @Bean(name=‘methodname‘)
The parameter identifies the bean's ID name, which defaults to the method name.
@Beanpublic A getA(){ return new A();}@Beanpublic B getB(){ return new B(getA());}@Beanpublic C getC(){ return new C(getA());}
Building components in the above code B,c uses a constructor that injects a bean, and getA()
b,c beans in a generic Java program hold different instances of a, but not in spring, and all getA()
will be intercepted and returned to the same bean. By default, spring's bean is singleton.
You can create b,c beans in a much simpler way, with the following code:
@Beanpublic B getB(){ return new B(A a); // 也可以使用setter方式, 或者任何其他方式创建bean}@Beanpublic C getC(){ return new C(A a);}
@Import({ClassnameA.class,ClassnameB.class})
For beans of different locations, Javaconfig can use this annotation to import additional configuration classes.
XML Config
XML is the most primitive form of assembly in spring, and it is helpful to be familiar with XML config to understand historical projects and to incrementally refactor to Java Config or automatic configuration.
There are two ways in which the XML configuration constructor (setter) is injected, both of which have pros and cons:
- C-named control or the P-namespace feature is concise but cannot be injected into a set; You need to introduce XSD when you use it.
- At the top of the configuration file, you need to declare multiple XSD.
<bean class="com.xlx.c02.MyClass">
A bean is declared, the default ID is com.xlx.c02.myclass#0, and you can use the property ID to specify a name for the Bean <bean id="myclass" class="com.xlx.c02.MyClass">
.
<constructor-arg>
The parameters used to configure the constructor injection, which are used when referencing other beans, are <constructor-arg ref="otherBeanId">
equivalent to the C-namespace attribute c:otherBean-ref=‘otherBeanId‘
, or the parameter numeric index c:_0-ref=‘otherBeanId‘
(a single argument can omit a number), or you can inject a literal value <constructor-arg value="this is value">
equal to the C-namespace attribute c:_name=‘jery‘
, or c:_0=‘jery‘
(single parameter can omit number)
- Assembly set
<constuctor-arg> <list> <value>first</value> <value>second</value> </list></constuctor-arg>
- Set properties:
<property name="propertyName" ref="beanId">
orp:propertyName-ref="beanId"
- The functionality of the Util namespace simplifies some of the literal property settings, and you need to declare the XSD first.
<!--定义集合--><util:list id="trackList"> <value>first</value> <value>second</value></util:list><!--p-命名的方式引用集合-->p:tracks-ref="trackList"
Hybrid configuration
One. Java config references other
@Import
You can import Java configuration classes
@ImportResource
You can import XML configurations, such as@ImportResource("classpath:my-config.xml")
Two. XML Config references Other
<import resource=‘my-config.xml‘>
referencing other XML configurations
<bean>
Referencing Java Config
Regardless of how it is referenced, creating a root configuration makes the configuration more hierarchical and easier to read and manage.
Spring Series (ii) Bean assembly