我正在努力学习Spring和Hibernate,我真的很难理解Annotations以及它们是如何工作的.我在互联网上看到的大多数示例都是基于注释的示例,所以我需要先了解注释的工作原理才能学习Spring或Hibernate
我知道它们是什么以及它们用于什么.我知道他们会替换xml配置.即您可以使用注释在Java代码中直接配置bean.我不明白的是如何使用它们以及何时可以使用它们.
试着理解如何做到这一点我认为如果我看到两者之间的差异会有所帮助.我这里有一个简单的Spring程序.如果我要将此示例程序转换为使用注释,我还需要做什么?
我想这样做的原因是因为我在下面提供的程序是我非常理解的程序(我正在阅读的Spring in Action书中的一个例子).如果将其转换为注释版本,我将了解注释的使用方式和位置.
有什么建议?
提前致谢
instrumentalist.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-2.0.xsd">
<bean id="saxophone" class="com.sia.ch1.instrumentalist.Saxophone" />
<bean id="piano" class="com.sia.ch1.instrumentalist.Piano" />
<!-- Injecting into bean properties Ken 1 -->
<bean id="kenny" class="com.sia.ch1.instrumentalist.Instrumentalist">
<property name="song" value="Jingle Bells"/>
<property name="instrument" ref="piano"/>
</bean>
</beans>
Run Code Online (Sandbox Code Playgroud)
乐器界面
package com.sia.ch1.instrumentalist;
public interface Instrument {
void play();
}
Run Code Online (Sandbox Code Playgroud)
乐器演奏者
package com.sia.ch1.instrumentalist;
import com.sia.ch1.performer.PerformanceException;
import com.sia.ch1.performer.Performer;
public class Instrumentalist implements Performer{
private Instrument instrument;
private String song;
public Instrumentalist(){}
public void perform() throws PerformanceException{ …Run Code Online (Sandbox Code Playgroud)