How to use @Autowired in spring

2020-07-27 12:17发布

问题:

my spring.xml is as below

<?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>

and I have Test1 class

package com.mkyong.service;

import org.springframework.stereotype.Component;
@Component("test1")
public class Test1 {
public int i=1;
}

I have a App class

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);
    }

}

But I get the output as null.I can not Autowire it properly.Where I am doing wrong?

回答1:

When you do this:

App app=new App();

It creates a new instance of App that is NOT managed by Spring, hence you wont have the autowired components.

You need to declare the App bean in the spring xml and then retrieve it using context.getBean("beanName")

E.g. in the Spring XML you can declare the bean something like this:

<bean id="app" class="com.mkyong.common.App" />

and then retrieve it back with context.getBean("app") then it will have the Autowired components.



回答2:

You don't have to search for your bean using context.getBean("beanName"). This introduces
unnecessary dependency on Spring framework. That's not very clean.

It could be better to annotate your App class with @Configurable annotation.

...
@Configurable
public class App 
{
    @Autowired
    Test1 test1;
...

and add to your spring.xml one tag:

<context:spring-configured />

Thats all. Now you can create your App class as you wanted and autowiring will work.

...
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
App app=new App();
System.out.println(app.test1);
...

Note: you need spring-aspects, spring-aop, aspectjrt on your classpath.

<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>