下面是我的代码:
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
。可以在独立的应用程序中使用自动装配吗?
发布于 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
。
发布于 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应用程序。
发布于 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
或其子包下。
https://stackoverflow.com/questions/3659720
复制相似问题