我正试图学习春天和温和,真的在努力理解说明和他们如何工作。 在互联网上看到的大多数例子都是以说明为基础的例子,因此,需要了解说明在我能够学习春天或自由之前如何首先工作。
我有这样一个想法,即它们是什么,它们是什么用的。 我知道,他们取代了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
我理解一下第一份《欧洲刑法》中的例子,但第二份《欧洲刑法》把我混为一谈。 在第二个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;
}
}
我认为,没有必要对任何其他类别作说明。
增 编