Java 如何在spring中使用@Autowired
我的spring.xml如下所示Java 如何在spring中使用@Autowired,java,spring,autowired,Java,Spring,Autowired,我的spring.xml如下所示 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xm
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:oxm="http://www.springframework.org/schema/oxm" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
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
">
<context:component-scan base-package="com.mkyong.service" />
<context:annotation-config />
</beans>
我有一个应用程序类
package com.mkyong.common;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.mkyong.service.Test1;
public class App
{
@Autowired
Test1 test1;
public static void main( String[] args )
{
ApplicationContext context = new ClassPathXmlApplicationContext(
"spring.xml");
App app=new App();
System.out.println(app.test1);
}
}
但是我得到的输出为空。我无法正确自动连线。我哪里做错了?执行此操作时:
App app=new App();
它创建了一个不由Spring管理的应用程序的新实例,因此您不会拥有自动连接的组件
您需要在SpringXML中声明AppBean,然后使用context.getBean(“beanName”)
例如,在SpringXML中,您可以这样声明bean:
<bean id="app" class="com.mkyong.common.App" />
然后用context.getBean(“app”)
将其检索回来,然后它将拥有自动连接的组件。执行此操作时:
App app=new App();
它创建了一个不由Spring管理的应用程序的新实例,因此您不会拥有自动连接的组件
您需要在SpringXML中声明AppBean,然后使用context.getBean(“beanName”)
例如,在SpringXML中,您可以这样声明bean:
<bean id="app" class="com.mkyong.common.App" />
然后用
context.getBean(“app”)
将其检索回来,然后它将拥有自动连接的组件。您不必使用context.getBean(“beanName”)
搜索您的bean。这将引入对Spring框架不必要的依赖。那不是很干净 最好用
@可配置的注释来注释你的应用程序
类
...
@Configurable
public class App
{
@Autowired
Test1 test1;
...
并在spring.xml中添加一个标记:
<context:spring-configured />
注意:在类路径上需要spring方面、spring aop和aspectjrt
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
</dependency>
org.springframework
春季方面
org.springframework
春季aop
org.aspectj
aspectjrt
您不必使用context.getBean(“beanName”)
搜索bean。这将引入
对Spring框架不必要的依赖。那不是很干净
最好用@可配置的注释来注释你的应用程序
类
...
@Configurable
public class App
{
@Autowired
Test1 test1;
...
并在spring.xml中添加一个标记:
<context:spring-configured />
注意:在类路径上需要spring方面、spring aop和aspectjrt
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
</dependency>
org.springframework
春季方面
org.springframework
春季aop
org.aspectj
aspectjrt
谢谢你,伙计。现在很好用。我也从你的回答中学到了。再次感谢。顺便问一下,你知道为什么不允许在6分钟内接受答案吗?@sanjayalianage阅读:*理由是,如果人们在几分钟甚至几秒钟内接受答案(不是开玩笑),他们甚至没有时间正确评估或测试答案。*这可以在没有不必要的context.getBean(“beanName”)的情况下完成
lookup见下面我的答案。谢谢你,伙计。现在很好用。我也从你的回答中学到了。再次感谢。顺便问一下,你知道为什么不允许在6分钟内接受答案吗?@sanjayalianage阅读:*理由是,如果人们在几分钟甚至几秒钟内接受答案(不是开玩笑),他们甚至没有时间正确评估或测试答案。*这可以在没有不必要的context.getBean(“beanName”)的情况下完成
lookup查看下面我的答案。谢谢朋友。这真的很有帮助。如果这是快速的,我本可以接受这个答案。无论如何,这里是我对完美答案的投票。一台计算机可以工作,但在另一个相同的项目中打印空System.out.println(app.test1);在我做了你要求的改变后。有什么想法吗。这真的很有帮助。如果这是快速的,我本可以接受这个答案。无论如何,这里是我对完美答案的投票。一台计算机可以工作,但在另一个相同的项目中打印空System.out.println(app.test1);在我做了你要求的更改之后。有什么想法吗