spring入门

1.spring下载

https://repo.spring.io/libs-release-local/org/springframework/spring/

2.logging下载

http://commons.apache.org/proper/commons-logging/download_logging.cgi

3.导入jar

spring的核心jar为上图4个,各个jar的作用,待补充 //TODO

4.入门

4.1代码结构

 

4.2代码

实体类:Person.java

package springtest.entity;
import springtest.servcie.StudyService;
public class Person {
	private String name;
	private int age;
	private StudyService studyService;
	/*
	 * springbean:不能缺少set方法
	 */
	public void setStudyService(StudyService studyService) {
		this.studyService = studyService;
	}
	public void setName(String name) {
		this.name = name;
	}
	public void setAge(int age) {
		this.age = age;
	}
	@Override
	public String toString() {
		return "name = " + name + "; age = " + age;
	}
	public void doStudy() {
		studyService.study();
	}
}

接口:StudyService.java

package springtest.servcie;
public interface StudyService {
	public void study();
}

接口实现1:StudyServiceImpl.java

package springtest.servcie;
public class StudyServiceImpl implements StudyService {
	@Override
	public void study() {
		System.out.println("学习.....");
	}
}

接口实现2:StudyServiceImpl2.java

package springtest.servcie;
public class StudyServiceImpl2 implements StudyService {
	@Override
	public void study() {
		System.out.println("努力学习...上清华...");
	}
}

spring配置文件: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 id="person" class="springtest.entity.Person">
		<property name="name" value="张三"></property>
		<property name="age" value="18"></property>
		<!-- ref指定实现类,也可以指定为studyServiceImpl2 -->
		<property name="studyService" ref="studyServiceImpl"></property>
		<!--<property name="studyService" ref="studyServiceImpl2"></property>-->
	</bean>
	<!--  不能配置两个相同的实例
	<bean id="person2" class="springtest.entity.Person"> 
		<property name="name" value="张三"></property>
		<property name="age" value="28"></property>
	</bean>
	-->
	<!--  接口不能作为实例
	<bean id="studyService" class="springtest.servcie.StudyService">
	</bean>
	-->
 	<bean id="studyServiceImpl" class="springtest.servcie.StudyServiceImpl">
	</bean>
	<bean id="studyServiceImpl2" class="springtest.servcie.StudyServiceImpl2"> 
	</bean>
</beans>

测试类:Test1.java

package springtest.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import springtest.entity.Person;
public class Test1 {
	public static void main(String[] args) {
		ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
		Person person = ctx.getBean("person",Person.class);
		System.out.println(person.toString());
		/*
		 * doStudy调用的是接口的方法,并不知道其实现,在spring中配置其实现类,可以获得具体方法内容 
		 * --->【面向接口编程】
		 */
		person.doStudy();
	}
}

4.3运行Test1.java

如果配置文件中studyService指向修改为:

<property name="studyService" ref="studyServiceImpl2">

输出:

5.小结

spring中的bean的属性不能缺少set方法,依靠setter注入配置文件中的实例,如果没有setter,该属性为null。

person.doStudy()调用的是接口的方法,并不知道其实现,在spring中配置其实现类,可以获得具体方法内容(bean的属性为接口StudyService,配置ref指定实现类StudyServiceImpl,也可以其他实现类StudyServiceImpl2) ---->【面向接口编程】。

spring配置文件,接口不能作bean来配置,必须为实例。

spring配置文件,不能配置两个相同的实例。

【未完...】

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值