Spring_IoC学习笔记

来源:互联网 发布:好玩的电脑软件 编辑:程序博客网 时间:2024/05/29 03:57

1. Sppring_HelloWorld

第一步:首先创建一个HelloWorld.java类

package cn.aeths.test;public class HelloWorld {public void say(){System.out.println("Spring Hello");}}

第二步:创建文件夹,拷贝依赖的Spring的jar包,然后配置到构建路径中去

第三步:配置一个beans.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:p="http://www.springframework.org/schema/p"    xmlns:context="http://www.springframework.org/schema/context"    xsi:schemaLocation="        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.xsd">            <!-- 相当于new了这个类的对象,可以通过这个id来取出这个类的对象 --><bean id="HelloWorld" class="cn.aeths.test.HelloWorld"></bean></beans>


第四步: 编写测试的类Test.java类

package cn.aeths.service;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import cn.aeths.test.HelloWorld;public class Test {public static void main(String[] args) {ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");HelloWorld hw = (HelloWorld)ac.getBean("HelloWorld");hw.say();}}


2. Spring_IoC详解

第一步:创建一个测试接口:

package cn.aeths.service;public interface Tester {public void test();}


第二步:分别实现接口

package cn.aeths.service;public class ZhangSan implements Tester{public void test(){System.out.println("张三--测试程序");}}package cn.aeths.service;public class LiSi implements Tester {public void test(){System.out.println("李四--测试程序");}}


第三步:创建一个类去实现测试人员的选择

package cn.aeths.service;public class JavaWork {private Tester tester;public void setTester(Tester tester) {this.tester = tester;}public void doTest(){//以前是这样实现的//ZhangSan zhangsan = new ZhangSan();//zhangsan.test();//LiSi lisi = new LiSi();//lisi.test();tester.test();}}


第四步:编写测试的方法,初步实现解耦

package cn.aeths.test;import cn.aeths.service.JavaWork;import cn.aeths.service.LiSi;import cn.aeths.service.ZhangSan;public class Test {public static void main(String[] args) {//JavaWork javawork = new JavaWork();//javawork.doTest();JavaWork javawork = new JavaWork();javawork.setTester(new ZhangSan());javawork.doTest();javawork.setTester(new LiSi());javawork.doTest(); //在测试的时候选择人}}

第五步:这个工作还可以通过Spring的配置文件去完成

<?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:p="http://www.springframework.org/schema/p"    xmlns:context="http://www.springframework.org/schema/context"    xsi:schemaLocation="        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.xsd">    <!-- 相当于new了这个类的对象,可以通过这个id来取出这个类的对象 --><bean id="HelloWorld" class="cn.aeths.test.HelloWorld"></bean><bean id="zhangsan" class="cn.aeths.service.ZhangSan"></bean><bean id="lisi" class="cn.aeths.service.LiSi"></bean><bean id="javawork" class="cn.aeths.service.JavaWork">    <property name="tester" ref="zhangsan"></property></bean></beans>

第六步:编写一个类,使用Spring去控制测试人员的选择:实现控制的反转

package cn.aeths.test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import cn.aeths.service.JavaWork;public class Test2 {public static void main(String[] args) {ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");JavaWork jw = (JavaWork)ac.getBean("javawork");jw.doTest();}}


0 0