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

1、 创建Spring项目

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

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

创建配置文件

2、 简单的IoC案例

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

  1. package com.test1;
  2. public class IntrduceDemo {
  3. //姓名
  4. private String name;
  5. //年龄
  6. private int age;
  7. public int getAge() {
  8. return age;
  9. }
  10. public void setAge(int age) {
  11. this.age = age;
  12. }
  13. public String getName() {
  14. return name;
  15. }
  16. public void setName(String name) {
  17. this.name = name;
  18. }
  19. /**
  20. * 自我介绍
  21. */
  22. public void intrduce(){
  23. System.out.println( "您好,我叫"+ this.name+ "今年"+ this.age+ "岁!");
  24. }
  25. }

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

    接下来我们配置Bean.xml文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation= "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5. <!--我们可以将这个bean理解为我们的javaBean,其中两个property标签即表示给IntrduceDemo类中的name和age属性赋值!-->
  6. <bean id="IntrduceDemo" class="com.test1.IntrduceDemo">
  7. <property name="name" value="李佳奇"/>
  8. <property name="age" value="2"/>
  9. </bean>
  10. </beans>
这里我们再新建一个测试类testMain
  1. package com.test1;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. public class testMain {
  5. public static void main(String[] args) {
  6. //创建Spring上下文(加载bean.xml)
  7. ApplicationContext acx= new ClassPathXmlApplicationContext( "bean.xml");
  8. //获取HelloWorld实例
  9. IntrduceDemo id=acx.getBean( "IntrduceDemo",IntrduceDemo.class);
  10. //调用方法
  11. id.intrduce();
  12. }
  13. }
运行后得到结果:

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修改
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation= "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5. <!--我们可以将这个bean理解为我们的javaBean,其中两个property标签即表示给IntrduceDemo类中的name和age属性赋值!-->
  6. <bean id="IntrduceDemo" class="com.test1.IntrduceDemo" scope="singleton">
  7. </bean>
  8. </beans> 

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

  1. package com.test1;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. public class testMain {
  5. public static void main(String[] args) {
  6. //创建Spring上下文(加载bean.xml)
  7. ApplicationContext acx= new ClassPathXmlApplicationContext( "bean.xml");
  8. //获取HelloWorld实例并赋值
  9. IntrduceDemo id=acx.getBean( "IntrduceDemo",IntrduceDemo.class);
  10. id.setName( "李佳奇");
  11. id.setAge( 2);
  12. //再获取一个HelloWorld实例,不赋值
  13. IntrduceDemo idNew=acx.getBean( "IntrduceDemo",IntrduceDemo.class);
  14. //调用方法
  15. id.intrduce();
  16. idNew.intrduce();
  17. }
  18. }
结果如下:

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

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


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

  1. package com.test1;
  2. public class IntrduceDemo {
  3. //姓名
  4. private String name;
  5. //年龄
  6. private int age;
  7. public int getAge() {
  8. return age;
  9. }
  10. public void setAge(int age) {
  11. this.age = age;
  12. }
  13. public String getName() {
  14. return name;
  15. }
  16. public void setName(String name) {
  17. this.name = name;
  18. }
  19. /**
  20. * 自我介绍
  21. */
  22. public void intrduce(){
  23. System.out.println( "您好,我叫"+ this.name+ "今年"+ this.age+ "岁!");
  24. }
  25. //bean初始化时调用的方法
  26. public void init(){
  27. System.out.println( "Bean初始化.....");
  28. }
  29. //bean销毁时调用的方法
  30. public void destroy(){
  31. System.out.println( "Bean销毁.....");
  32. }
  33. }
这时我们在bean.xml配置bean的init-method和destroy-method属性:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation= "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5. <!--我们可以将这个bean理解为我们的javaBean,其中两个property标签即表示给IntrduceDemo类中的name和age属性赋值!-->
  6. <bean id="IntrduceDemo" class="com.test1.IntrduceDemo" scope="singleton" init-method="init" destroy-method="destroy">
  7. </bean>
  8. </beans>

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

这是我们的测试类:

  1. package com.test1;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. public class testMain {
  5. public static void main(String[] args) {
  6. //创建Spring上下文(加载bean.xml)
  7. ApplicationContext acx= new ClassPathXmlApplicationContext( "bean.xml");
  8. //获取HelloWorld实例并赋值
  9. IntrduceDemo id=acx.getBean( "IntrduceDemo",IntrduceDemo.class);
  10. id.setName( "李佳奇");
  11. id.setAge( 2);
  12. //调用方法
  13. id.intrduce();
  14. //销毁实例对象
  15. ((ClassPathXmlApplicationContext) acx).close();
  16. }
  17. }
运行测试类结果如下:




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值