Spring-HelloWorld

一、本人的理解

       在开始学习Spring框架之前,我们首先了解的不是他的理论知识,因为如果没有一点对Spring的了解直接看理论是不怎么好看懂的,最好的办法就是先写个简单的使用了Spring框架的程序,然后和未使用Spring框架之前的代码做比较。

       关于Spring框架我们现在只需要知道Spring是一个开源的轻量级的框架,并且主要组成成分是IOC(DI)和AOP容器构成 。

       所谓轻量级不是指jar包有多大而是指Spring是非侵入性的,使用该框架的时候不需要去实现Spring给我们实现的接口或者给我们提供的任何父类,然后享用Spring给我们提供的功能

       IOC(DI)依赖注入:所谓的IOC是指在程序设计中实例不在由调用者创建,而是由Spring容器直接控制;DI其实就是IOC,只不过是从两个不同的角度来描述同一个对象,DI可以这样理解,如果一个对象A需要使用另一个对象B才能实现某个功能,这时就可以说对象A依赖于对象B,而Spring容器创建A对象时,会自动将A对象需要的B对象注入到A对象中,这个过程就是依赖注入

       AOP面向切面编程:通过名称就可以知道主要就是一种思想,以后会讲解道,暂时不给予详细讲解


二、原始的HelloWorld书写

HelloWorld.java代码

public class HelloWorld {
    private String name;
    
    public void setName(String name) {
		this.name = name;
	}
    
    public void method(){
    	System.out.println("名字:" + this.name);
    }
}


Main.java代码


public class Main {
	public static void main(String[] args) {
		//1.创建一个HelloWorld对象
		HelloWorld helloWorld = new HelloWorld();
		//2.给helloWorld对象的属性赋值
		helloWorld.setName("张三");
		//3.调用方法
		helloWorld.method();	
	}
}

三、使用了Spring之后的HelloWorld书写



HelloWorld.java

public class HelloWorld {
    private String name;
    
    public void setName(String name) {
		this.name = name;
	}
    
    public void method(){
    	System.out.println("名字:" + this.name);
    }   
}


Main.java

public class Main {
	public static void main(String[] args) {
		//1.创建Spring的IOC容器 的对象
		ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); 
		//2.从IOC容器中获取Bean实例
		HelloWorld helloWorld = (HelloWorld) ctx.getBean("helloWorld");
		//3.调用方法
		helloWorld.method();	
	}
}


applicationContext.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"
	xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
	
    <bean id="helloWorld" class="com.syh.spring1.HelloWorld">
       <property name="name" value="李四"/>
    </bean>
</beans>

四、对applicationContext.xml的bean进行简介

    <bean></bean>:这个无非就是创建一个bean

    bean 中的id属性就是唯一标识,所有的bean都拥有自己的id,并且他们的值都不一样

    bean中的class属性是为了通过该属性值所对应的类来通过反射来创建对象

    <property />:就是在该bean对应的类中给属性赋值

    property中的name就是指明要给哪些属性赋值。但是有一点重要的是setter方法来实现的,所以属性值对应的应该填写setXxxx()中的Xxxx,即name="Xxxx",并非是属性名称,属性name的值并不和类的属性名称一一对应

    property中的value无非就是通过setter方法来给对象的属性赋值

 下面我property中的name使用实例来解释一下

  

 HelloWorld.java

public class HelloWorld {
    private String name;
    
    public void setName3(String name) {
		this.name = name;
	}
    
    public void method(){
    	System.out.println("名字:" + this.name);
    }   
}


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:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
	
    <bean id="helloWorld" class="com.syh.spring1.HelloWorld">
       <property name="name3" value="李四"/>
    </bean>
</beans>


Main.java不发生变化









评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值