你不得不知道的Spring基础知识之IOC实例(2)


IOC实例

  1. 在码云上新建一个Springcase
    在这里插入图片描述

  2. 修改默认的.gitignore中的内容

# Compiled class file
*.class

# Log file
*.log

# BlueJ files
*.ctxt

# Mobile Tools for Java (J2ME)
.mtj.tmp/

# Package Files #
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar

# Maven #
target/

# IDEA #
.idea/
*.iml

# Eclipse #
.settings/
.classpath
.project

# MacOS #
.DS_Store
.AppleDouble
.LSOverride

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
  1. 从idea中引入项目
    在这里插入图片描述

  2. 新建一个servicemodel模块(quickStart)
    在这里插入图片描述
    在这里插入图片描述

  3. 删除项目中自动生成的pom.xml,然后根据模板重新新建一个适合Spring的pom.xml

    1. spring-core
    2. spring-context
    3. spring-beans
    4. spring-tx

Springpom.xml模板:

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>servicemodle</artifactId>
    <version>1.0-SNAPSHOT</version>

    <name>servicemodle</name>
    <!-- FIXME change it to the project's website -->
    <url>http://www.example.com</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
        <spring.version>5.2.5.RELEASE</spring.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/log4j/log4j -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>

        <!--spring-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${spring.version}</version>
        </dependency>

    </dependencies>

    <build>
        <resources>
            <resource>
                <filtering>true</filtering>
                <directory>${project.basedir}/src/main/resources</directory>
                <includes>
                    <include>*.properties</include>
                    <include>*.xml</include>
                </includes>
            </resource>
        </resources>
    </build>
</project>
  1. 在main下添加resources资源文件
    在这里插入图片描述

  2. 引入log4j和<build>
    在这里插入图片描述
    在这里插入图片描述

  3. 通过模板创建一个application-context.xml文件 (所有需要在Spring中进行管理的对象都需要在其中注册)

    1. <beans>
    2. <bean>中定义了所有的对象
    3. 例如:<bean id = "myUser" class = "com.lazy.domain.Users"/>
    4. 属性注入:<bean> <property name = "uname" value = "xiaoli" /></bean> name: 指定属性 value: 为属性赋值
    5. ref:对象的实例 … <property name = “ptid” ref = “myPtype”/>
    6. 构造方法注入:<bean> <constructor-agr name = "uname" value = "xiaozhang"/> <constructor-agr name = "upwd" value = "qaz123"/> </bean>

    【博客园】正确给各种类型的属性赋值

Springapplication-context.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">
</beans>
  1. 在包下创建domain,创建Users类

    1. private String uname; private String upwd;
    2. 构造方法、getter and setter
      在这里插入图片描述
  2. 测试类APP

  Users myUser = new Users();
  myUser.setUname("zzxb");
  Sout打印myUser.getUname();

使用Spring 将更改为:

 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("application-context.xml");
 Users myUser = (Users)applicationContext.getBean("myUser");
 myUser.setUname("zzxb");

测试类代码:

package org.example;

import org.example.domain.Users;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("application-context.xml");
        Users myUser = (Users)applicationContext.getBean("myUser");
      /*  myUser.setUname("zzxb");*/
        System.out.println(myUser.getUname());
    }
}

效果图展示

效果图展示

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

TomLazy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值