如何在简单的spring helloworld应用程序中使用@resource替换applicationcontext [英] How do I replace applicationcontext using @resource in simple spring helloworld application

查看:81
本文介绍了如何在简单的spring helloworld应用程序中使用@resource替换applicationcontext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用spring MVC和web应用程序。但现在我使用spring的简单应用程序。我正在尝试使用@Resource注释替换所有组件扫描概念。我在下面粘贴我的代码。请帮忙纠正它



HelloWorldService.java



I had been using spring MVC and web application. but now i am using simple application using spring. I am trying to replace all that component scan concept using @Resource annotation. I am pasting my code below. Please help to correct it

HelloWorldService.java

package com.gide.iis.bls;

public class HelloWorldService {
 
	private String name;
 
	public void setName(String name) {
		this.name = name;
	}
 
	public String sayHello() {
		return "Hello! " + name;
	}
}





SayHello.java



SayHello.java

package com.gide.iis.bls_main;

import javax.annotation.Resource;

import org.springframework.test.context.ContextConfiguration;

import com.gide.iis.bls.HelloWorldService;

@ContextConfiguration(locations ={ "/META-INF/spring/applicationContext.xml" })
public class SayHello {
	 @Resource(name="helloWorldService")
	 private HelloWorldService helloWorldService;
	 
	 public void test(){
		 String message = helloWorldService.sayHello();
			System.out.println(message);
			 
			//set a new name
			helloWorldService.setName("Spring");
			message = helloWorldService.sayHello();
			System.out.println(message);
	 }
	 
	 public static void main(String[] args) {
		SayHello s1 = new SayHello();
		s1.test();

	}

}







< b> applicationContext.xml




applicationContext.xml

<beans xmlns="http://www.springframework.org/schema/beans"

	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"

	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"

	xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"

	xmlns:task="http://www.springframework.org/schema/task"

	xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd">
 
	<context:component-scan base-package="com.gide.iis.bls" />
	 <context:annotation-config/>
 
	<bean id="helloWorldService"

		class="com.gide.iis.bls.HelloWorldService">
		<property name="name" value="Program Creek Readers" />
	</bean>
</beans>







i有app lrc / main / resource / META-INF / spring /中的licationContext。上面的代码在SayHello.java中与ApplicationContext一起正常工作,但在尝试用@Resource和@ContextConfiguration替换时。它显示以下错误

线程main中的异常java.lang.NullPointerException

at com.gide.iis.bls_main.SayHello.test(SayHello.java:15)

com.gide.iis.bls_main.SayHello.main(SayHello.java:30)



i知道它无法读取豆。如何运行



我尝试过:



我有尝试也给@ContextConfiguration(locations = {classpath:/META-INF/spring/applicationContext.xml})




i have applicationContext in src/main/resource/META-INF/spring/. The above code working fine with ApplicationContext in SayHello.java but when trying to replace with @Resource and @ContextConfiguration. it showing following error
Exception in thread "main" java.lang.NullPointerException
at com.gide.iis.bls_main.SayHello.test(SayHello.java:15)
at com.gide.iis.bls_main.SayHello.main(SayHello.java:30)

i know its not able to read that bean. How to make run

What I have tried:

I have tried giving also @ContextConfiguration(locations ={ "classpath:/META-INF/spring/applicationContext.xml" })

推荐答案

经过这么多研究。我得到了解决方案



在独立的Java应用程序中使用Spring 3 autowire - Stack Overflow [ ^ ]
After so much research. i got the solution

Using Spring 3 autowire in a standalone Java application - Stack Overflow[^]


这篇关于如何在简单的spring helloworld应用程序中使用@resource替换applicationcontext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆