Spring(二)

项目名:Spring01-Constractor
讲解内容:
    构造器注入法
导入jar包
commons-logging.jar
spring.jar

配置applicationContext.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"
    xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> 
        <!-- bean definitions here -->
        <bean id="cls" class="com.fyl.entitiy.Classes"/>
        <bean id="stu" class="com.fyl.entitiy.Student">
            <constructor-arg type="java.lang.Integer" value="1001"/>
            <constructor-arg type="java.lang.String" value="小白"/>
            <constructor-arg ref="cls"/>
        </bean>
</beans>

调用方法

//1.初始化Spring容器
        ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
        //2.提取bean,根据容器中的id来获取的
        Student stu=(Student)ctx.getBean("stu");
        //3.调方法
        stu.method();
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,774评论 19 139
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 47,309评论 6 342
  • 2、IOC中Bean的配置 1、基于xml文件 2、通过工厂方法1、通过调用静态工厂方法创建bean 配置文件:通...
    yzw12138阅读 250评论 0 0
  • 那一天, 带着憧憬不回头的离开, 坐上那开往未来的列车, 天边无尽的边际线其实是家人无限的牵挂; 那一刻, 心里的...
    惠君阅读 212评论 0 1
  • 我在PMCAFF看到这么一个问题“ 最有效的激励措施有哪些 激励措施现在是个产品都会上,一方面是拉新,另一方面是刺...
    三曰_阅读 1,261评论 1 3

友情链接更多精彩内容