Spring入门学习手册 1:最简单的反转控制

本文介绍Spring框架中JavaBean的定义与使用,包括XML配置方式、依赖注入及多实例配置,揭示JavaBean初始化时机与作用域特性。

Spring入门学习手册 1:最简单的反转控制

一、什么是Javabean

看到的一个比较专业的解释是:

JavaBean定义了一组规则

JavaBean就是遵循此规则的平常的Java对象

JavaBean是一种特殊的Java类,提供getter 和 setter方法访问它的属性。具体的内容可以查看:

菜鸟教程Javabean

学习JavaBean

JavaBean可以跨平台,可以用于多种情况下。

二、Spring里面怎么用JavaBean

首先定义一个JavaBean:

package com.learn.springDemo;

public class Category {
    private int id;
    private String name;

    public int getId() {
        return this.id;
    }

    public String getName() {
        return this.name;
    }

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

    public void setName(String name) {
        this.name = name;
    }
}
复制代码

然后建立一个xml文件,基于这个文件来配置JavaBean

<?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 name="c" class="com.learn.springDemo.Category">
        <property name="name" value="Eather"></property>
        <property name="id" value="12345"></property>
    </bean>

</beans>
复制代码

可以看到xml中配置了JavaBean的属性。 当然,还有基于注解来配置的方式使用 Java 配置进行 Spring bean 管理

使用时直接从容器中取出来就行:

package com.learn.springTest;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.learn.springDemo.Category;

public class Test {
    public static void main(String args[]) {
        ApplicationContext ac = new ClassPathXmlApplicationContext(new String[] {"applicationContext.xml"});

        Category c =  (Category) ac.getBean("c");

        System.out.println(c.getName() + " " + c.getId());

    }

}
复制代码

运行结果:

Eather 12345
复制代码

三、一些问题

  • 每次取出的JavaBean是一个吗?
package com.learn.springTest;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.learn.springDemo.Category;

public class Test {
    public static void main(String args[]) {
        ApplicationContext ac = new ClassPathXmlApplicationContext(new String[] {"applicationContext.xml"});

        Category c =  (Category) ac.getBean("c");

        System.out.println(c.getName() + " " + c.getId());

        c.setId(10086);
        c.setName("David");

/////////////////////////////////////////////////////
        Category cc = (Category) ac.getBean("c");//再取一个取出来
        System.out.println(cc.getName() + " " + cc.getId());//打印


    }

}
复制代码

根据这段代码的运行结果,我认为应该是同一个,每次取出的或者说引用的都是同一个对象,在程序开始后的某一个时刻初始化好后就一直用的是一个对象。

Eather 12345
David 10086

复制代码
  • JavaBean是什么时候被初始化的?

    看看下面这段代码

    package com.learn.springTest;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import com.learn.springDemo.Category;
    public class Test {
      public static void main(String args[]) {
          ApplicationContext ac = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});
    
          Category c = (Category) ac.getBean("c");
    
          System.out.println(c.getName() + " " + c.getId());
    
          c.setId(10086);
          c.setName("David");
    
          //重新初始化一个ApplicationContext
          ApplicationContext ac2 = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});
          Category cc = (Category) ac2.getBean("c");//再取一个取出来
          System.out.println(cc.getName() + " " + cc.getId());//打印
    
    
          }
      }
    复制代码

    运行结果为:

    Eather 12345
    Eather 12345
    复制代码

    JavaBean是什么时候初始化就很显而易见了。

  • 依赖注入怎么搞(在一个类里面引用另一个类的实例

<?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 name="c" class="com.learn.springDemo.Category">
        <property name="name" value="Eather"></property>
        <property name="id" value="12345"></property>
    </bean>
    <bean name="p" class="com.learn.springDemo.Product">
        <property name="name" value="a product"></property>
        <property name="id" value="1008611"></property>
        <!--这里注入Category实例,已经初始化过的-->
        <property name="category" ref="c"></property>
    </bean>

</beans>
复制代码

Product类的定义是这样的:

package com.learn.springDemo;

public class Product {
    private int id;
    private String name;
    private Category category;

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

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

    public void setCategory(Category category) {
        this.category = category;
    }

    public int getId() {
        return this.id;
    }

    public String getName() {
        return this.name;
    }

    public Category getCategory() {
        return this.category;
    }
}
复制代码

显然,在依赖注入时,xml文档中要用 ref,而非value

此外,依赖注入的必须是实例化好的对象。

  • xml里面可以写多个相同的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 name="c" class="com.learn.springDemo.Category">
        <property name="name" value="Eather"></property>
        <property name="id" value="12345"></property>
    </bean>
    <bean name="ccc" class="com.learn.springDemo.Category">
        <property name="name" value="Tom"></property>
        <property name="id" value="111111"></property>
    </bean>
    <bean name="p" class="com.learn.springDemo.Product">
        <property name="name" value="a product"></property>
        <property name="id" value="1008611"></property>
        <!--这里注入Category实例,已经初始化过的-->
        <property name="category" ref="c"></property>
    </bean>

</beans>
复制代码

非常显然可以,只要name不相同就可以(这个几乎是废话)。

考虑柔性负荷的综合能源系统低碳经济优化调度【考虑碳交易机制】(Matlab代码实现)内容概要:本文围绕“考虑柔性负荷的综合能源系统低碳经济优化调度”展开,重点研究在碳交易机制下如何实现综合能源系统的低碳化与经济性协同优化。通过构建包含风电、光伏、储能、柔性负荷等多种能源形式的系统模型,结合碳交易成本与能源调度成本,提出优化调度策略,以降低碳排放并提升系统运行经济性。文中采用Matlab进行仿真代码实现,验证了所提模型在平衡能源供需、平抑可再生能源波动、引导柔性负荷参与调度等方面的有效性,为低碳能源系统的设计与运行提供了技术支撑。; 适合人群:具备一定电力系统、能源系统背景,熟悉Matlab编程,从事能源优化、低碳调度、综合能源系统等相关领域研究的研究生、科研人员及工程技术人员。; 使用场景及目标:①研究碳交易机制对综合能源系统调度决策的影响;②实现柔性负荷在削峰填谷、促进可再生能源消纳中的作用;③掌握基于Matlab的能源系统建模与优化求解方法;④为实际综合能源项目提供低碳经济调度方案参考。; 阅读建议:建议读者结合Matlab代码深入理解模型构建与求解过程,重点关注目标函数设计、约束条件设置及碳交易成本的量化方式,可进一步扩展至多能互补、需求响应等场景进行二次开发与仿真验证。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值