首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >spring framework 3.2.9注释

spring framework 3.2.9注释
EN

Stack Overflow用户
提问于 2014-06-29 21:35:59
回答 1查看 378关注 0票数 0

我尝试使用spring framework 3.2.9,这是我的pom.xml

代码语言:javascript
复制
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
  http://maven.apache.org/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mkyong.common</groupId>
    <artifactId>3_2_9</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>3_2_9</name>
    <url>http://maven.apache.org</url>

    <dependencies>

        <!-- Spring framework -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>3.2.9.RELEASE</version>
        </dependency>
    </dependencies>

    <build>
        <finalName>3_2_9</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <configuration>
                    <webXml>WebContent\WEB-INF\web.xml</webXml>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

我有3个类*hello/MessageService.java:

代码语言:javascript
复制
package hello;

public interface MessageService {
    String getMessage();
}

*hello/MessagePrinter.java

代码语言:javascript
复制
package hello;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class MessagePrinter {

    final private MessageService service;

    @Autowired
    public MessagePrinter(MessageService service) {
        this.service = service;
    }

    public void printMessage() {
        System.out.println(this.service.getMessage());
    }
}

*hello/Application.java

代码语言:javascript
复制
package hello;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.*;

@Configuration
@ComponentScan
public class Application {

    @Bean
    MessageService mockMessageService() {
        return new MessageService() {
            public String getMessage() {
              return "Hello World!";
            }
        };
    }

  public static void main(String[] args) {
      ApplicationContext context = 
          new AnnotationConfigApplicationContext(Application.class);
      MessagePrinter printer = context.getBean(MessagePrinter.class);
      printer.printMessage();
  }
}

但是导入无法解析,而且我使用的注释都不是known.any,没有人可以帮助我。

EN

回答 1

Stack Overflow用户

发布于 2014-06-29 22:41:33

@Autowired注释驻留在spring-beans项目中,请尝试将以下依赖添加到您的pom中:

代码语言:javascript
复制
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-beans</artifactId>
    <version>3.2.9.RELEASE</version>
</dependency>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24476546

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档