Configuration files required by spring

Keywords: Java JavaEE Spring

1, beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""

   <bean id="MysqlImpl" class="com.kuang.dao.impl.UserDaoMySqlImpl"/>
   <bean id="OracleImpl" class="com.kuang.dao.impl.UserDaoOracleImpl"/>

   <bean id="ServiceImpl" class="com.kuang.service.impl.UserServiceImpl">
       <!--be careful: there name Is not an attribute , But set The part behind the method , Initial lowercase-->
       <!--Reference another bean , Not with value But with ref-->
       <property name="userDao" ref="OracleImpl"/><!--Which interface can be directly configured here-->


The construction method here is parameterless construction and injected through the set method
The following method is parametric construction

<!-- First basis index Parameter subscript setting -->
<bean id="userT" class="com.kuang.pojo.UserT">
   <!-- index Refers to the construction method , Subscript starts at 0 -->
   <constructor-arg index="0" value="kuangshen2"/>
<!-- The second is set according to the parameter name -->
<bean id="userT" class="com.kuang.pojo.UserT">
   <!-- name Refers to the parameter name -->
   <constructor-arg name="name" value="kuangshen2"/>
<!-- The third is set according to the parameter type(Not recommended) -->
<bean id="userT" class="com.kuang.pojo.UserT">
   <constructor-arg type="java.lang.String" value="kuangshen2"/>

2. Alias configuration in spring
① Alias sets an alias for a bean. You can set multiple aliases

<!--Setting alias: getting Bean You can use alias to get-->
<alias name="userT" alias="userNew"/>

② Configuring aliases in bean s

<!--bean namely java object,from Spring Create and manage-->

   id yes bean Identifier of,To be unique,If not configured id,name Is the default identifier
   If configured id,Configured again name,that name It's an alias
   name You can set multiple aliases,You can use commas,semicolon,Space separated
   If not configured id and name,Can be based on applicationContext.getBean(.class)Get object;

class yes bean Fully qualified name of=Package name+Class name
<bean id="hello" name="hello2 h2,h3;h4" class="com.kuang.pojo.Hello">
   <property name="name" value="Spring"/>

3. Automatic assembly of bean s
There are three assembly mechanisms for bean s in Spring:
① Explicit configuration in xml; (configure bean s in xml)
② Explicit configuration in java; (configured in notes)
③ Implicit bean discovery mechanism and automatic assembly.

Spring's automatic assembly needs to be implemented from two perspectives, or two operations:

Component scanning: spring will automatically discover the bean s created in the application context;
Autowiring: spring automatically satisfies the dependencies between bean s, which is what we call IoC/DI;
The combination of component scanning and automatic assembly has great power to minimize the configuration of display.

Modify the bean configuration and add an attribute autowire = "byName"

<bean id="user" class="com.kuang.pojo.User" autowire="byName">
   <property name="str" value="qinjiang"/>

Using autowire byType first needs to ensure that objects of the same type are unique in the spring container. If it is not unique, a non unique exception will be reported

<bean id="user" class="com.kuang.pojo.User" autowire="byType">
   <property name="str" value="qinjiang"/>

When a bean node has an autowire byName attribute.

All set method names in its class, such as setCat, will be searched to obtain a string with set removed and lowercase, that is, cat.
Go to the spring container to find whether there is an object with this string name id.
If any, take out the injection; If not, a null pointer exception is reported.

4. Automatic assembly using annotations

1. Introduce the context file header into the spring configuration file


2. Enable attribute annotation support!


3. @ Autowired is automatically transferred by type and does not support id matching.
You need to import the package of spring AOP!

Annotation example

public class User {
   private Cat cat;
   private Dog dog;
   private String str;

   public Cat getCat() {
       return cat;
   public Dog getDog() {
       return dog;
   public String getStr() {
       return str;

The configuration file is as follows


<bean id="dog" class="com.kuang.pojo.Dog"/>
<bean id="cat" class="com.kuang.pojo.Cat"/>
<bean id="user" class="com.kuang.pojo.User"/>

Note: @ Autowired(required=false) Description: false, the object can be null; true, the object must be saved and cannot be null.

In addition to @ Autowired, there are @ Qualifier and @ Resource

@Similarities and differences between Autowired and @ Resource:

1. Both @ Autowired and @ Resource can be used to assemble bean s. Can be written on a field or on a setter method.

2. @ Autowired is assembled by type by default (belonging to the spring specification). By default, dependent objects must exist. If null is allowed
Value, you can set its required property to false, such as @ Autowired(required=false)
, if we want to use the name assembly, we can use it in combination with the @ Qualifier annotation

3. @ Resource (belonging to J2EE complex return), assembled by name by default, and the name can be specified through the name attribute. If the name attribute is not specified, when the annotation is written on the field, the default is to take the field name to search by name. If the annotation is written on the setter method, the default is to take the attribute name for assembly. Assemble by type when no bean matching the name is found. However, it should be noted that once the name attribute is specified, it will only be assembled by name.

They have the same function. They inject objects by annotation, but the execution order is different@ Autowired byType first, @ Resource byName first.

5. Using annotation development
If you want to use annotation form, you must introduce aop package
In the configuration file, a context constraint must also be introduced

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""


Posted by leequalls on Mon, 22 Nov 2021 22:29:45 -0800