Simple learning of Spring framework

1. Basic configuration of spring

  1. When xml is injected, the value of value is given to the constructor. When it is created, it is not assigned a name variable
  2. Multi tag getBean
  3. import, which is used to merge xml configurations (generally used by teams, when each person develops different types and needs to configure them together)
    The remaining xml configuration files
    Assemble into an xml

2.set injection

1. Ordinary injection
2.bean injection
3. Collective injection
4.list injection
5.map injection
6.set injection
7. Empty injection
8.props injection (information)

<?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 injection <-->
    <bean id="address" class="com.Ethan.pojo.Address">
        <property name="address" value="Gui'an"/>
    </bean>

    <bean id="student" class="com.Ethan.pojo.Student">
    	<!-->Ordinary injection <-->
        <property name="name" value="Xiao Jun"/>
        <!-->Bean injection <-->
        <property name="address" ref="address"/> 
		<!-->array Array injection <-->
        <property name="book">
            <array>
                <value>c plus plus</value>
                <value>java</value>
                <value>python</value>
            </array>
        </property>
		<!-->list Set injection <-->
        <property name="hobbys">
            <list>
                <value>sing</value>
                <value>jump</value>
                <value>rap</value>
                <value>Basketball</value>
            </list>
        </property>
        <!-->Map Set injection <-->
        <property name="card">
            <map>
                <entry key="ID" value="333333444444555555"/>
                <entry key="bank card" value="999999999999999999"/>
            </map>
        </property>
		<!-->set injection <-->
        <property name="game">
            <set>
                <value>DOTA</value>
                <value>LOL</value>
                <value>CF</value>
            </set>
        </property>
		<!-->Empty injection <-->
        <property name="wife">
            <null/>
        </property>
		<!-->property injection <-->
        <property name="info">
            <props>
                <prop key="Student number"> 202105100</prop>
                <prop key="full name">Xiao Jun</prop>
                <prop key="sex">man</prop>
            </props>
        </property>

    </bean>


</beans>

2.1.p named injection and c named injection

Introducing naming rules into xml configuration files

		xmlns:p="http://www.springframework.org/schema/p"
        xmlns:c="http://www.springframework.org/schema/c"

I'm surprised that I clearly introduced two rules into my idea and set the URL in setting. But it still can't be used. However, these two injection rules are not the focus of learning.

3.Junit

Test @ test
Later, I'll come back and explain this Junit in detail when I have time. Just remember its usage.

4. Scope of bean

The following is an official picture, which focuses on the scope of bean s

Note: if you can't see the font clearly, you can go Official website Check (link attached)

1.prototype: prototype mode, which simply means that a new object will be created every time you get the object. See the official figure for details


We can take a look at the specific implementation of the code

  <bean id="user" class="com.Ethan.pojo.user"   scope="prototype"/>

Back to the test class, test whether the objects we get in the container are the same object or multiple objects

	@Test
    public void test(){

  
        ApplicationContext context = new ClassPathXmlApplicationContext("userbeans.xml");

        user user1 = (user) context.getBean("user");

        user user2 = (user) context.getBean("user");

        System.out.println(user1==user2);

        System.out.println(user1.hashCode());

        System.out.println(user2.hashCode());

        //Prototype mode
    }

hashcode: the value comes from the integer value converted from the internal address of this object. (I found it on the Internet and then went deeper. It's troublesome. I didn't read it again)

We can see whether the addresses of objects are the same to judge whether multiple objects or a single object are generated.

Obviously, user1 and user2 are two different objects

2.singleton: Singleton mode. The objects obtained from the container are the same (spring default)

I think the figure given on the official website can be more intuitive for us to understand singleton

   <bean id="user" class="com.Ethan.pojo.user"   scope="singleton"/>
public void test(){

        //Singleton mode

        ApplicationContext context = new ClassPathXmlApplicationContext("userbeans.xml");

        user user1 = (user) context.getBean("user");

        user user2 = (user) context.getBean("user");

        System.out.println(user1==user2);

        System.out.println(user1.hashCode());

        System.out.println(user2.hashCode());
    }

We can see our output

The output addresses are consistent, indicating that user1 and user2 we get are the same object.
Understanding of spring: when we use these objects. The configuration file in xml will add these objects to the IOC container for us. We can just get these objects

5.2 automatic assembly

1.AutoWire:1.ByName,2.ByType

  • byName: it will automatically find the bean id corresponding to the value after the set method of its own object in the container context
  • byType: it will automatically find bean s with the same object attribute type in the container context!

@Autowired: it will automatically find bean s with the same id, in other words, byName
@Qualifier (value = ""): an object with the same beanId as value will be found
**@Autowired(**required = false)

@Resource is deprecated

Keywords: Java Spring intellij-idea

Added by johnrcornell on Wed, 02 Feb 2022 10:54:26 +0200