我正在尝试学习 Spring 和 Hibernate,并且我真的很难理解注释及其工作原理。我在互联网上看到的大多数示例都是基于注释的示例,因此我需要先了解注释如何工作,然后才能学习 Spring 或 Hibernate
我知道它们是什么以及它们的用途。我知道它们替换了 xml 配置。 IE。您可以使用注释直接在 Java 代码中配置 bean。我不明白的是如何使用它们以及何时可以使用它们。
试图了解如何做到这一点,我认为如果我看到两者之间的区别将会很有帮助。我这里有一个简单的 Spring 程序。如果我要将此示例程序转换为使用注释,我需要做什么?
我想这样做的原因是因为我在下面提供的程序是我非常理解的程序(我当前正在阅读的 Spring in Action 书中的一个示例)。如果将其转换为注释版本,我将了解如何以及在何处使用注释。
有什么建议么?
提前致谢
工具主义者.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>
器乐界面
package com.sia.ch1.instrumentalist;
public interface Instrument {
void play();
}
器乐执行者
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{
System.out.print("Playing " + song + " : ");
instrument.play();
}
public void setInstrument(Instrument instrument) {
this.instrument = instrument;
}
public void setSong(String song) {
this.song = song;
}
}
乐器 - 钢琴
package com.sia.ch1.instrumentalist;
public class Piano implements Instrument{
public Piano(){}
public void play(){
System.out.println("PLINK PLINK");
}
}
乐器 - 萨克斯管
package com.sia.ch1.instrumentalist;
public class Saxophone implements Instrument{
public Saxophone(){}
public void play(){
System.out.println("TOOT TOOT TOOT");
}
}
主班
package com.sia.ch1.instrumentalist;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import com.sia.ch1.performer.PerformanceException;
import com.sia.ch1.performer.Performer;
public class InstrumentalistApp {
public static void main(String[] args){
ApplicationContext ctx = new FileSystemXmlApplicationContext("c:\\projects\\test\\conf\\instrumentalist.xml");
Performer performer = (Performer) ctx.getBean("kenny");
try {
performer.perform();
} catch (PerformanceException e) {
e.printStackTrace();
}
}
}
例外
package com.sia.ch1.performer;
public class PerformanceException extends Exception {
public PerformanceException() {
super();
// TODO Auto-generated constructor stub
}
public PerformanceException(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}
public PerformanceException(String message) {
super(message);
// TODO Auto-generated constructor stub
}
public PerformanceException(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}
}
Edit 1
为了尝试转换上面的内容,我将通过这两个简单的例子:
Ex1: http://jroller.com/habuma/entry/reducing_xml_with_spring_2 http://jroller.com/habuma/entry/reducing_xml_with_spring_2
Ex2: http://www.theserverside.com/tutorial/Spring-Without-XML-The-Basics-of-Spring-Annotations-vs-Spring-XML-Files http://www.theserverside.com/tutorial/Spring-Without-XML-The-Basics-of-Spring-Annotations-vs-Spring-XML-Files
我有点理解第一个 URL 中的示例,但第二个 URL 让我有点困惑。在第二个 URL 的示例中,其目的是什么SummaryConfig
班级?看起来好像SummaryConfig
class 是 XML 文件的 Java 版本。第一个示例中的示例中没有使用这种方法。两者有什么区别?
当您使用注释时,您是否可以将配置详细信息放在 Java 类中(例如SummaryConfig
)并且您还可以将注释放在 bean 本身中,如第一个 URL 中的示例所示?
Thanks
Edit 2
这是我到目前为止所做的,
我修改了xml文档,删除了配置并启用了组件的自动扫描(注:我更改了修改版本的包名称)
<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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:component-scan base-package="com.sia.ch1.instrumentalist.annotate" />
</beans>
向 Piano 和 Saxophone 类添加了 @Component 注释。我认为这告诉容器这个类应该包含在要自动扫描的类中。正确的?
package com.sia.ch1.instrumentalist.annotate;
import org.springframework.stereotype.Component;
@Component
public class Piano implements Instrument{
public Piano(){}
public void play(){
System.out.println("PLINK PLINK");
}
}
package com.sia.ch1.instrumentalist.annotate;
import org.springframework.stereotype.Component;
@Component
public class Saxophone implements Instrument{
public Saxophone(){}
public void play(){
System.out.println("TOOT TOOT TOOT");
}
}
这就是我陷入困境的地方(乐器演奏家类别)。
这个类需要@Component注解吗?或者仅当该类要从另一个类引用时才需要?
我知道我需要 @Autowire 乐器和歌曲属性,但我怎么知道我是否想按名称或按类型等自动连接
如果在此类中没有代表 String 属性的 bean,我将如何自动装配 String 属性?即乐器属性将引用钢琴类,但是歌曲属性将使用什么自动装配?
package com.sia.ch1.instrumentalist.annotate;
//
import org.springframework.stereotype.Component;
import com.sia.ch1.performer.PerformanceException;
import com.sia.ch1.performer.Performer;
//
@Component
public class Instrumentalist implements Performer{
private Instrument instrument;
private String song;
public Instrumentalist(){}
public void perform() throws PerformanceException{
System.out.print("Playing " + song + " : ");
instrument.play();
}
public void setInstrument(Instrument instrument) {
this.instrument = instrument;
}
public void setSong(String song) {
this.song = song;
}
}
我认为我是对的,任何其他类都不需要注释。
Thanks