Spring之XML配置

Spring框架是由于软件开发的复杂性而创建的轻量级控制反转(IoC)和面向切面(AOP)的容器框架。
它使用的是基本的JavaBean来完成以前只可能由EJB完成的事情。然而,其用途不仅仅限于服务器端的开发。从简单性、可测试性和松耦合性角度而言,绝大部分Java应用都可以从spring中受益。

IoC(Inversion of Control)控制反转 DI(Dependency Inject)依赖注入
AOP(Aspect Oriented Programming)面向切面编程
Spring官网
需要的jar包:
spring-core :包含Spring框架基本的核心工具类
spring-beans:包含访问配置文件,创建和管理bean以及进行IoC或者DI相关的所有类
spring-context:Spring核心提供了大量扩展。可以找到使用Spring ApplicationContext特性时所需的全部类,JDNI所需的全部类,UI方面的用来与模板(Templating)引擎如 Velocity、FreeMarker、JasperReports集成的类,以及校验Validation方面的相关类。
spring-expression:定义了Spring的表达式语言
实现方式:
基于XML配置:
传参类:

package com.lengxin1;

import lombok.Data;

@Data
public class Start {
    public int getWoman() {
        return woman;
    }

    public void setWoman(int woman) {
        this.woman = woman;
    }

    public Long getTime() {
        return time;
    }

    public void setTime(Long time) {
        this.time = time;
    }

    public String getMan() {
        return man;
    }

    public void setMan(String man) {
        this.man = man;
    }

    private int woman;
    private Long time;
    private String man;

}

实现类:

package com.lengxin1;

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

public class shixianlei {
    public static void main(String[] args) {
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("Spring-config.xml");//加载配置文件,传配置好的XML文件
        Start start=(Start) applicationContext.getBean("user");//获取XML文件的指定ID
            System.out.println(start);
        }

配置文件:

<?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 id="user" class="com.lengxin1.Start">//指定唯一ID,用于实现类传参,class指定类
    <property name="time" value="10"></property>//使用property标签传参:name和value
    <property name="man" value="15"></property>
    <property name="woman" value="18"></property>
</bean>
</beans>

输出结果:Start(woman=18, time=10, man=15)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值