快速入门
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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean id="userService" class="com.service.UserService">
<property name="name">
<value>刘天池</value>
</property>
<!-- 在userService中引用studentService -->
<property name="studentService" ref="studentService"/>
</bean>
<bean id="studentService" class="com.service.StudentService">
<property name="name" value="张昭"/>
</bean>
</beans>
package com.test;
import com.util.*;
import com.service.*;
public class Test {
public static void main(String[] args){
// 传统方法,调用UserService的sayHello方法
UserService userService=new UserService();
userService.setName("鲁肃");
//userService.sayHello();
//spring 完成调用
//1.得到spring接口
((UserService)ApplicationContextUtil.getApplicationContext().getBean("userService")).sayHello();
}
}
package com.service;
public class UserService {
private String name;
private StudentService studentService;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public StudentService getStudentService() {
return studentService;
}
public void setStudentService(StudentService studentService) {
this.studentService = studentService;
}
public void sayHello(){
System.out.println("hello,"+name);
studentService.doHomework();
}
}
package com.service;
public class StudentService {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void doHomework(){
System.out.println("doHomework"+name);
}
}
package com.util;
/*
* ApplicationContext是重量级组件,要做成单态的
*/
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
final public class ApplicationContextUtil {
private static ApplicationContext ac=null;
private ApplicationContextUtil(){
}
static {
ac=new ClassPathXmlApplicationContext("applicationContext.xml");
}
public static ApplicationContext getApplicationContext(){
return ac;
}
}
运行结果: 三月 01, 2020 5:00:10 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@12922f6: display name [org.springframework.context.support.ClassPathXmlApplicationContext@12922f6]; startup date [Sun Mar 01 17:00:10 CST 2020]; root of context hierarchy 三月 01, 2020 5:00:10 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions INFO: Loading XML bean definitions from class path resource [applicationContext.xml] 三月 01, 2020 5:00:10 下午 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory INFO: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@12922f6]: org.springframework.beans.factory.support.DefaultListableBeanFactory@141622d 三月 01, 2020 5:00:10 下午 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@141622d: defining beans [userService,studentService]; root of factory hierarchy hello,刘天池 doHomework张昭
对上面案例总结: spring实际上是一个容器框架,可以配置各种 bean(action/service/domain/dao),并且可以维护bean与bean的关系,当我们 需要使用某个bean的时候,我们可以getBean(id),使用即可
ioc是什么? 答 :ioc(inverse of controll ) 控制反转: 所谓控制反转就是把创建对象 (bean),和维护对象(bean)的关系的权利从程序中转移到spring的容器 (applicationContext.xml),而程序本身不再维护.
DI是什么? 答: di(dependency injection) 依赖注入: 实际上di和ioc是同一个概念, spring设计者认为di更准确表示spring核心技术
☞ 学习框架,最重要的就是学习各个配置.
`