当前位置:   article > 正文

04,Spring - 基于xml的spring应用(bean的配置详解)_spring xml配置bean

spring xml配置bean

1,Bean的常用配置一览

在这里插入图片描述


2,Bean的全局id设置<bean id=“” class=“”>

2.1 在beans.xml中不设置id查看默认别名(UserService)

<?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">
    <!--    配置UserServiceImpl-->
    <bean  class="com.study.service.impl.UserServiceImpl">
        <property name="userDao" ref="userDao"/>
    </bean>

    <!--    配置UserDaoImpl-->
    <bean id="userDao" class="com.study.dao.impl.UserDaoImpl"></bean>
</beans>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

2.2 debug 查看默认别名

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-vy9bGRGM-1672904873442)(https://note.youdao.com/yws/res/3845/WEBRESOURCEc370bbc88cefa5eda1a77fb31629ddf2)]

2.3 修改测试类

package com.study.demoTest;

import com.study.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ApplicationContextDemo02 {
    public static void main(String[] args) {

        ApplicationContext applicationContext =
                new ClassPathXmlApplicationContext("beans.xml");

        UserService userService =(UserService)applicationContext.getBean("com.study.service.impl.UserServiceImpl");

        System.out.println(userService);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

3,通过name设置bean的别名 <bean name=“”>

3.1 修改 beans.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">
    <!--    配置UserServiceImpl-->
    <bean id="userService" name="aaa,bbb" class="com.study.service.impl.UserServiceImpl">
        <property name="userDao" ref="userDao"/>
    </bean>

    <!--    配置UserDaoImpl-->
    <bean id="userDao" class="com.study.dao.impl.UserDaoImpl"></bean>
</beans>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

3.2 修改测试类

package com.study.demoTest;

import com.study.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ApplicationContextDemo01 {
    public static void main(String[] args) {

        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");

        UserService userService =(UserService)applicationContext.getBean("aaa");

        System.out.println(userService);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

在这里插入图片描述


4,bean的作用范围 <bean scope=“”>

在这里插入图片描述

4.1 修改 beans.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="userService" class="com.study.service.impl.UserServiceImpl" scope="singleton">
        <property name="userDao" ref="userDao"/>
    </bean>

    <!--    测试原形模式-->
    <bean id="userDao" class="com.study.dao.impl.UserDaoImpl" scope="prototype"></bean>
</beans>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

4.2 applicationContext 测试类

package com.study.demoTest;

import com.study.dao.UserDao;
import com.study.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ApplicationContextDemo02 {
    public static void main(String[] args) {

        ApplicationContext applicationContext =
                new ClassPathXmlApplicationContext("beans.xml");

        UserService userService01 =(UserService)applicationContext.getBean("userService");
        UserService userService02 =(UserService)applicationContext.getBean("userService");

        System.out.println("userService01 =>"+userService01);
        System.out.println("userService02 =>"+userService02);
        System.out.println(userService01 == userService02);

        UserDao userDao01 =(UserDao)applicationContext.getBean("userDao");
        UserDao userDao02 =(UserDao)applicationContext.getBean("userDao");

        System.out.println("userDao01 =>"+userDao01);
        System.out.println("userDao02 =>"+userDao02);
        System.out.println(userDao01 == userDao02);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

在这里插入图片描述

在这里插入图片描述


5,bean 的延迟加载

在这里插入图片描述

5.1 修改beans.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="userService" class="com.study.service.impl.UserServiceImpl" lazy-init="true">
        <property name="userDao" ref="userDao"/>
    </bean>

    <!--    测试原形模式-->
    <bean id="userDao" class="com.study.dao.impl.UserDaoImpl" scope="prototype"></bean>
</beans>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

5.2 修改 applicationContext 测试类

package com.study.demoTest;

import com.study.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ApplicationContextDemo02 {
    public static void main(String[] args) {

        ApplicationContext applicationContext =
                new ClassPathXmlApplicationContext("beans.xml");

        UserService userService01 =(UserService)applicationContext.getBean("userService");

        System.out.println(userService01);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

在这里插入图片描述


6,bean初始化和销毁方法的配置 <bean init-method=“init” destroy-method=“destroy”>

在这里插入图片描述

6.1 修改 UserServiceImpl

package com.study.service.impl;

import com.study.dao.UserDao;
import com.study.service.UserService;

public class UserServiceImpl implements UserService {
    private UserDao userDao;

    //BeanFactory调用该方法,从容器中获得UserDao设置到此处
    public void setUserDao(UserDao userDao){
        System.out.println("beanfactory调用该方法获取到userDao:"+userDao);
        this.userDao = userDao;
    }

    public void init (){
        System.out.println("初始化方法");
    }

    public void destroy (){
        System.out.println("销毁方法");
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

6.2 修改beans.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初始化和销毁-->
    <bean id="userService" class="com.study.service.impl.UserServiceImpl" init-method="init" destroy-method="destroy">
        <property name="userDao" ref="userDao"/>
    </bean>

    <!--    测试原形模式-->
    <bean id="userDao" class="com.study.dao.impl.UserDaoImpl" scope="prototype"></bean>
</beans>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

6.3 修改 applicationContext 测试类

package com.study.demoTest;

import com.study.service.UserService;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ApplicationContextDemo02 {
    public static void main(String[] args) {

        ClassPathXmlApplicationContext applicationContext =
                new ClassPathXmlApplicationContext("beans.xml");

        UserService userService01 =(UserService)applicationContext.getBean("userService");

        System.out.println(userService01);

        //关闭容器
        applicationContext.close();
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

在这里插入图片描述


7,扩展 InitializingBean 接口

在这里插入图片描述

7.1 修改 UserServiceImpl

package com.study.service.impl;

import com.study.dao.UserDao;
import com.study.service.UserService;
import javafx.fxml.Initializable;
import org.springframework.beans.factory.InitializingBean;

public class UserServiceImpl implements UserService, InitializingBean {
    private UserDao userDao;

    //BeanFactory调用该方法,从容器中获得UserDao设置到此处
    public void setUserDao(UserDao userDao){
        System.out.println("beanfactory调用该方法获取到userDao:"+userDao);
        this.userDao = userDao;
    }

    public void init (){
        System.out.println("初始化方法");
    }

    public void destroy (){
        System.out.println("销毁方法");
    }

    //执行时间早与 init
    public void afterPropertiesSet() throws Exception {
        System.out.println("afterPropertiesSet");
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

在这里插入图片描述


8,spring实例化配置

在这里插入图片描述

8.1 构造方法实例化

在这里插入图片描述

8.1.1 修改 UserServiceImpl

package com.study.service.impl;

import com.study.dao.UserDao;
import com.study.service.UserService;

public class UserServiceImpl implements UserService {

    public UserServiceImpl() {
        System.out.println("无参构造方法执行");
    }

    public UserServiceImpl(String username,String age) {
        System.out.println("有参构造方法执行 username :"+ username);
        System.out.println("有参构造方法执行 age :"+ age);
    }

    public void setUserDao(UserDao userDao){
        System.out.println("beanfactory调用该方法获取到userDao:"+userDao);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

8.1.2 修改 beans.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="userService" class="com.study.service.impl.UserServiceImpl">
        <constructor-arg name="username" value="李乃龙"/>
        <constructor-arg name="age" value="26"/>
        <property name="userDao" ref="userDao"/>
    </bean>

    <!--    测试原形模式-->
    <bean id="userDao" class="com.study.dao.impl.UserDaoImpl" scope="prototype"></bean>
</beans>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

8.1.3 修改 applicationContext 测试类

package com.study.demoTest;

import com.study.service.UserService;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ApplicationContextDemo02 {
    public static void main(String[] args) {

        ClassPathXmlApplicationContext applicationContext =
                new ClassPathXmlApplicationContext("beans.xml");

        UserService userService01 =(UserService)applicationContext.getBean("userService");

        System.out.println(userService01);

        //关闭容器
        applicationContext.close();
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

在这里插入图片描述

8.2 工厂方式实例化bean

在这里插入图片描述

8.2.1 静态工厂方式实例化bean

8.2.1.1 创建静态工厂类
package com.study.factory;

import com.study.dao.UserDao;
import com.study.dao.impl.UserDaoImpl;

//创建静态工厂
public class MyBeanFactory1 {
    public static UserDao userDao(){
        return new UserDaoImpl();
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
8.2.1.2 修改beans.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
    factory-method指定一个类中的一个方法的返回值保存到bean容器中
    -->
    <bean id="userDao2" class="com.study.factory.MyBeanFactory1" factory-method="userDao"></bean>

</beans>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
8.2.1.3 创建 ApplicationContextDemo03 测试类
package com.study.demoTest;

import com.study.dao.UserDao;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ApplicationContextDemo03 {
    public static void main(String[] args) {

        ClassPathXmlApplicationContext applicationContext =
                new ClassPathXmlApplicationContext("beans.xml");

        UserDao userDao =(UserDao)applicationContext.getBean("userDao2");

        System.out.println(userDao);

    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

在这里插入图片描述

8.2.2 实例工厂方式实例化bean

8.2.2.1 创建实例化工厂 MyBeanFactory2
package com.study.factory;

import com.study.dao.UserDao;
import com.study.dao.impl.UserDaoImpl;
//创建实例化工厂
public class MyBeanFactory2 {
    public UserDao userDao(){
        //可以进行其他业务逻辑操作
        return new UserDaoImpl();
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
8.2.2.2 修改beans.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="userDao1" factory-bean="myBeanFactory2" factory-method="userDao"/>
    <bean id="myBeanFactory2" class="com.study.factory.MyBeanFactory2"></bean>


    <bean id="userDao2" class="com.study.factory.MyBeanFactory1" factory-method="userDao"></bean>

</beans>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
8.2.2.3 修改ApplicationContextDemo03 测试类
package com.study.demoTest;

import com.study.dao.UserDao;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ApplicationContextDemo03 {
    public static void main(String[] args) {

        ApplicationContext applicationContext =
                new ClassPathXmlApplicationContext("beans.xml");

        UserDao userDao =(UserDao)applicationContext.getBean("userDao1");

        System.out.println(userDao);

    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

在这里插入图片描述

8.2.3 有参数的静态工厂和非静态工厂的实例化

8.2.3.1,修改 MyBeanFactory1
package com.study.factory;

import com.study.dao.UserDao;
import com.study.dao.impl.UserDaoImpl;

//创建静态工厂
public class MyBeanFactory1 {
    public static UserDao userDao(String username,String age){
        System.out.println("MyBeanFactory1 username :"+ username);
        System.out.println("MyBeanFactory1 age :"+ age);
        //可以进行其他业务逻辑操作
        return new UserDaoImpl();
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
8.2.3.2,修改 MyBeanFactory2
package com.study.factory;

import com.study.dao.UserDao;
import com.study.dao.impl.UserDaoImpl;
//创建实例化工厂
public class MyBeanFactory2 {
    public UserDao userDao(String username,String age){
        System.out.println("MyBeanFactory2 username :"+ username);
        System.out.println("MyBeanFactory2 age :"+ age);
        //可以进行其他业务逻辑操作
        return new UserDaoImpl();
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
8.2.3.3,修改 beans.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="userDao1" factory-bean="myBeanFactory2" factory-method="userDao">
        <constructor-arg name="username" value="李乃龙"/>
        <constructor-arg name="age" value="26"/>
    </bean>
    <bean id="myBeanFactory2" class="com.study.factory.MyBeanFactory2"></bean>


    <bean id="userDao2" class="com.study.factory.MyBeanFactory1"
          factory-method="userDao">
        <constructor-arg name="username" value="李乃龙"/>
        <constructor-arg name="age" value="26"/>
    </bean>

</beans>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
8.2.3.4 修改ApplicationContextDemo03 测试类
package com.study.demoTest;

import com.study.dao.UserDao;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ApplicationContextDemo03 {
    public static void main(String[] args) {

        ApplicationContext applicationContext =
                new ClassPathXmlApplicationContext("beans.xml");
        UserDao userDao2 =(UserDao)applicationContext.getBean("userDao2");
        UserDao userDao1 =(UserDao)applicationContext.getBean("userDao1");
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

8.2.4 FactoryBean创建bean

8.2.4.1 创建 MyBeanFactory3
package com.study.factory;

import com.study.dao.UserDao;
import com.study.dao.impl.UserDaoImpl;
import org.springframework.beans.factory.FactoryBean;

public class MyBeanFactory3 implements FactoryBean<UserDao> {
    public UserDao getObject() throws Exception {
        return new UserDaoImpl();
    }

    public Class<?> getObjectType() {
        return UserDao.class;
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
8.2.4.2 修改 beans.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">

    <!--FactoryBean演示-->
    <bean id="myBeanFactory3" class="com.study.factory.MyBeanFactory3"></bean>

    <bean id="userDao1" factory-bean="myBeanFactory2" factory-method="userDao">
        <constructor-arg name="username" value="李乃龙"/>
        <constructor-arg name="age" value="26"/>
    </bean>
    <bean id="myBeanFactory2" class="com.study.factory.MyBeanFactory2"></bean>


    <bean id="userDao2" class="com.study.factory.MyBeanFactory1"
          factory-method="userDao">
        <constructor-arg name="username" value="李乃龙"/>
        <constructor-arg name="age" value="26"/>
    </bean>

</beans>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
8.2.4.3 修改ApplicationContextDemo03 测试类
package com.study.demoTest;

import com.study.dao.UserDao;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ApplicationContextDemo03 {
    public static void main(String[] args) {

        ApplicationContext applicationContext =
                new ClassPathXmlApplicationContext("beans.xml");
        UserDao userDao2 =(UserDao)applicationContext.getBean("myBeanFactory3");
        System.out.println(userDao2);
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

在这里插入图片描述

在这里插入图片描述

9,Bean的依赖注入配置

在这里插入图片描述

9.1 注入list类型数据

9.1.1,修改 UserService

package com.study.service;

public interface UserService {
    public void show();

    public void showUserDaoList();
}


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

9.1.2,修改 UserServiceImpl

package com.study.service.impl;

import com.study.dao.UserDao;
import com.study.service.UserService;

import java.util.List;

public class UserServiceImpl implements UserService {
    //注入list
    private List<String> stringList;

    private List<UserDao> userDaoList;

    public UserServiceImpl() {
        System.out.println("无参构造方法执行");
    }

    public UserServiceImpl(String username,String age) {
        System.out.println("有参构造方法执行 username :"+ username);
        System.out.println("有参构造方法执行 age :"+ age);
    }

    public void setStringList(List<String> stringList) {
        this.stringList = stringList;
    }

    public void setUserDao(UserDao userDao){
        System.out.println("beanfactory调用该方法获取到userDao:"+userDao);
    }

    public void show(){
        System.out.println(stringList.toString());
    }

    public void showUserDaoList() {
        System.out.println(userDaoList.toString());
    }

    public void setUserDaoList(List<UserDao> userDaoList) {
        this.userDaoList = userDaoList;
    }
}


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44

9.1.3,修改beans.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">

    <!--注入list演示-->
    <bean id="userService" class="com.study.service.impl.UserServiceImpl">
        <property name="stringList">
            <list>
                <value>aaa</value>
                <value>bbb</value>
                <value>ccc</value>
            </list>
        </property>
        <property name="userDaoList">
            <list>
                <ref bean="userDao1"></ref>
                <ref bean="userDao1"></ref>
                <ref bean="userDao1"></ref>
            </list>
        </property>
    </bean>


    <bean id="myBeanFactory3" class="com.study.factory.MyBeanFactory3"></bean>

    <bean id="userDao1" factory-bean="myBeanFactory2" factory-method="userDao">
        <constructor-arg name="username" value="李乃龙"/>
        <constructor-arg name="age" value="26"/>
    </bean>
    <bean id="myBeanFactory2" class="com.study.factory.MyBeanFactory2"></bean>


    <bean id="userDao2" class="com.study.factory.MyBeanFactory1"
          factory-method="userDao">
        <constructor-arg name="username" value="李乃龙"/>
        <constructor-arg name="age" value="26"/>
    </bean>

</beans>


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42

9.1.4,新建ApplicationContextDemo04 测试类

package com.study.demoTest;

import com.study.dao.UserDao;
import com.study.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ApplicationContextDemo04 {
    public static void main(String[] args) {

        ApplicationContext applicationContext =
                new ClassPathXmlApplicationContext("beans.xml");
        UserService userService =(UserService)applicationContext.getBean("userService");
        userService.show();
        userService.showUserDaoList();
    }
}


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

9.2 注入set类型数据

9.2.1,修改 UserService

package com.study.service;

public interface UserService {
    public void show();

    public void showUserDaoList();

    public void showSet();
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

9.2.2,修改 UserServiceImpl

package com.study.service.impl;

import com.study.dao.UserDao;
import com.study.service.UserService;

import java.util.List;
import java.util.Set;

public class UserServiceImpl implements UserService {
    //注入list
    private List<String> stringList;

    private List<UserDao> userDaoList;

    private Set<String> strSet;

    private Set<UserDao> UserDaoSet;

    public UserServiceImpl() {
        System.out.println("无参构造方法执行");
    }

    public UserServiceImpl(String username,String age) {
        System.out.println("有参构造方法执行 username :"+ username);
        System.out.println("有参构造方法执行 age :"+ age);
    }

    public void setStringList(List<String> stringList) {
        this.stringList = stringList;
    }

    public void setUserDao(UserDao userDao){
        System.out.println("beanfactory调用该方法获取到userDao:"+userDao);
    }

    public void show(){
        System.out.println(stringList.toString());
    }

    public void showUserDaoList() {
        System.out.println(userDaoList.toString());
    }

    public void showSet() {
        System.out.println(strSet.toString());
        System.out.println(UserDaoSet.toString());
    }

    public void setUserDaoList(List<UserDao> userDaoList) {
        this.userDaoList = userDaoList;
    }

    public void setStrSet(Set<String> strSet) {
        this.strSet = strSet;
    }

    public void setUserDaoSet(Set<UserDao> userDaoSet) {
        UserDaoSet = userDaoSet;
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61

9.2.3,修改 beans.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">

    <!--注入list演示-->
    <bean id="userService" class="com.study.service.impl.UserServiceImpl">
        <property name="stringList">
            <list>
                <value>aaa</value>
                <value>bbb</value>
                <value>ccc</value>
            </list>
        </property>
        <property name="userDaoList">
            <list>
                <ref bean="userDao1"></ref>
                <ref bean="userDao1"></ref>
                <ref bean="userDao1"></ref>
            </list>
        </property>
        <property name="strSet">
            <set>
                <value>aaa</value>
                <value>aaa</value>
                <value>bbb</value>
            </set>
        </property>
        <property name="userDaoSet">
            <set>
                <ref bean="userDao1"></ref>
                <ref bean="userDao1"></ref>
                <ref bean="userDao1"></ref>
            </set>
        </property>
    </bean>


    <bean id="myBeanFactory3" class="com.study.factory.MyBeanFactory3"></bean>

    <bean id="userDao1" factory-bean="myBeanFactory2" factory-method="userDao">
        <constructor-arg name="username" value="李乃龙"/>
        <constructor-arg name="age" value="26"/>
    </bean>
    <bean id="myBeanFactory2" class="com.study.factory.MyBeanFactory2"></bean>


    <bean id="userDao2" class="com.study.factory.MyBeanFactory1"
          factory-method="userDao">
        <constructor-arg name="username" value="李乃龙"/>
        <constructor-arg name="age" value="26"/>
    </bean>

</beans>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55

9.2.4,修改 ApplicationContextDemo04 测试类

package com.study.demoTest;

import com.study.dao.UserDao;
import com.study.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ApplicationContextDemo04 {
    public static void main(String[] args) {

        ApplicationContext applicationContext =
                new ClassPathXmlApplicationContext("beans.xml");
        UserService userService =(UserService)applicationContext.getBean("userService");
        userService.show();
        userService.showUserDaoList();
        userService.showSet();
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

9.3 注入map类型数据

9.3.1 修改 UserService

package com.study.service;

public interface UserService {
    public void show();

    public void showUserDaoList();

    public void showSet();

    public void showMap();
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

9.3.2 修改 UserServiceImpl

package com.study.service.impl;

import com.study.dao.UserDao;
import com.study.service.UserService;

import java.util.List;
import java.util.Map;
import java.util.Set;

public class UserServiceImpl implements UserService {
    //注入list
    private List<String> stringList;

    private List<UserDao> userDaoList;

    private Set<String> strSet;

    private Set<UserDao> UserDaoSet;

    private Map<String,String> strMap;
    private Map<String,UserDao> UserDaoMap;

    public UserServiceImpl() {
        System.out.println("无参构造方法执行");
    }

    public UserServiceImpl(String username,String age) {
        System.out.println("有参构造方法执行 username :"+ username);
        System.out.println("有参构造方法执行 age :"+ age);
    }

    public void setStringList(List<String> stringList) {
        this.stringList = stringList;
    }

    public void setUserDao(UserDao userDao){
        System.out.println("beanfactory调用该方法获取到userDao:"+userDao);
    }

    public void show(){
        System.out.println(stringList.toString());
    }

    public void showUserDaoList() {
        System.out.println(userDaoList.toString());
    }

    public void showSet() {
        System.out.println(strSet.toString());
        System.out.println(UserDaoSet.toString());
    }

    public void showMap() {
        System.out.println(strMap.toString());
        System.out.println(UserDaoMap.toString());
    }

    public void setStrMap(Map<String, String> strMap) {
        this.strMap = strMap;
    }

    public void setUserDaoMap(Map<String, UserDao> userDaoMap) {
        UserDaoMap = userDaoMap;
    }

    public void setUserDaoList(List<UserDao> userDaoList) {
        this.userDaoList = userDaoList;
    }

    public void setStrSet(Set<String> strSet) {
        this.strSet = strSet;
    }

    public void setUserDaoSet(Set<UserDao> userDaoSet) {
        UserDaoSet = userDaoSet;
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78

9.3.3 修改 beans.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">

    <!--注入list演示-->
    <bean id="userService" class="com.study.service.impl.UserServiceImpl">
        <property name="stringList">
            <list>
                <value>aaa</value>
                <value>bbb</value>
                <value>ccc</value>
            </list>
        </property>
        <property name="userDaoList">
            <list>
                <ref bean="userDao1"></ref>
                <ref bean="userDao1"></ref>
                <ref bean="userDao1"></ref>
            </list>
        </property>
        <property name="strSet">
            <set>
                <value>aaa</value>
                <value>aaa</value>
                <value>bbb</value>
            </set>
        </property>
        <property name="userDaoSet">
            <set>
                <ref bean="userDao1"></ref>
                <ref bean="userDao1"></ref>
                <ref bean="userDao1"></ref>
            </set>
        </property>

        <property name="strMap">
            <map>
                <entry key="d1" value="userDao01"/>
                <entry key="d1" value="userDao01"/>
                <entry key="d2" value="userDao02"/>
                <entry key="d3" value="userDao03"/>
            </map>
        </property>

        <property name="userDaoMap">
            <map>
                <entry key="d1" value-ref="userDao1"/>
                <entry key="d1" value-ref="userDao1"/>
                <entry key="d2" value-ref="userDao1"/>
                <entry key="d3" value-ref="userDao1"/>
            </map>
        </property>
    </bean>


    <bean id="myBeanFactory3" class="com.study.factory.MyBeanFactory3"></bean>

    <bean id="userDao1" factory-bean="myBeanFactory2" factory-method="userDao">
        <constructor-arg name="username" value="李乃龙"/>
        <constructor-arg name="age" value="26"/>
    </bean>
    <bean id="myBeanFactory2" class="com.study.factory.MyBeanFactory2"></bean>


    <bean id="userDao2" class="com.study.factory.MyBeanFactory1"
          factory-method="userDao">
        <constructor-arg name="username" value="李乃龙"/>
        <constructor-arg name="age" value="26"/>
    </bean>

</beans>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73

9.3.4 修改 ApplicationContextDemo04 测试类

package com.study.demoTest;

import com.study.dao.UserDao;
import com.study.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ApplicationContextDemo04 {
    public static void main(String[] args) {

        ApplicationContext applicationContext =
                new ClassPathXmlApplicationContext("beans.xml");
        UserService userService =(UserService)applicationContext.getBean("userService");

        userService.showMap();
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/309175
推荐阅读
相关标签
  

闽ICP备14008679号