2020-04-02 Spring学习------实例化Bean的三种方法

一.配置文件
1.applicationContext.xml

<?xml version="1.0" encoding="utf-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd"
       xmlns:c="http://www.springframework.org/schema/c"
       xmlns:p="http://www.springframework.org/schema/p">
    <!--三种实例化bean的方式-->

    <!--1. 使用类构造器实例化(默认无参数)-->
    <bean id="mathMethods" class="test.spring.Service.Impl.MathMethodImpl"></bean>

    <!--2. 使用静态工厂方法实例化(简单工厂模式):节省内存消耗-->
    <bean id="person" class="test.spring.Service.Impl.PersonServiceFactory" factory-method="createPerson"></bean>

    <!--3. 使用实例工厂方法实例化(工厂方法模式)-->
    <bean id="personFactory" class="test.spring.Service.Impl.PersonServiceFactory" scope="prototype"></bean>
    <bean id="per" factory-bean="personFactory" factory-method="newPerson"></bean>

</beans>


2.web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

    <!-- 初始化spring容器,上下文的位置 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
</web-app>


二、Bean
1. 使用类构造器实例化(默认无参数)-MathMethodImpl

package test.spring.Service.Impl;

import test.spring.Service.MathMethodService;

public class MathMethodImpl implements MathMethodService {
    public int getCalc(int a, int b) {
        return a+b;
    }
}


 

2.PersonImpl 

package test.spring.Service.Impl;

public class PersonImpl {
    public void sayHi(){
        System.out.println("Hi");
    }
}


3. 使用静态工厂方法实例化(简单工厂模式)-PersonServiceFactory
 

package test.spring.Service.Impl;
public class PersonServiceFactory {
    public static PersonImpl createPerson(){
        return new PersonImpl();
    }
}

4.使用实例工厂方法实例化(工厂方法模式)-PersonServiceFactory

package test.spring.Service.Impl;
public class PersonServiceFactory {
    public PersonImpl newPerson() {
        return new PersonImpl();
    }
}


三、测试

public class MathMethodController {
    public static void main(String[] args){
        //法一:普通实现方法
        MathMethodService math = new MathMethodImpl();
        System.out.println(math.getCalc(2,3));

        //法二:spring实现方法(实例化bean三种方式):区别在xml中。
        //1.类构造器实例化,无参
            /* 第一步:初始化spring容器*/
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
            /*第二步:从容器中获取service对象*/
        MathMethodService methodService = (MathMethodService)applicationContext.getBean("mathMethods");
            /*第三步:使用对象的方法*/
        System.out.println(methodService.getCalc(1,2));

        //2.静态工厂方法实例化
        PersonImpl person = (PersonImpl)applicationContext.getBean("person");
        person.sayHi();

        //3.使用实例工厂方法实例化
        PersonImpl per = (PersonImpl)applicationContext.getBean("per");
        per.sayHi();
    }
}


【负荷预测】基于VMD-CNN-LSTM的负荷预测研究(Python代码实现)内容概要:本文介绍了基于变分模态分解(VMD)、卷积神经网络(CNN)和长短期记忆网络(LSTM)相结合的VMD-CNN-LSTM模型在负荷预测中的研究与应用,采用Python代码实现。该方法首先利用VMD对原始负荷数据进行分解,降低序列复杂性并提取不同频率的模态分量;随后通过CNN提取各模态的局部特征;最后由LSTM捕捉时间序列的长期依赖关系,实现高精度的负荷预测。该模型有效提升了预测精度,尤其适用于非平稳、非线性的电力负荷数据,具有较强的鲁棒性和泛化能力。; 适合人群:具备一定Python编程基础和深度学习背景,从事电力系统、能源管理或时间序列预测相关研究的科研人员及工程技术人员,尤其适合研究生、高校教师及电力行业从业者。; 使用场景及目标:①应用于日前、日内及实时负荷预测场景,支持智慧电网调度与能源优化管理;②为研究复合型深度学习模型在非线性时间序列预测中的设计与实现提供参考;③可用于学术复现、课题研究或实际项目开发中提升预测性能。; 阅读建议:建议读者结合提供的Python代码,深入理解VMD信号分解机制、CNN特征提取原理及LSTM时序建模过程,通过实验调试参数(如VMD的分解层数K、惩罚因子α等)优化模型性能,并可进一步拓展至风电、光伏等其他能源预测领域。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值