【发布时间】:2012-02-10 09:14:04
【问题描述】:
我正在努力学习 Spring 和 Hibernate,但我真的很难理解 Annotations 以及它们是如何工作的。我在 Internet 上看到的大多数示例都是基于注释的示例,因此我需要先了解注释的工作原理,然后才能学习 Spring 或 Hibernate
我知道它们是什么以及它们的用途。我知道他们替换了 xml 配置。 IE。您可以使用注释直接在 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>
乐器界面
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
}
}
编辑 1
为了尝试转换上述内容,我将通过以下两个简单示例:
Ex1:http://jroller.com/habuma/entry/reducing_xml_with_spring_2
我有点理解第一个 URL 中的示例,但第二个 URL 让我有点困惑。在第二个 URL 的示例中,SummaryConfig 类的用途是什么?看起来SummaryConfig 类是 XML 文件的 Java 版本。在第一个示例中的示例中未使用此方法。两者有什么区别?
难道当您使用注解时,您可以将配置详细信息放在 Java 类中(例如 SummaryConfig),并且您还可以将注解放在 bean 本身中,就像第一个 URL 中的示例一样?
谢谢
编辑 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 乐器和歌曲属性,但我怎么知道我是否要自动连接 byname 或 bytype 等
如果在此类中没有代表它的 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;
}
}
我认为我是对的,因为其他任何类都不需要注释。
谢谢
【问题讨论】:
-
至少尝试自己做而不是问。这就是你将如何学习。它会迫使你阅读文档。您将需要 Component、Autowired 和 Qualifier。
-
感谢@JB Nizet。我正在浏览一些文档,但我正在努力解决它。你能推荐一些好的教程吗?我先看看你提到的那三个注释。
-
我实际上已经完成了另一个注释教程并运行了程序。我想了解的是哪个注释替换了 XML 配置的哪个位。使用上面的示例会更容易,因为我了解配置。
-
Spring 参考文档是了解 Spring 的最佳场所。组件将一个类定义为一个 bean,并允许给它一个名字。 Autowired 告诉 Spring 必须调用某些字段或构造函数或设置器来注入 bean。限定符允许指定要注入的 bean 的名称,以防您有多个相同类型的 bean。
标签: java spring oop dependency-injection annotations