认识spring及环境搭建

一、spring是什么?

      spring是一个开源框架,为简化企业级应用而生,使用spring可以使简单的javabean实现以前只有ejb才能实现的功能

      spring是一个IOC(DI)和AOP容器框架

具体描述spring:

1.轻量级:spring是非侵入性,基于spring开发应用的对象可以不依赖springAPI

2.依赖注入:(dependency inject、IOC)

3.面向切面编程(APO)

4.容器:Spring是一个容器,因为它包含并管理应用对象的生命周期

5.框架:spring实现了使用简单的组件配置组合成一个复杂的应用,在spring中可以使用xml和java注解组合这些对象

6.一站式:在IOC和AOP的基础上可以整合各种企业应用的开源框架和优秀的第三方类库(实际上spring自身也提供了展现层的springmvc和持久层的JDBC)

例子:一个对象,调用时创建对象,并为对象赋值

package com.jff.springdemo;
public class HelloWorld {
	private String name;

	public HelloWorld() {
	}
	public void setName(String name) {
		this.name = name;
	}
	public void hello() {
		System.out.println("hello" + name);
	}
	public static void main(String[] args) {
		//创建一个
		HelloWorld hw=new HelloWorld();
        //为name属性赋值
		hw.setName("feifei");
		//调用hello方法
		hw.hello();
	}
}

二、环境搭建

maven工程下pom.xml引入jar包

<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-context</artifactId>
	<version>5.2.1.RELEASE</version>
</dependency>

spring必须要用的4个包

我们要对上面的例子进行优化,使用spring创建对象并为属性赋值

首先,创建一个资源文件路径,存放spring的xml文件,这里注意选择的是资源文件包,起名为“src/main/resources”

其次,要创建一个springxml,选择file文件,起名为“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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 填写beans -->	
</beans>

 下面我们就用spring改造上面的代码,使用spring创建对象,并为属性赋值

package com.jff.springdemo;
public class Person {
	private String name;
	private int age;
	private Car car;
	public Person() {
		// TODO Auto-generated constructor stub
	}
	public Person(String name, int age, Car car) {
		super();
		this.name = name;
		this.age = age;
		this.car = car;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Car getCar() {
		return car;
	}
	public void setCar(Car car) {
		this.car = car;
	}
	@Override
	public String toString() {
		return "Person [name=" + name + ", age=" + age + ", car=" + car + "]";
	}
}

编写springxml文件

<?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">
    <!-- 配置beans,class通过反射的方式创建实例,要求必须类中有一个无参的构造器 -->
    <!-- 通过set方法进行注入 -->
	<bean id="helloWorld" class="com.jff.springdemo.HelloWorld">
		<property name="name" value="feifei"></property>
	</bean>
</beans>

编写test类

@Test
//属性注入的方法
public void shuxingTest(){
   //1.创建spring ioc的容器对象
    //ApplicationContext是一个接口代表ioc容器
    //ClassPathXmlApplicationContext接口的实现类,该实现类从类路径下来加载配置文件
     ApplicationContext atx=new ClassPathXmlApplicationContext("applicationContext.xml");
    //2.从ioc容器中获取bean实例,使用id获取,getBean必须和xml中的id保持一致
    //利用id定位到容器中的bean非常明确的指定需要的内容
    HelloWorld hw=(HelloWorld) atx.getBean("helloWorld");
    //另种从ioc容器中获取bean实例,使用class获取;利用类型返回ioc中的bean要求bean在ioc容器中是唯一的
    //缺点:就是xml中有两个id都是helloworld类他就不知道具体返回那个了
    HelloWorld hs=atx.getBean(HelloWorld.class);
    hw.hello();
}

步骤:创建spring ioc的容器对象——》从ioc容器中获取bean实例,

说明:从ioc容器中获取bean实例有两种方式

1)第一种:使用id获取,getBean()必须和xml中的id保持一致,利用id定位到容器中的bean非常明确的指定需要的内容

HelloWorld hw=(HelloWorld) atx.getBean("helloWorld");

2)第二种:使用class获取;利用类型返回ioc中的bean要求bean在ioc容器中是唯一的缺点:就是xml中有两个id都是helloworld类他就不知道具体返回那个了

HelloWorld hs=atx.getBean(HelloWorld.class);

运行成功

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值