Spring IOC的基本使用

一、Spring

1、使用手动加载jar包方式

  1. 导包
commons-logging-1.2.jar
spring-beans-5.2.3.RELEASE.jar
spring-context-5.2.3.RELEASE.jar
spring-core-5.2.3.RELEASE.jar
spring-expression-5.2.3.RELEASE.jar
  1. 写配置

Person.java

package com.lw.bean;

public class Person {
   
    private int id;
    private String name;
    private int age;
    private String gender;

    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 String getGender() {
   
        return gender;
    }

    public void setGender(String gender) {
   
        this.gender = gender;
    }

    @Override
    public String toString() {
   
        return "Person{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", gender='" + gender + '\'' +
                '}';
    }
}

ioc.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">

    <!--注册一个对象,spring回自动创建这个对象-->
    <!--
    一个bean标签就表示一个对象
    id:这个对象的唯一标识
    class:注册对象的完全限定名
    -->
    <bean id="person" class="com.mashibing.bean.Person">
        <!--使用property标签给对象的属性赋值
        name:表示属性的名称
        value:表示属性的值
        -->
        <property name="id" value="1"></property>
        <property name="name" value="zhangsan"></property>
        <property name="age" value="18"></property>
        <property name="gender" value=""></property>
    </bean>
</beans>
  1. 测试

SpringDemoTest.java

package com.lw.test;

import com.mashibing.bean.Person;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringDemoTest {
   
    public static void main(String[] args) {
   
        //ApplicationContext:表示ioc容器
        //ClassPathXmlApplicationContext:表示从当前classpath路径中获取xml文件的配置
        //根据spring的配置文件来获取ioc容器对象
        ApplicationContext context = new ClassPathXmlApplicationContext("ioc.xml");
        Person person = (Person) context.getBean("person");
        System.out.println(person);
    }
}

2、使用Maven的方式构建项目

  1. 创建Maven项目,定义项目的groupId、artifactId
  2. 添加对应的pom依赖

pom.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>com.lw</groupId>
    <artifactId>spring_demo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.3.RELEASE</version>
        </dependency>
    </dependencies>
</project>
  1. 测试

MyTest.java

import com.lw.bean.Person;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
   
    public static void main(String[] args) {
   
        ApplicationContext context = new ClassPathXmlApplicationContext("ioc.xml");
        Person person = (Person) context.getBean("person");
        System.out.println(person);
    }
}

总结:
以上两种方式创建spring的项目都是可以的,但是在现在的企业开发环境中使用更多的还是maven这样的方式,无须自己处理jar之间的依赖关系,也无须提前下载jar包,只需要配置相关的pom即可,因此推荐大家使用maven的方式,具体的maven操作大家可以看maven的详细操作文档。

搭建spring项目需要注意的点:

​ 1、一定要将配置文件添加到类路径中,使用idea创建项目的时候要放在resource目录下

​ 2、导包的时候别忘了commons-logging-1.2.jar包

细节点:

​ 1、ApplicationContext就是IOC容器的接口,可以通过此对象获取容器中创建的对象

​ 2、对象在Spring容器创建完成的时候就已经创建完成,不是需要用的时候才创建

​ 3、对象在IOC容器中存储的时候都是单例的,如果需要多例需要修改属性

​ 4、创建对象给属性赋值的时候是通过setter方法实现的

​ 5、对象的属性是由setter/getter方法决定的,而不是定义的成员属性

二、Spring对象的获取与属性赋值

1、通过bean的id获取IOC容器中的对象

上面已经用过

2、通过bean的类型获取对象

MyTest.java

import com.lw.bean.Person;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
   
    public static void main(String[] args) {
   
        ApplicationContext context = new ClassPathXmlApplicationContext("ioc.xml");
        Person bean = context.getBean(Person.class);
        System.out.println(bean);
    }
}

注意:通过bean的类型在查找对象的时候,在配置文件中不能存在两个类型一致的bean对象,如果有的话,可以通过如下方法
MyTest.java

import com.lw.bean.Person;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
   
    public static void main(String[] args) {
   
        ApplicationContext context = new ClassPathXmlApplicationContext("ioc.xml");
        Person person = context.getBean("person", Person.class);
        System.out.println(person);
    }
}

3、通过构造器给bean对象赋值

ioc.xml

<!--给person类添加构造方法-->
	<bean id="person2" class="com.lw.bean.Person">
        <constructor-arg name="id" value="1"></constructor-arg>
        <constructor-arg name="name" value="lisi"></constructor-arg>
        <constructor-arg name="age" value="20"></constructor-arg>
        <constructor-arg name="gender" value=""></constructor-arg>
    </bean>

	<!--在使用构造器赋值的时候可以省略name属性,但是此时就要求必须严格按照构造器参数的顺序来填写了-->
	<bean id="person3" class="com.lw.bean.Person">
        <constructor-arg value="1"></constructor-arg>
        <constructor-arg value="lisi"></constructor-arg>
        <constructor-arg value="20"></constructor-arg>
        <constructor-arg value=""></constructor-arg>
    </bean>

	<!--如果想不按照顺序来添加参数值,那么可以搭配index属性来使用-->
    <bean id="person4" class="com.lw.bean.Person">
        <constructor-arg value="lisi" index="1"></constructor-arg>
        <constructor-arg value="1" index="0"></constructor-arg>
        <constructor-arg value="" index="3"></constructor-arg<
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值