下午好
我为自己设定了学习Java语言的Spring框架的目标。 在研究期间,我决定自行检查Richard Feynman的学习算法:组织简化培训另一个。 总的来说,我开始为春季的初学者录制一系列短期课程。 对于那些想重复的人,我列出了
源代码 。
课程01.了解IoC
因此,让我们将诗人召集在一起,让他们创作并朗读我们的诗歌。
首先,声明生成诗句
src \ main \ java \ spring \ interfaces \ Lyricist.java的接口:package spring.intarfaces; public interface Lyricist { public String Generate(); }
我们在
src \ main \ java \ spring \ impls \ Mayakovsky.java类中实现此接口:
package spring.impls; import spring.intarfaces.Lyricist; public class Mayakovsky implements Lyricist { @Override public String Generate() { return " \r\n" + " \r\n" + " \r\n" + " ?"; } }
我们将写道,我们将在xml配置文件
src \ main \ resources \ ApplicationContext.xml中创建一个特定的Mayakovsky对象:
<?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="LyricistBean1" class="spring.impls.Mayakovsky"/> </beans>
将id LyricistBean1与Mayakovsky类名相关联。 几乎所有东西都准备好了,只有调用类
src \ main \ java \ spring \ main \ Start.java丢失了 :
package spring.main; import org.springframework.context.ApplicationContext; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import spring.intarfaces.Lyricist; public class Start { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml"); Lyricist lyr1 = context.getBean("LyricistBean1", Lyricist.class); System.out.println(lyr1.Generate()); ((ConfigurableApplicationContext) context).close();
在Start类中,我们读取配置文件,创建了一个名为bean LyricistBean1的诗人的实例。 得到了诗人玛雅科夫斯基的副本。 Generate()函数向我们返回了不朽的字符串:
还有你
夜曲
可以
在排水管长笛上?
课程02.选择要通过配置文件创建的实例
假设有一位朋友帮助我们,并提供了Lyricist
src \ main \ java \ spring \ impls \ Poushkin.java接口的另一种实现:
package spring.impls; import spring.intarfaces.Lyricist; public class Poushkin implements Lyricist { @Override public String Generate() { return " :\r\n" + " ,\r\n" + " ,\r\n" + " ."; } }
在ApplicationContext.xml配置文件中,将Mayakovsky更改为Poushkin。
运行程序。 我们
得到了完全不同的结果,
而无需重建 :
我记得美好的时光
你出现在我面前
像转瞬即逝的愿景
像一个纯洁的天才。
课程03.代码中的Bean,不在配置文件中
假设我们不希望将正在创建的诗人类的选择
交到配置文件中
的命运之中。
更改Poushkin.java类
- 添加导入库,导入org.springframework.stereotype.Component;
- 将Component批注(“ LyricistBean1”)放在类声明之前
因此,我们向Spring暗示,我们希望看到第一位诗人而不是其他诗人的形式。
仍然需要稍微修复配置文件ApplicationContext.xml:
1.添加名称空间上下文:xmlns:context =“
www.springframework.org/schema/context ”;
2.删除不必要的行:/>
3.添加命令而不是它来自动将spring.impls目录中所有类的垃圾箱添加到程序中:
<context:component-scan base-package =“ spring.impls” />
结果,ApplicationContext.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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"> <context:component-scan base-package="spring.impls"/> </beans>
我们开始。 我们得到与上一课相同的结果。
课程04.配置类
让我们完全摆脱xml文件。
所有设置也可以在java类中。 有时很方便。
在我们的程序中添加一个特殊的配置类
src \ main \ java \ spring \ main \ LyricistConfiguration.java :
package spring.main; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @ComponentScan(basePackages = { "spring.impls" }) @Configuration public class LyricistConfiguration { }
@ComponentScan显示要在其中查找bean的目录。
@Configuration报告我们的类是特殊的配置。
就是这样,不再需要xml文件。 仍然需要将此消息报告给我们的主要班级:
- 补充必要的导入导入org.springframework.context.annotation.AnnotationConfigApplicationContext;
- 替换上下文创建行
ApplicationContext上下文=新的AnnotationConfigApplicationContext(LyricistConfiguration.class);
因此,主
src \ main \ java \ spring \ main \ Start.java文件应如下所示:
package spring.main; import org.springframework.context.ApplicationContext; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import spring.intarfaces.Lyricist; public class Start { public static void main(String[] args) { ApplicationContext context = new AnnotationConfigApplicationContext(LyricistConfiguration.class); Lyricist lyr1 = context.getBean("LyricistBean1", Lyricist.class); System.out.println(lyr1.Generate()); ((ConfigurableApplicationContext) context).close();
一切开始,仍然产生不朽的普希金线。 一切正常。 但是已经没有xml了。
课05.单例和原型
探索配置文件的功能。 因此,让我们从第二课回到项目。 让我们尝试在Start.java中创建Poushkin类的两个实例,并确保伟大的诗人是相同的:
Lyricist lyr1 = context.getBean("LyricistBean1", Lyricist.class); System.out.println(lyr1.Generate()); System.out.println(lyr1.hashCode()); Lyricist lyr2 = context.getBean("LyricistBean1", Lyricist.class); System.out.println(lyr2.Generate()); System.out.println(lyr2.hashCode());
和经文是相同的,并且哈希码下降。 一切都很好。
将不写
src \ main \ java \ spring \ impls \ SimpleMan.java的诗句添加到我们的项目中:
package spring.impls; import spring.intarfaces.Lyricist; public class SimpleMan implements Lyricist { @Override public String Generate() { return " , "; } }
在配置文件中注册该类(紧接在第一个bin之后)
<bean id="LyricistBean2" class="spring.impls.SimpleMan"/>
我们正在努力创造
Lyricist lyr1 = context.getBean("LyricistBean2", Lyricist.class); System.out.println(lyr1.Generate()); System.out.println(lyr1.hashCode()); Lyricist lyr2 = context.getBean("LyricistBean2", Lyricist.class); System.out.println(lyr2.Generate()); System.out.println(lyr2.hashCode());
市民一直都是一样的。 一团糟...
通过添加魔术字scope =“ prototype”来更改配置文件中Bean的注册。
结果,
src \ main \ resources \ ApplicationContext.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"> <bean id="LyricistBean1" class="spring.impls.Poushkin" scope="singleton"/> <bean id="LyricistBean2" class="spring.impls.SimpleMan" scope="prototype"/> </beans>
现在一切都很好。 市民每次都是不同的。 但是普希金总是一个人。
细心的人注意到,第一个bin中添加了设置范围=“ singleton”,但是默认情况下使用此设置,因此您无法编写它。
今天就这些了。 如果这篇文章经过审核,并且我有时间和精力,那么有关AOP,JDBC,事务,MVC和其他知识的内容将会继续。
此致弗拉基米尔