首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在独立的Java应用程序中使用Spring3自动线缆

在独立的Java应用程序中使用Spring3自动线缆
EN

Stack Overflow用户
提问于 2010-09-07 22:44:41
回答 5查看 101.9K关注 0票数 72

下面是我的代码:

public class Main {

    public static void main(String[] args) {
        Main p = new Main();
        p.start(args);
    }

    @Autowired
    private MyBean myBean;
    private void start(String[] args) {
        ApplicationContext context = 
            new ClassPathXmlApplicationContext("META-INF/config.xml");
        System.out.println("my beans method: " + myBean.getStr());
    }
}

@Service 
public class MyBean {
    public String getStr() {
        return "string";
    }
}

<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-3.0.xsd
     http://www.springframework.org/schema/context
     http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 
    <context:annotation-config /> 
    <context:component-scan base-package="mypackage"/>
</beans>

为什么这个不起作用?我得到了NullPointerException。可以在独立的应用程序中使用自动装配吗?

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2010-09-07 23:02:42

Spring在独立应用程序中工作。您正在使用错误的方式创建spring bean。正确的方法如下:

@Component
public class Main {

    public static void main(String[] args) {
        ApplicationContext context = 
            new ClassPathXmlApplicationContext("META-INF/config.xml");

        Main p = context.getBean(Main.class);
        p.start(args);
    }

    @Autowired
    private MyBean myBean;
    private void start(String[] args) {
        System.out.println("my beans method: " + myBean.getStr());
    }
}

@Service 
public class MyBean {
    public String getStr() {
        return "string";
    }
}

在第一种情况下(问题中的情况),您将自己创建对象,而不是从Spring上下文中获取它。因此,Spring没有机会对依赖项进行Autowire (这会导致NullPointerException)。

在第二种情况下(此答案中的情况),您从Spring上下文中获取bean,因此它是Spring托管的,而Spring负责autowiring

票数 132
EN

Stack Overflow用户

发布于 2016-12-07 20:54:19

Spring不再使用XML文件,而是大量使用注释。下面的示例是一个简单的独立Spring应用程序,它使用注释而不是XML文件。

package com.zetcode.bean;

import org.springframework.stereotype.Component;

@Component
public class Message {

   private String message = "Hello there!";

   public void setMessage(String message){

      this.message  = message;
   }

   public String getMessage(){

      return message;
   }
}

这是一个简单的bean。它使用@Component注释进行装饰,以便由Spring容器进行自动检测。

package com.zetcode.main;

import com.zetcode.bean.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;

@ComponentScan(basePackages = "com.zetcode")
public class Application {

    public static void main(String[] args) {

        ApplicationContext context
                = new AnnotationConfigApplicationContext(Application.class);

        Application p = context.getBean(Application.class);
        p.start();
    }

    @Autowired
    private Message message;
    private void start() {
        System.out.println("Message: " + message.getMessage());
    }
}

这是主要的Application类。@ComponentScan注释将搜索组件。@Autowired注释将bean注入到message变量中。AnnotationConfigApplicationContext用于创建Spring应用程序上下文。

我的Standalone Spring tutorial展示了如何创建同时包含XML和注解的独立Spring应用程序。

票数 29
EN

Stack Overflow用户

发布于 2017-01-16 03:42:30

对于Spring4,使用Spring Boot,我们可以在不使用直接从ApplicationContext获取Bean的反模式的情况下获得以下示例:

package com.yourproject;

@SpringBootApplication
public class TestBed implements CommandLineRunner {

    private MyService myService;

    @Autowired
    public TestBed(MyService myService){
        this.myService = myService;
    }

    public static void main(String... args) {
        SpringApplication.run(TestBed.class, args);
    }

    @Override
    public void run(String... strings) throws Exception {
        System.out.println("myService: " + MyService );
    }

}

@Service 
public class MyService{
    public String getSomething() {
        return "something";
    }
}

确保所有注入的服务都在com.yourproject或其子包下。

票数 11
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3659720

复制
相关文章

相似问题

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