spring温习笔记-入门-依赖注入

14 篇文章 0 订阅
3 篇文章 0 订阅

Spring容器最大的作用便是解耦合,所有的控制反转、依赖注入其实是一个意思,创建被调用者的实例由Sping容器来完成,然后注入调用者。


spring架包可以去官方下载,也可以移驾这里:点击下载spring3.0.5的架包


创建helloworld实例


1、创建一个工程,引用spring架包、common-logging.jar

2、创建一个bean.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://www.springframework.org/schema/beans"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
	<!-- 将TestService类部署成Spring容器中的Bean  -->
	<bean id="testService" class="xl.TestService">
		<property name="name" value="world"/>
	</bean>
</beans>

3、创建一个service文件  其实就类似一个JavaBean

package xl;

public class TestService {
	private String name;

	public void setName(String name) {
		this.name = name;
	}
	
	public void call() {
		System.out.println("hello,"+name);
	}
}

4、运行  Hello,World

package xl;

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

public class SpringTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		ApplicationContext appContext=new ClassPathXmlApplicationContext("bean.xml");
		TestService test=appContext.getBean("testService",TestService.class);
		test.call();
		
	}

}

5、输出结果

Hello,World


项目打包下载:点击下载



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值