English 中文(简体)
需要一些帮助理解说明——春季说明
原标题:Need some help to understand Anotations - Spring annotations

我正试图学习春天和温和,真的在努力理解说明和他们如何工作。 在互联网上看到的大多数例子都是以说明为基础的例子,因此,需要了解说明在我能够学习春天或自由之前如何首先工作。

我有这样一个想法,即它们是什么,它们是什么用的。 我知道,他们取代了Xml配置。 页: 1 你可以使用说明在 Java代码内直接配置豆类。 确切理解的是如何使用这些工具,何时可以使用。

Trying to understand how this can be done i think it would be helpful if i see the difference between the two. I have here a simple Spring program. If i was to convert this sample program to use annotations what would i need to do?

The reason i want to do it this way is because the program i have provided below is one that i understand very well (an example from the Spring in Action book that i am currently reading). If it is converted to an annotations version i will get an idea as to how and where annotations can be used.

任何建议?

预 收


页: 1

<?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>

Instrumentalist interface

package com.sia.ch1.instrumentalist;
public interface Instrument {
    void play();
}

仪器执行器

package com.sia.ch1.instrumentalist;

import com.sia.ch1.performer.Performance例外;
import com.sia.ch1.performer.Performer;

public class Instrumentalist implements Performer{

    private Instrument instrument;
    private String song;

    public Instrumentalist(){}

    public void perform() throws Performance例外{
        System.out.print("Playing " + song + " : ");        
        instrument.play();
    }

    public void setInstrument(Instrument instrument) {
        this.instrument = instrument;
    }   

    public void setSong(String song) {
        this.song = song;
    }   
}

文书 - Piano

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.Performance例外;
    import com.sia.ch1.performer.Performer;

    public class InstrumentalistApp {

        public static void main(String[] args){
            ApplicationContext ctx = new FileSystemXmlApplicationContext("c:\projects\test\conf\页: 1");

            Performer performer = (Performer) ctx.getBean("kenny");

            try {
                performer.perform();            
            } catch (Performance例外 e) {
                e.printStackTrace();
            }
        }   
    }

例外

package com.sia.ch1.performer;

public class Performance例外 extends 例外 {

    public Performance例外() {
        super();
        // TODO Auto-generated constructor stub
    }

    public Performance例外(String message, Throwable cause) {
        super(message, cause);
        // TODO Auto-generated constructor stub
    }

    public Performance例外(String message) {
        super(message);
        // TODO Auto-generated constructor stub
    }

    public Performance例外(Throwable cause) {
        super(cause);
        // TODO Auto-generated constructor stub
    }
}

Edit 1

尝试和转换上述两个简单例子:

页: 1

Ex2: http://www.theserverside.com/tutorial/Spring-Without-XML-The-Basics-of-Spring-Annotations-vs-Spring-XML-Files

我理解一下第一份《欧洲刑法》中的例子,但第二份《欧洲刑法》把我混为一谈。 在第二个URL中,的目的 摘要类别: 参看<代码>。 摘要Config类别是XML文档的 Java版本。 这种做法在第一个例子中没有使用。 两者之间有什么区别?

Could it be that when you using annotations you can put the configuration details in a Java class (e.g. SummaryConfig) and you can also put the annotations in the beans themselves as in the examples in the first URL?

增 编

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>

Added the @Component annotation to the Piano and Saxophone classes. I think this tells the container that this class should be included in the classes to be auto-scanned. right?

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");
    }
}

这里是一只 st子(仪器)。

  • Is the @Component annotation required in this class? Or is it only required if the class is to be referenced from another class?

  • 我知道,我需要@Autowire 仪器和歌曲特性,但我知道我是否希望通过名称或类型实现自动化。

  • How would i autowire the String property if in this class there is no bean that represents it? i.e the instrument property would refer to the piano class but what would the song property be autowired with?


package com.sia.ch1.instrumentalist.annotate;
//
    import org.springframework.stereotype.Component;
    import com.sia.ch1.performer.Performance例外;
    import com.sia.ch1.performer.Performer;
    //
        @Component
        public class Instrumentalist implements Performer{

        private Instrument instrument;
        private String song;

        public Instrumentalist(){}

        public void perform() throws Performance例外{
            System.out.print("Playing " + song + " : ");        
            instrument.play();
        }

        public void setInstrument(Instrument instrument) {
            this.instrument = instrument;
        }   

        public void setSong(String song) {
            this.song = song;
        }   
    }

我认为,没有必要对任何其他类别作说明。

增 编

最佳回答

这里是一只 st子(仪器)。

  • Is the @Component annotation required in this class? Or is it only required if the class is to be referenced from another class?

Yes, it is, if you want the annotation scanning to create the bean from the class for you without separate xml-configuration. Since you ask for an Instrumentalist -implementation with the bean name kenny (by name, not by type Instrumentalist) in your main-method, it also needs to be named.

附加说明的类别包括:@Component,@Reposit, @Controller and @SER., 春季在设计申请目录时的扫描。 这四项说明之间的区别是粗略的(区别该类别在法典中的作用),它们完全相同(例如,你有一些仅处理某种注解的“全权行动”手段;现在,你不需要对此加以注意)。

说明上述任何说明中的某一类同在Xml宣布为 be:

<bean id="saxophone" class="com.sia.ch1.instrumentalist.Saxophone"> ... </bean>

@Component
public class Saxophone implements Instrument{

注:缺席时,除上等名称的第一封信改为下级(见@)。 部分公共类别 . SomeClass将形成一个叫作“一些Class”。

如果你要点名,请将名字作为说明的参数:

@Component("kenny")
public class Instrumentalist implements Performer {

 <bean id="kenny" class="com.sia.ch1.instrumentalist.Instrumentalist">

为说明提供参数的其他途径是@Component(=“kenny”)。 * E/CN.6/2009/1。 - 部分是任择性的,因为说明工作类似:如果只给出一个参数而没有说明外地名称,说明中包含一个称为“<>>tong>/strong>的外地,则参数将定在-field。 如果外地名称是别的,或你想要确定通知的多个领域,则你需要明确界定以下领域:@SomeAnnotation(field1=“stern string”, Field2 = 100)@SomeAnnotation( Value=“someValue”,另一个领域=“ing others”。 这是除此点外的一点,但是它很想知道,因为它首先会混淆。

So, the @Component-annotation tells the Spring context that you want to create a bean (or beans, see @Scope) from the annotated classes. When there s no @Scope -annotation set, the beans will be created as singletons by default (you can autowire the bean to multiple classes, but they all see the same, single instance). For more information about the different scopes, I suggest reading the official documentation.

  • I know that i need to @Autowire the instrument and song properties
    but how do i know if i want to autowire byname or bytype etc

通常,如果你只执行一类(接口),则更方便按类型进行自动化。 只有在单一实施的情况下,按类型进行自动连接,否则春天不能决定哪一种执行方式。

在您的情形下,您有两门不同的班级,分别实施<条码>入侵<>/条码>-接口:<条码>、/条码>和<条码>。 如果你试图按类型使<代码>自动化 。 仪器名称:-bean:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.sia.ch1.instrumentalist.annotate.Instrument] is defined: expected single matching bean but found 2: [piano, saxophone]

由于有2个<条码> 入侵执行, 春天没有足够信息来确定你想要在<条码>上注入什么东西。 这就是@Qualifier。 - 说明步骤。 你用@Qualifier的话说,春天要注入自动电线的依附:。 将<条码>Piano-implementation of Instrument在<条码>上使用@Qualifier :

@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>

请注意,没有必要(但如您愿意)在上使用<>@Component(piano”),因为缺席点名更改了该类别的第一封信,以减小案件,然后将其用作标名。

  • How would i autowire the String property if in this class there is no bean that represents it? i.e the instrument property would refer to the piano class but what would > the song property be autowired with?

This is where the line is drawn with annotations (AFAIK); you cannot declare a bean of type String with annotations (Since java.lang.String is not an interface, cannot be extended as it s final and doesn t have an interface). However, with xml-configuration this is possible:

<bean id="songName" class="java.lang.String">   
    <constructor-arg value="Valley of the Queens"/>
</bean>

您可以将XML和说明结合起来,并参照从说明中宣布为xml的豆类,反之亦然,将本信注入<代码>。 仪器名称:song-field:

@Autowired
@Qualifier("songName")
private String song;

Hope this helped you to understand Springs annotations in general and get started, I d still strongly suggest you to read the official documentation, as there s a LOT more. If you prefer to read books instead of from screen, I d suggest for example Appress s Spring Recipes (but I m sure there are many other good books too).

问题回答

说明可用作标识,如标记接口

class Foo implements java.io.Serializable{
 ...
}

Serializable is just a marker interface so that your application can know information about the class at runtime (basically by reflection).

The problem with marker interfaces is that you can t use them to mark fields or methods,this why annotations were introduced.

假设你有这一说明

public @interface myAnnotation{
}

你们只要能找到这些标志在运行时加以校正的方法或领域。

如果是这些框架的制定者,那么你会如何做到这一点? 当然,说明是最佳解决办法(至少是更清洁的方法)

Don t考虑过时的标识接口。 使用标识也有一些好处,因为它们确保了类型安全。

 void M(Serializable s)

除非采用可乘标记,否则你可以将任何物体转至这种方法。 详情请查阅:。 这里有大量解释。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...