Spring学习
What
Spring框架,由Rod Johnson开发,是一个非常强大的反转控制(IOC)框架,以帮助分离项目组件之间的依赖关系。
个人理解就是相当一个对象工厂,可以生产对象并放置于容器中,然后我们可以从该容器中获取该对象,然后我们就少了 new 的过程。
Why
显然使用了 Spring 使我们的项目低耦合。
How
先入个门,写个Hello World。(本人用的IDE 是 Intellij IDEA)
前期准备
1.首先,导入 Spring 框架。 右键项目,加入框架支持,勾选 Spring 即可,它会自动下载导入。
2.导入完成后,会自动创建 libs 目录,相关的 jar 包会在里面。
然后开始创建 Spring 容器,名字自定义。(我的是 Bean.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>
根标签为 beans 由此可以看出 里面应该是装载各种 bean 对象的。
我们新建一个 HelloWorld 类:
public class HelloWorld {
private String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
然后在 Spring 容器中添加一个 HelloWorld 的实例:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="helloworld" class="spring.com.helloworld.HelloWorld" >
<property name="message" value="Hello World!"/>
</bean>
</beans>
配置中,id 是指这个对象的 唯一 id,形成映射关系,class 表示它来自哪个类,后面的 property 显然是属性参数,对 message 赋值 “Hello World!”.
创建测试类:
public class HelloWorldTest {
private HelloWorld helloWorld;
@Before
public void init(){
//取到这个容器上下文对象
ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
//然后根据 id 获取实例
helloWorld = (HelloWorld) context.getBean("helloworld");
}
@Test
public void getMessage() {
System.out.println(helloWorld.getMessage());
}
}
执行结果:
Hello World!
刚才的例子中,使用 property 标签来初始化成员变量,前提是要有 setter 方法,才能够这样做。
还有一种是通过构造方法的注入。
public class HelloWorld {
private String message;
public HelloWorld(String message) {
this.message = message;
}
}
Spring 容器配置:
<bean id="helloworld" class="spring.com.helloworld.HelloWorld" >
<constructor-arg name="message" value="Hello World!"/>
</bean>
这样能够达到同样的效果,这里的成员变量是 String 同样的,如果是成员变量是自定义的类对象,也能够自由注入。
如:
public class SpellChecker {
public SpellChecker() {
System.out.println("Inside SpellChecker constructor");
}
public void checkSpelling(){
System.out.println("Inside checkSpelling");
}
}
public class TextEditor {
private SpellChecker spellChecker;
public TextEditor(SpellChecker spellChecker) {
System.out.println("Inside TextEditor constructor." );
this.spellChecker = spellChecker;
}
public void spellCheck(){
spellChecker.checkSpelling();
}
}
然后 Spring 如何构建 TextEditor 的实例呢?:
<bean id="textEditor" class="spring.com.constructorDemo.TextEditor">
<constructor-arg name="spellChecker" ref="spellChecker"/>
</bean>
<bean id="spellChecker" class="spring.com.constructorDemo.SpellChecker"/>
在容器里创建一个 SpellChecker 的实例,然后再引用它。(注意:ref,前面是 value)
测试类:
public class TextEditorTest {
private TextEditor textEditor;
@Before
public void setUp() throws Exception {
ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
textEditor = (TextEditor) context.getBean("textEditor");
}
@Test
public void spellCheck() {
textEditor.spellCheck();
}
}
执行结果:
Inside SpellChecker constructor
Inside TextEditor constructor.
Inside checkSpelling
赞多继续,给予博主一点动力,嘿嘿。。。。