0
点赞
收藏
分享

微信扫一扫

spring基础XML装配

青乌 2023-08-21 阅读 47


新建maven项目

spring基础XML装配_java

 导入依赖

<dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.3.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>

 User.java

package com.shrimpking.assemble;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author : Shrimpking
 * @create 2023/7/5 11:19
 */
public class User
{
    private String userName;
    private String password;
    private List<String> list;

    public User(String userName, String password, List<String> list)
    {
        this.userName = userName;
        this.password = password;
        this.list = list;
    }

    public User()
    {
    }

    public String getUserName()
    {
        return userName;
    }

    public void setUserName(String userName)
    {
        this.userName = userName;
    }

    public String getPassword()
    {
        return password;
    }

    public void setPassword(String password)
    {
        this.password = password;
    }

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

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

    @Override
    public String toString()
    {
        return "User{" + "userName='" + userName + '\'' + ", password='" + password + '\'' + ", list=" + list + '}';
    }
}

applicationContext.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="user1" class="com.shrimpking.assemble.User">
        <constructor-arg index="0" value="zhangsan"/>
        <constructor-arg index="1" value="123456"/>
        <constructor-arg index="2">
            <list>
                <value>"constructorValue1"</value>
                <value>"constructorValue2"</value>
            </list>
        </constructor-arg>
    </bean>

    <bean id="user2" class="com.shrimpking.assemble.User">
        <property name="userName" value="lisi"/>
        <property name="password" value="222222"/>
        <property name="list">
            <list>
                <value>"listValue1"</value>
                <value>"listValue2"</value>
            </list>
        </property>
    </bean>

</beans>

XmlTest.java

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author : Shrimpking
 * @create 2023/7/5 11:29
 */
public class XmlTest
{

    @Test
    public void test()
    {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        System.out.println(context.getBean("user1"));
        System.out.println(context.getBean("user2"));
    }
}

spring基础XML装配_spring_02

举报

相关推荐

0 条评论