Java 需要一些帮助来理解Anotations-Spring注释吗
我正在努力学习Spring和Hibernate,我真的很难理解注释以及它们是如何工作的。我在互联网上看到的大多数示例都是基于注释的示例,因此在学习Spring或Hibernate之前,我需要先了解注释是如何工作的 我知道它们是什么,它们的用途是什么。我知道它们取代了xml配置。也就是说,您可以使用注释在Java代码中直接配置bean。我不明白的是如何使用它们以及何时可以使用它们 试图了解如何做到这一点,我认为如果我看到两者之间的区别,这将是有益的。我这里有一个简单的Spring程序。如果我要将这个示例程序转换为使用注释,我需要做什么 我之所以想这样做,是因为我在下面提供的程序是我非常了解的(我目前正在阅读的《Spring in Action》一书中的一个例子)。如果将其转换为注释版本,我将了解如何以及在何处使用注释 有什么建议吗 提前谢谢Java 需要一些帮助来理解Anotations-Spring注释吗,java,spring,oop,dependency-injection,annotations,Java,Spring,Oop,Dependency Injection,Annotations,我正在努力学习Spring和Hibernate,我真的很难理解注释以及它们是如何工作的。我在互联网上看到的大多数示例都是基于注释的示例,因此在学习Spring或Hibernate之前,我需要先了解注释是如何工作的 我知道它们是什么,它们的用途是什么。我知道它们取代了xml配置。也就是说,您可以使用注释在Java代码中直接配置bean。我不明白的是如何使用它们以及何时可以使用它们 试图了解如何做到这一点,我认为如果我看到两者之间的区别,这将是有益的。我这里有一个简单的Spring程序。如果我要将这
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;
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
为了尝试转换上述内容,我将通过以下两个简单示例:
例1:
例2:
我有点理解第一个URL中的示例,但是第二个URL让我有点困惑。在第二个URL的示例中,SummaryConfig
类的用途是什么?看起来,SummaryConfig
类是XML文件的Java版本。第一个示例中的示例未使用此方法。这两者的区别是什么
当您使用注释时,是否可以将配置详细信息放在Java类中(例如,SummaryConfig
),也可以像第一个URL中的示例一样将注释放在bean中
谢谢
编辑2
这是我到目前为止所做的
我修改了xml文档以删除配置并启用组件的自动扫描(注意:我更改了修改版本的包名)
这就是我被困的地方(乐器演奏班)
- 此类中是否需要@Component注释?或者仅当该类要从另一个类引用时才需要它
- 我知道我需要@Autowire乐器和歌曲属性,但我如何知道我是想按名称还是按类型自动连线
- 如果在这个类中没有表示字符串属性的bean,我将如何自动连接字符串属性?i、 e乐器属性将指钢琴类,但歌曲属性将自动连接到什么
我想我是对的,其他任何类都不需要注释
谢谢注释可以像标记接口一样用作标记
class Foo implements java.io.Serializable{
...
}
Serializable只是一个标记接口,这样应用程序就可以了解
运行时的类(基本上是通过反射)
标记接口的问题在于不能使用它们来标记字段或方法,这就是引入注释的原因
假设您有这个注释
public @interface myAnnotation{
}
您可以简单地获取在运行时由该标记修饰的方法或字段
Hibernate和Spring由于许多框架需要一些关于代码或类的信息,如果您是这些框架的开发人员,您将如何实现这一点?当然,注释是最好的解决方案(至少是更干净的方法)
不要认为标记接口过时了,使用标记也有一些优点,因为它们确保类型安全。
void M(Serializable s)
除非实现了可序列化标记,否则不能将任何对象传递给该方法。p> 这就是我被困的地方(乐器演奏班)
- 此类中是否需要@Component注释?或者只是 如果要从另一个类引用该类,则该类是必需的
Instrumentalist
)实现Instrumentalist
,因此还需要对其进行命名
当配置ApplicationContext时,Spring会扫描带有@Component、@Repository、@Controller和@Service注释的类。这四个注释之间的区别是语义上的(区分类在代码中的角色),它们都做完全相同的事情(例如,除非您有一些只处理特定注释类型的AOP东西;现在您不需要关心这一点)
使用上述任何注释之一注释类与在xml中声明bean相同:
<bean id="saxophone" class="com.sia.ch1.instrumentalist.Saxophone"> ... </bean>
请注意,默认情况下,bean的名称与类相同,只是类名的第一个字母改为小写(因此@Component public class SomeClass
将创建一个名为“SomeClass”的bean)
如果要命名bean,请将该名称作为注释的参数:
@Component("kenny")
public class Instrumentalist implements Performer {
与
@Component
public class Saxophone implements Instrument{
<bean id="kenny" class="com.sia.ch1.instrumentalist.Instrumentalist">
<bean id="kenny" class="com.sia.ch1.instrumentalist.Instrumentalist">
<property name="instrument" ref="piano"/>
</bean>
由于有两种Instrument
-实现,因此Spring没有足够的信息来确定要在Instrumentalist
中注入哪一种。这就是-注释的作用。使用@Qualifier,您告诉Spring按名称注入自动连接的依赖项。要插入钢琴
,使用乐器演奏者
中的@Qualifier实现乐器
,可通过以下方式完成:
@Component(value="kenny")
public class Instrumentalist implements Performer
{
@Autowired
@Qualifier("piano")
private Instrument instrument;
与
@Component
public class Saxophone implements Instrument{
<bean id="kenny" class="com.sia.ch1.instrumentalist.Instrumentalist">
<bean id="kenny" class="com.sia.ch1.instrumentalist.Instrumentalist">
<property name="instrument" ref="piano"/>
</bean>
您可以混合和匹配XML
@Component(value="kenny")
public class Instrumentalist implements Performer
{
@Autowired
@Qualifier("piano")
private Instrument instrument;
<bean id="kenny" class="com.sia.ch1.instrumentalist.Instrumentalist">
<property name="instrument" ref="piano"/>
</bean>
<bean id="songName" class="java.lang.String">
<constructor-arg value="Valley of the Queens"/>
</bean>
@Autowired
@Qualifier("songName")
private String song;