<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-coreartifactId>
<version>5.0.0.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-beansartifactId>
<version>5.0.0.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>5.0.0.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-context-supportartifactId>
<version>5.0.0.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-expressionartifactId>
<version>5.0.0.RELEASEversion>
dependency>
创建ioc.xml文件
文件模板:
<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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<bean>
bean>
beans>
创建想要被管理的对象
public class HelloController {
private HelloService helloService;//注意这里我们是没有手动实例化的,我将演示利用SpringIoc注入我们的实例化对象
public void dealRequest(String message){
helloService.sayMessage(message);
}
public HelloService getHelloService() {
return helloService;
}
public void setHelloService(HelloService helloService) {
this.helloService = helloService;
}
}
public class HelloService {
private String username;
private String password;
public HellorService(){
this.username="GodSchool";
this.password="GodSchool";
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public void sayHelloWolrd(){
System.out.println("HelloWolrd"+this.username+this.password);
}
public void sayMessage(String message){
System.out.println(message);
}
}
<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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<bean id="HelloController" class="ioc_01.HelloController">bean>
<bean id="HelloService" class="ioc_01.HelloService">bean>
beans>
写程序入口(1)加载配置文件,自动创建对象(2)取出我们的对象
public class test01 {
public static void main(String[] args) {
ApplicationContext ac=new ClassPathXmlApplicationContext("bean-ioc.xml");
HelloController helloController = ac.getBean("HelloController", HelloController.class);
HelloService helloService = ac.getBean("HelloService", HelloService.class);
}
}
效果:
我们实现了xml文件管理对象的创建,是不是很清晰,我们创建了那些对象在xml中一目了然,这样开发的时候就可以有计划性的管理对象了,相信你已经体验到了他的好处!
那么我们继续吧,现在我们只解决了独立的创建两个对象;我们想让一个对象引用另一个对象的方法,如何做到呢,这就叫依赖注入;
改造xml文件
<bean id="HelloController" class="ioc_01.HelloController">
<property name="helloService" ref="HelloService">property>
bean>
<bean id="HelloService" class="ioc_01.HelloService">bean>
测试:
public class test01 {
public static void main(String[] args) {
ApplicationContext ac=new ClassPathXmlApplicationContext("bean-ioc.xml");
HelloController helloController = ac.getBean("HelloController", HelloController.class);
helloController.dealRequest("Hello autowise");
/**
我们拿到了service对象,然后调用service对象的sayMessage方法
* public void dealRequest(String message){
* helloService.sayMessage(message);
* }
*/
}
}
Ps:实际开发过程中,mvc的tomcat整合时,dispathcerServlet就是根据路径找到Controler,其他的都跟tomcat无关,所以这里我们一样只需要拿出创建好的controller,其内部的service已经被注入好了;
至此我们入门就结束了,下一章节开始我们将会xml的所有管理方式罗列出来,以及注解管理的方法;
现在都流行使用注解!!!xml我们可以了解一下,有时候写一些配置的时候还是挺管用!!!
经典总结:利用描述文件,告诉我们的要创建的对象的类在哪里,包名+类名形式;利用描述文件,告诉我们创建的对象的初始化属性是什么,或者依赖的另外一个对象的id是什么,所有对象都被平级创建在一个ioc的大容器中,利用id可以相应取出来;
后续Spring专题:
SpringIOC——XML管理全套
SpringIOC——注解管理全套
SpringIOC——内部工具全套
SpringIOC——核心源码剖析