Detailed introduction to Spring


This article is followed by the detailed introduction to spring (1). It is recommended to read the first article before reading this article. The links are as follows:

Detailed introduction to Spring (1)

I. spring injection mode

1. set method injection

<bean name="user" class="com.jichi.entity.User" >
  <property name="name" value="Xiao Ming"></property>
  <property name="age" value="18"></property>

2. Construction method injection

<bean name="user" class="com.jichi.entity.User" >
    <constructor-arg name="name" value="Xiaohong" ></constructor-arg>
  <constructor-arg name="age" value="50"></constructor-arg>

3. p namespace injection

<bean name="user" class="com.jichi.entity.User" p:name="Xiao Bai" p:age="10"></bean>

4. spel expression injection

    <bean name="user" class="com.jichi.entity.User">
        <property name="name" value="Xiaohong"></property>
        <property name="age" value="18"></property>
    <bean name="user1" class="com.jichi.entity.User">
        <property name="name" value="#{}"></property>
        <property name="age" value="#{user.age}"></property>

II. spring complex type injection

public class Collection {

    public String[] arr;
    public List<String> list;
    public Map<String,Object> map;

    public Properties props;

    public String[] getArr() {
        return arr;

    public void setArr(String[] arr) {
        this.arr = arr;

    public List<String> getList() {
        return list;

    public void setList(List<String> list) {
        this.list = list;

    public Map<String, Object> getMap() {
        return map;

    public void setMap(Map<String, Object> map) { = map;

    public Properties getProps() {
        return props;

    public void setProps(Properties props) {
        this.props = props;

    public String toString() {
        return "Collection [arr=" + Arrays.toString(arr) + ", list=" + list + ", map=" + map + ", props=" + props + "]";

1. Array type injection

        <bean name="collect" class="com.jichi.entity.Collection">
            <property name="arr">

2. list type injection

        <bean name="collect" class="com.jichi.entity.Collection">
            <property name="list">

3. map type injection

        <bean name="collect" class="com.jichi.entity.Collection">
            <property name="map">
                    <entry key="name" value="xiaohei"></entry>
                    <entry key="age" value="18"></entry>

4. properties type injection

        <bean name="collect" class="com.jichi.entity.Collection">
            <property name="props">
                    <prop key="name">xiaohei</prop>
                    <prop key="age">18</prop>

3. Configure spring to start initialization with web project

Configure in web.xml.


IV. sub configuration files of spring

Method 1:

ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext1.xml","applicationContext2.xml")

Mode two:

<import resource="applicationContext.xml"></import>

V. spring annotation configuration

1. Enable annotation scanning

<context:component-scan base-package="com.jichi.entity"></context:component-scan>

Scan for annotations in all classes under com.jichi.entity.

2. Add notes to the class

public class User {

6. spring common notes

1, @ composet, @ Controller, @ Service, @ Repository are four component annotations that act on the class. There is no difference between the four notes, just for convenience.

2. The @ Scope annotation works on the class.

@Scope(scopeName="singleton")  //Singleton mode
public class User {
@Scope(scopeName="prototype")  //manyExample mode
public class User {

3, @ Value is used to inject common type Value

The first way is to break the encapsulation of objects by reflecting the value of filed on attributes.

private String name;

The second way is to assign a value through the set method without destroying the encapsulation of the object.

    public void setName(String name) { = name;

4 @ Autowired, @ Resource, @ Qualifier comments

Please refer to the previous blog for details on the assembly method of reference type.

    private Car car;
    private Car car;

5, @ PostConstruct and @ PreDestroy

    @PostConstruct   //Call before creating object
    public void init(){
    @PreDestroy     //Call before object destruction
    public void destory(){

Tags: Java Spring xml

Posted on Mon, 02 Dec 2019 02:41:30 -0500 by nikes90