Spring IOC的初步进阶

来源:互联网 发布:阿里云短信接口 asp 编辑:程序博客网 时间:2024/06/10 00:47

  对于学习spring对我来说实在是失败的次数太多了,今天终于开始向前走了。

  第一个HelloSpring,导包我导的是:projects\spring-build\lib\ivy下的所有包和根目录的dist下的所有包,不管那么多了,先跑再说。

  对了还有一件事,项目右键--buildpath--config--libraries--add libraries--junit 4.

第一阶段:

  第一步:先写接口。

package com.action;public interface HelloApi {void sayHello();}

  第二步:写实现类。

package com.action;public class HelloImpl implements HelloApi{@Overridepublic void sayHello() {System.out.println("Hello World");}}


  第三部:写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"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsd"><!-- id表示你这个组件的名字,class表示组件类 --><bean id="hello" class="com.action.HelloImpl"></bean></beans>

  第四步:写测试类。

package com.action;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class HelloTest {public static void sayHello() {//读取配置文件实例化一个IOC容器ApplicationContext ac = new ClassPathXmlApplicationContext("com/resource/HelloWorld.xml");//从根目录开始的路径//从容器中获取bean,注意此处完全“面向接口” 而不是 “面向实现”HelloApi helloApi = ac.getBean("hello",HelloApi.class);//执行业务逻辑helloApi.sayHello();}public static void main(String[] args) {sayHello();}}

第二阶段:
  第一部分:使用刚才的接口写实现类。

package com.action;public class HelloImpl2 implements HelloApi {private String manager;public HelloImpl2() {this.manager = "Hello Spring wucan";}public HelloImpl2(String manager) {this.manager = manager;}@Overridepublic void sayHello() {System.out.println(manager);}}


  第二部分:配置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"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsd"><!-- 使用无参构造器 --><bean id="bean1" class="com.action.HelloImpl2" ></bean><!-- 使用有参构造器 并且通过value传入参数--><bean id="bean2" class="com.action.HelloImpl2"><constructor-arg index="0" value="Hello Spring" /></bean></beans>


  第三部分:测试类。
package com.action;import org.springframework.beans.factory.BeanFactory;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Test {public static void main(String[] args) {BeanFactory beanFactory = new ClassPathXmlApplicationContext("resource/instantiatingBean.xml");HelloApi bean1 = beanFactory.getBean("bean1", HelloApi.class);bean1.sayHello();HelloApi bean2 = beanFactory.getBean("bean2", HelloApi.class);bean2.sayHello();}}

第三阶段:(使用静态工厂方式实例化bean)

  第一部分:写工厂类。

package com.action;public class HelloApiStaticFactory {//工厂方法public static HelloApi newInstance(String manager){//返回需要的bean实例return new HelloImpl2(manager);}}


  第二部分:写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"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsd"><bean id="bean3" class="com.action.HelloApiStaticFactory" factory-method="newInstance"><constructor-arg index="0" value="Hello Spring new Instance !"></constructor-arg></bean></beans>

  第三部分:测试。

package com.action;import org.springframework.beans.factory.BeanFactory;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Test {public static void main(String[] args) {BeanFactory beanFactory = new ClassPathXmlApplicationContext("resource/instantiatingBean.xml");HelloApi bean3 = beanFactory.getBean("bean3", HelloApi.class);bean3.sayHello();}}


第四阶段:(实例工厂方法实例化Bean
第一部分:写工厂类。

package com.action;public class HelloApiInstanceFactory {public HelloApi newInstance(String massage){return new HelloImpl2(massage);}}


第二部分:配置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"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsd">        <!-- 定义实例工厂Bean -->        <bean id="beanInstanceFactory" class="com.action.HelloApiInstanceFactory"></bean>        <!-- 使用实例工厂Bean创建Bean -->        <bean id="bean4" factory-bean="beanInstanceFactory" factory-method="newInstance">        <constructor-arg index="0" value="Hello Spring of newInstance"></constructor-arg>        </bean></beans>


第三部分:写测试类。

package com.action;import org.springframework.beans.factory.BeanFactory;import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestInstance {public static void main(String[] args) {BeanFactory beanFactory = new ClassPathXmlApplicationContext("com/resource/instantiatingBean.xml");HelloApi bean4 = beanFactory.getBean("bean4", HelloApi.class);bean4.sayHello();}}