idea中Spring项目创建以及实现一个小的IoC案例

1、 创建Spring项目

     我们在idea中创建一个Spring项目,具体如下
      勾选Spring以及Web Application

选择项目路径以及项目名(自动下载所需jar包)

创建配置文件

2、 简单的IoC案例

我们在src目录下新建com.test1包,并创建一个IntroduceDemo类,实现一个简单的自我介绍功能,代码如下:

package com.test1;

public class IntrduceDemo {
    //姓名
    private String name;
    //年龄
    private  int  age;

    public int getAge() {
        return age;
    }

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

    public String getName() {
        return name;
    }

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

    /**
     * 自我介绍
     */
    public void intrduce(){
        System.out.println("您好,我叫"+this.name+"今年"+this.age+"岁!");
    }

}

    这是类中有两个私有属性,分别是name和age,还有一个自我介绍的方法intrduce;

    接下来我们配置Bean.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理解为我们的javaBean,其中两个property标签即表示给IntrduceDemo类中的name和age属性赋值!-->
    <bean id="IntrduceDemo" class="com.test1.IntrduceDemo">
       <property name="name" value="李佳奇"/>
       <property name="age" value="2"/>
    </bean>
</beans>
这里我们再新建一个测试类testMain
package com.test1;

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

public class testMain {
    public static void main(String[] args) {
        //创建Spring上下文(加载bean.xml)
        ApplicationContext acx= new ClassPathXmlApplicationContext("bean.xml");
        //获取HelloWorld实例
        IntrduceDemo id=acx.getBean("IntrduceDemo",IntrduceDemo.class);
        //调用方法
        id.intrduce();
    }
}
运行后得到结果:

3、 Bean的其他属性

<bean id="IntrduceDemo" class="com.test1.IntrduceDemo"></bean>
上面的bean标签除了id和class两个属性外,还有其他属性,这里我们介绍scope、init_method和destroy-method

1scope
bean id=" IntrduceDemo " class=" com.test1.IntrduceDemo " scope="singleton"></bean>
      当我么设置scope属性为“singleton”时,当我们每次通过Spring容器的getBean方法获取IntrduceDemo实例时,得到的都是相同的一个实例,我们这里将示例中的bean.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理解为我们的javaBean,其中两个property标签即表示给IntrduceDemo类中的name和age属性赋值!-->
    <bean id="IntrduceDemo" class="com.test1.IntrduceDemo" scope="singleton">

    </bean>
</beans> 

接着在测试类testMain.java中做如下修改:

package com.test1;

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

public class testMain {
    public static void main(String[] args) {
        //创建Spring上下文(加载bean.xml)
        ApplicationContext acx= new ClassPathXmlApplicationContext("bean.xml");
        //获取HelloWorld实例并赋值
        IntrduceDemo id=acx.getBean("IntrduceDemo",IntrduceDemo.class);
        id.setName("李佳奇");
        id.setAge(2);
        //再获取一个HelloWorld实例,不赋值
        IntrduceDemo idNew=acx.getBean("IntrduceDemo",IntrduceDemo.class);
        //调用方法
        id.intrduce();
        idNew.intrduce();
    }
}
结果如下:

我们并没有为第二个对象idNew赋值,但是它的属性却是有值的,这是因为我们在bean.xml中bean标签中的scope属性设置为singleton,即单例模式,所以在id为其属性赋值后,后面每次新实例化的对象都是相同的。

与singleton对应的是prototype,如果将scope属性设置为prototype,再运行程序,我们将得到如下结果:


2init-methoddestroy-method
    这两个属性分别定义bean初始化和销毁时自动调用的方法,比如我们在 IntrduceDemo 如下修改:

package com.test1;

public class IntrduceDemo {
    //姓名
    private String name;
    //年龄
    private  int  age;

    public int getAge() {
        return age;
    }

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

    public String getName() {
        return name;
    }

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

    /**
     * 自我介绍
     */
    public void intrduce(){
        System.out.println("您好,我叫"+this.name+"今年"+this.age+"岁!");
    }

    //bean初始化时调用的方法
public void init(){
    System.out.println("Bean初始化.....");
}
//bean销毁时调用的方法
public void destroy(){
    System.out.println("Bean销毁.....");
   }
}
这时我们在bean.xml配置bean的init-method和destroy-method属性:

<?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理解为我们的javaBean,其中两个property标签即表示给IntrduceDemo类中的name和age属性赋值!-->
    <bean id="IntrduceDemo" class="com.test1.IntrduceDemo" scope="singleton" init-method="init" destroy-method="destroy">

    </bean>
</beans>

这里需要注意,当我们将bean的scope属性设置为singleton或者默认时,当容器销毁时才会调用destroy-method的方法。

这是我们的测试类:

package com.test1;

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

public class testMain {
    public static void main(String[] args) {
        //创建Spring上下文(加载bean.xml)
        ApplicationContext acx= new ClassPathXmlApplicationContext("bean.xml");
        //获取HelloWorld实例并赋值
        IntrduceDemo id=acx.getBean("IntrduceDemo",IntrduceDemo.class);
        id.setName("李佳奇");
        id.setAge(2);
        //调用方法
        id.intrduce();
        //销毁实例对象
        ((ClassPathXmlApplicationContext) acx).close();
    }
}
运行测试类结果如下:









  • 9
    点赞
  • 34
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值