对于学习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 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 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();
}
}
第一部分:写工厂类。
package com.action;
public class HelloApiInstanceFactory {
public HelloApi newInstance(String massage){
return new HelloImpl2(massage);
}
}
<?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();
}
}
本文详细介绍了使用Spring框架实现从简单的HelloWorld示例到更复杂实例工厂的构建过程,包括接口定义、实现类编写、配置XML文件、测试类实现等多个阶段。
320

被折叠的 条评论
为什么被折叠?



