java 初识spring

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

/**
*@author Danbro
*@version 创建时间:2019年6月27日上午9:50:38
*@funcition 
**/
public class Main {
	public static void main(String[] args) {
//		不用spring 
//		创建HelloWorld的对象
		HelloWorld helloWorld = new HelloWorld();
//		为helloWorld的对象赋值
		helloWorld.setName("dan");
		调用hello方法
		helloWorld.hello();
		

	}
}

HelloWorld类

/**
*@author Danbro
*@version 创建时间:2019年6月27日上午9:49:20
*@funcition 
**/
public class HelloWorld {
	private String name;
	public void setName(String name) {
		this.name = name;
	}
	public void hello() {
		System.out.println("HelloWorld " + name );
	}
}

 

之前我们建立对象是通过自己创建对象,再调用对象的方法

现在我们用spring的bean来配置

bean配置文件 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">
	<!--配置bean 
	用反射的方式通过类名找到相应的类
	-->
	<bean id = "helloWorld" class = "HelloWorld"><!-- id是实例名称 class是类的路径-->
	<property name="name" value = "Spring"></property><!-- name是属性名 value是相应属性的值-->
	</bean>

</beans>
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
*@author Danbro
*@version 创建时间:2019年6月27日上午9:50:38
*@funcition 
**/
public class Main {
	public static void main(String[] args) {
		
		//用spring
		//创建spring 的IOC容器对象
		//ApplicationContext代表容器 
		//ClassPathXmlApplicationContext是ApplicationContext接口的实现类 该实现类从类路径来加载配置文件
		ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
//		//从容器对象获取bean实例
		HelloWorld helloWorld = (HelloWorld) ctx.getBean("helloWorld");//用id来定位IOC容器中的bean
		HelloWorld helloWorld2 = ctx.getBean(HelloWorld.class);//利用类型返回IOC容器中的bean,但要求IOC容器必须只能有一个该类型的bean
//		//调用hello方法
		helloWorld.hello();
	}
}

打印出HelloWorld Spring

 

创建容器时发生了什么?

HelloWorld

/**
*@author Danbro
*@version 创建时间:2019年6月27日上午9:49:20
*@funcition 
**/
public class HelloWorld {
	private String name;
	public void setName(String name) {
		System.out.println("set方法");
		this.name = name;
	}
	public void hello() {
		System.out.println("HelloWorld " + name );
	}
	public HelloWorld() {
		System.out.println("构造器");
	}
}
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
*@author Danbro
*@version 创建时间:2019年6月27日上午9:50:38
*@funcition 
**/
public class Main {
	public static void main(String[] args) {
		
		//用spring
		//创建spring 的IOC容器对象
		ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
//		//从容器对象获取bean实例
//		HelloWorld helloWorld = (HelloWorld) ctx.getBean("helloWorld");
//		//调用hello方法
//		helloWorld.hello();
	}
}

这时会发现

构造器
set方法

在创建容器时会自动加载类的构造器和set方法
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值