spring(一).入门基础

提到Spring想必大家一定会知道有IOC(Inverse of control,控制反转)、AOP(Aspect Oriented Programming,面向切面编程 )、DI(Dependency Injectio,依赖注入)、javaBean等概念。笔者在刚接触Spring的时候,虽然有技术大牛导师指导,但是还是不能完全理解这几个概念到底是怎么回事,我们不妨先放下对这几个概念的理解,先跟着笔者向下学习,慢慢的以实例向大家渗透这几个概念到底是怎么回事!

现在我们开始实战:

1.先搭建建一个spring IOC项目,项目名称:springtest

2.引入相关的.jar包,下面是引入Spring5.0.8.release包,这里我们事实上用不到这个包里所有的东西,但是日常中不容易做某个功能到底引入哪些包,为了不引入错包或漏引入包,我们干脆全部引入到我们的项目中来,放在lib文件夹里。

注意:jdk1.7只能和spring4.0以下的包搭配使用,而你要是用Spring4.0以上的包就要用jdk1.8.0乃至以上的版本。

3.接着引入applicationContext.xml文件,放在src目录下

该文件内容如下:

<?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" xmlns:util="http://www.springframework.org/schema/util"  
	xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
		http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">

	
</beans>

4.现在我们来写一个Computer.java类,该类中只要有setXXX()方法就行,该类放的位置我就不出图了,大家可以随意。

package org.cdd;

public class Computer {
	public String brand;  //品牌
	public String color;  //颜色
	public String getBrand() {
		return brand;
	}
	public void setBrand(String brand) {
		this.brand = brand;
	}
	public String getColor() {
		return color;
	}
	public void setColor(String color) {
		this.color = color;
	}
	public String toString() {
		return "Computer [brand=" + brand + ", color=" + color + "]";
	}
}

 

5.现在我们在Spring容器applicationContext.xml(暂时可以这么理解)中把这个类注册为javaBean。

             6.现在我们来测试下,编写一个TestComputer类

package org.cdd;

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

public class TestComputer {

	public static void main(String[] args) {
		String conf = "applicationContext.xml";
		ApplicationContext ac = new ClassPathXmlApplicationContext(conf);  //实例化sring容器
		Computer c = ac.getBean("computer", Computer.class);   //从spring容器中拿到Computer类对象
		System.out.println(c);

	}

}

            7.现在运行这个类,进行测试

注:这里打印出这两个属性信息,是因为我在Compute里重写了toString方法

至此,这个项目就完成了,大家有没有发现什么!

我们Computer类我们没有自己实例化啊,怎么可以直接用?其实整个流程是这样的,

我们刚开始写了个Computer类;

然后在applicationContext.xml文件,即,Spring容器,把这个类注册到这个容器中,这就是一个JavaBean,并且为其属性设置了值;

接下来在TestCompute类中,实例化了这个Spring容器,从这个容器中拿到Computer的类对象,并且输出。

在这个过程中我们没有自己实例化Computer类,是Spring容器帮我们实例化了,这个实例化的举动由我们程序员交给了Spring容器来实现,这就是控制反转,IOC。

 

上面是基于xml配置把类注册到Spring容器中的,接下来我们看看怎么通过注解把类注册到Spring容器中

1.我们再写一个Computer2类,给该类打上注解@Component(“c2),并且设置其属性

package org.cdd;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component("c2")          //注解组件,其中参数是这个bean的id,如果参数不写,bean的id默认为类名首字母小写
@Scope("singleton")  //单例,类对象只有一个
public class Computer2 {
	@Value("apple")    //给该属性赋值为apple
	public String brand;  //品牌
	
	@Value("red")   //给该属性赋值为red
	public String color;  //颜色
	public String getBrand() {
		return brand;
	}
	public void setBrand(String brand) {
		this.brand = brand;
	}
	public String getColor() {
		return color;
	}
	public void setColor(String color) {
		this.color = color;
	}
	public String toString() {
		return "Computer [brand=" + brand + ", color=" + color + "]";
	}
}

2.然后在applicationContext.xml文中把打上@Component注解的类扫描到,并且注册到Spring容器中

3.接下来用TestComputer类进行测试

4.来看输出结果

大家有没有发现基于注解比基于xml文件的方式注册javaBean要简单多了,的确是这样的,大家快动手练下吧!

 

这只是菜鸟笔者的个人理解,不到之处欢迎大佬指正,有什么问题也可以给我留言!

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值