Spring IOC的初步进阶

本文详细介绍了使用Spring框架实现从简单的HelloWorld示例到更复杂实例工厂的构建过程,包括接口定义、实现类编写、配置XML文件、测试类实现等多个阶段。

  对于学习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{
	@Override
	public 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/beans
	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
	http://www.springframework.org/schema/context
	http://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;
	}

	@Override
	public 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/beans
	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
	http://www.springframework.org/schema/context
	http://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/beans
	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
	http://www.springframework.org/schema/context
	http://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/beans
	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
	http://www.springframework.org/schema/context
	http://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();
	}
}


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值