SpringFramework初使用

这篇博客介绍了如何将Java Maven项目转化为Web Maven项目,包括修改pom.xml配置,创建webapp目录和web.xml。还讲解了如何在不部署Tomcat的情况下,使用Maven插件运行项目。此外,文章详细阐述了Spring Framework的使用,包括注入依赖、创建Spring配置文件,以及Spring容器的概念和Bean的生命周期。最后,讨论了XML命名空间在配置文件中的应用。
摘要由CSDN通过智能技术生成

将普通javaMaven项目变成webMaven项目

1、在pom.xml文件中添加,将项目打包成war(java项目打包为jar)

<packaging>war</packaging>

2、接着在main文件夹下添加webapp文件夹,
再在webapp文件夹下添加WEB-INF文件夹,
再在WEB-INF中添加web.xml
webMaven项目的结构

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                               http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         version="2.5">
        <welcome-file-list>
            <welcome-file>index.html</welcome-file>
        </welcome-file-list>
</web-app>

在不部署tomcat的情况下,使用插件运行项目

有关build表标签的简述:

  1. 一种被称为Project Build(全局配置),即是的直接子元素。
  2. 另一种被称为Profile Build(配置),即是的直接子元素。

有关build标签详情请看https://www.cnblogs.com/yinziqiang0909/p/11724266.html

在此我只用到了plugins配置

  • groupId:是项目组织唯一的标识符,实际对应JAVA的包的结构,是main目录里java的目录结构
  • artifactId:就是项目的唯一的标识符,实际对应项目的名称,就是项目根目录的名称
  • version:版本号
  • configuration:该插件所需要的特殊配置,在父子项目之间可以覆盖或合并

在pom.xml的project标签下写入以下代码

<!-- 插件 -->
    <build>
        <plugins>
            <!-- define the project compile level -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>

            <!-- 添加tomcat插件 -->
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <path>/</path>
                    <port>8081</port>
                </configuration>
            </plugin>
        </plugins>
    </build>

1、接着打开Maven窗口
打开Maven窗口
2、运行项目(第一次运行,程序会自行下载,接着再运行一次即可)
运行项目
3、运行结果:
运行结果图

使用StrpingFramework

1、注入依赖

<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.1.5.RELEASE</version>
        </dependency>

注入SpringFramework
2、点击resources,右键new—>XML Configuration File—>Spring Config
在这里插入图片描述
3、我们先创建一个Person类,在构造方法中各打印一句话

public class Person {
    private int id;
    private String name;
    private int age;
    private boolean sex;

    public Person(){
		System.out.println("无参构造");
    }

    public Person(int id, String name, int age, boolean sex) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.sex = sex;
        System.out.println("有参构造");
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public boolean isSex() {
        return sex;
    }

    public void setSex(boolean sex) {
        this.sex = sex;
    }

    @Override
    public String toString() {
        return "Person{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", sex=" + (sex ?"boy" : "girl") +
                '}';
    }

简单的进行配置bean对象

<?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="person1" class="com.qfedu.bean.Person">
        <property name="id" value="1"/>
        <property name="name" value="张爱国"/>
        <property name="age" value="18"/>
        <property name="sex" value="true"/>
    </bean>
</beans>

4、再创建一个PersonDemo测试类

简单介绍一下Spring容器——Spring容器是生成Bean实例的工厂,并管理容器中的Bean

有关Spring容器详情请看https://www.jianshu.com/p/2854d8984dfc

BeanFactory和ApplicationContext是Spring的两大核心接口,而其中ApplicationContext是BeanFactory的子接口

  • ApplicationContext(应用上下文)常用实现类
  1. FileSystemXmlApplicationContext——从文件系统下的一个或多个xml配置文件中加载上下文定义,也就是说系统盘符中加载xml配置文件。
  2. ClassPathXmlApplicationContext——从类路径下的一个或多个xml配置文件中加载上下文定义,适用于xml配置的方式

测试代码:

这里我们使用ClassPathXmlApplicationContext类

public class PersonDemo {
    @Test
    public void testPerson(){
        ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
        Person p1 = (Person) ac.getBean("person1");
        System.out.println(p1);
    }
}

运行结果:
调用了无参构造
从结果可以看出调用了无参构造,并通过get、set方法赋值,而不是调用有参构造

我们再看看使用有参构造的方法

在beans.xml中添加一下代码,我们使用了constructor-arg标签

<bean id="person2" class="com.qfedu.bean.Person">
        <constructor-arg name="id" value="1" />
        <constructor-arg name="name" value="张爱国" />
        <constructor-arg name="age" value="18" />
        <constructor-arg name="sex" value="true" />
    </bean>

测试代码:

    @Test
    public void testPerson(){
        ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
        Person p1 = (Person) ac.getBean("person1");
        System.out.println(p1);

        Person p2 = (Person) ac.getBean("person2");
        System.out.println(p2);
    }

运行结果:
调用了有参构造
我们再来看看下面的代码:

    @Test
    public void testPerson(){
        ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
        Person p1 = (Person) ac.getBean("person1");
        Person p2 = (Person) ac.getBean("person1");
        System.out.println(p1 == p2);

        Person p3 = (Person) ac.getBean("person2");
        Person p4 = (Person) ac.getBean("person2");
        System.out.println(p3 == p4);

        System.out.println(p1 == p3);
    }

运行结果:
默认是单例模式
我们可以看出,p1 和 p2 、p3 和 p4 是同一个对象,这说明bean对象默认是单例模式。
我们可以通过添加scope属性为prototype,将bean对象设置为多实例对象

<bean id="person1" class="com.qfedu.bean.Person" scope="prototype">
        <property name="id" value="1"/>
        <property name="name" value="张爱国"/>
        <property name="age" value="18"/>
        <property name="sex" value="true"/>
    </bean>

我们甚至可以混合使用

在Person类中添加一个构造方法

public Person(int id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

在beans.xml中添加

<bean id="person3" class="com.qfedu.bean.Person">
        <constructor-arg name="id" value="1" />
        <constructor-arg name="name" value="张爱国" />
        <constructor-arg name="age" value="18" />
        <property name="sex" value="false"/>
    </bean>

测试代码:

@Test
    public void testPerson(){
        ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
        Person p3 = (Person) ac.getBean("person3");
        System.out.println(p3);
    }

运行结果:
混合使用
如果对象的属性中包含对象呢?

创建一个Address类

public class Address {
    private int id;
    private String province;
    private String city;

    public Address(){
        
    }
    
    public Address(int id, String province, String city) {
        this.id = id;
        this.province = province;
        this.city = city;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getProvince() {
        return province;
    }

    public void setProvince(String province) {
        this.province = province;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    @Override
    public String toString() {
        return "Address{" +
                "id=" + id +
                ", province='" + province + '\'' +
                ", city='" + city + '\'' +
                '}';
    }
}

在Person类中添加Address属性 和 一个构造方法 以及set、get 方法

private Address address;

public Person(int id, String name, int age, boolean sex, Address address) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.sex = sex;
        this.address = address;
    }

在beans.xml中的代码,使用ref调用address1的引用

<bean id="address1" class="com.qfedu.bean.Address">
        <property name="id" value="1"/>
        <property name="province" value="湖北"/>
        <property name="city" value="武汉"/>
    </bean>
    
    <bean id="person4" class="com.qfedu.bean.Person">
        <property name="id" value="2"/>
        <property name="name" value="张爱岗"/>
        <property name="age" value="20"/>
        <property name="sex" value="true"/>
        <property name="address" ref="address1"/>
    </bean>

运行结果:
对象的引用

命名空间的使用

命名空间的作用:XML 命名空间提供避免元素命名冲突的方法。

  1. 使用前缀来避免命名冲突
    在头文件中添加xmlns:p="http://www.springframework.org/schema/p"(p是代表namespace,xmlns后的p可以改名)
<bean id="address2" class="com.qfedu.bean.Address" p:id="2" p:province="湖南" p:city="长沙"/>
    <bean id="person5" class="com.qfedu.bean.Person" p:id="3" p:name="李四" p:age="18" p:sex="false" p:address-ref="address2"/>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值