当我使用autowire和组件注释时,不会注入或创建我的spring bean [英] My spring bean is not injected or created when I use the autowire and component annotations

查看:114
本文介绍了当我使用autowire和组件注释时,不会注入或创建我的spring bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试理解DI,但是当我使用autowire和组件注释时,我的spring bean没有被注入或创建。相反,我得到一个nullpointer异常,因为我的bean是null。我应该手动创建它,在那种情况下?当几个bean相互依赖时,您在哪里指定创建bean的顺序?这是我的代码:



App.java

  package se。 springtest; 

import se.springtest.Person;

import org.springframework.core.io.ClassPathResource;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;

公共类App
{
public static void main(String [] args)
{
BeanFactory factory = new XmlBeanFactory(
new使用ClassPathResource( 应用程序的context.xml));

Person p =(Person)factory.getBean(person);
p.show();

}
}

application-context.xml

 <?xml version =1.0encoding =UTF-8?> 

< beans xmlns =http://www.springframework.org/schema/beans
xmlns:xsi =http://www.w3.org/2001/XMLSchema -instance
xmlns:jdbc =http://www.springframework.org/schema/jdbc
xmlns:context =http://www.springframework.org/schema/context
xsi:schemaLocation =http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/ schema / jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring -context.xsd>

< context:component-scan base-package =se.springtest/>

< / beans>

Person.java

  package se.springtest; 

public interface Person {
public void show();
}

PersonImpl.java

  package se.springtest; 
import org.springframework.stereotype.Component;
import org.springframework.beans.factory.annotation.Autowired;

@Component(person)
公共类PersonImpl实现Person {
private String firstName;
private String lastName;
私人AdressInfo地址;

public PersonImpl(){firstName =Olle;的lastName = 奥尔森; System.out.println(创建PersonImpl对象);}


@Autowired
public void setAdress(AdressInfo adress){
this.adress = adress;
}

public AdressInfo getAdress(){
return adress;
}

public String getFirstName(){
return firstName;
}

public void setFirstName(String firstName){
this.firstName = firstName;
}

public String getLastName(){
return lastName;
}

public void setLastName(String lastName){
this.lastName = lastName;
}

public void show(){
System.out.println(Name is+ getFirstName()++ getLastName());
if(adress!= null)
adress.show();
else System.out.println(null);
}
}

AdressInfo.java

  package se.springtest; 

公共界面AdressInfo {
public void show();
}

AdressInfoImpl.java

  package se.springtest; 

import org.springframework.stereotype.Service;

@Service(adress)
公共类AdressInfoImpl实现AdressInfo {
private String adress;

public AdressInfoImpl(){adress =Storgatan 1; System.out.println(创建AdressImpl对象);}

public String getAdress(){
return adress;
}

public void setAdress(String adr){
this.adress = adr;
}

public void show(){
System.out.println(Address is+ adress);
}
}

pom.xml

 < project xmlns =http://maven.apache.org/POM/4.0.0xmlns:xsi =http://www.w3 .org / 2001 / XMLSchema-instance
xsi:schemaLocation =http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd\"> ;
< modelVersion> 4.0.0< / modelVersion>
< groupId> se.springtest< / groupId>
< artifactId> spring-hello< / artifactId>
< packaging> jar< / packaging>
< version> 1.0-SNAPSHOT< / version>
< name> spring-hello< / name>
< url> http://maven.apache.org< / url>

< build>
< plugins>
< plugin>
< groupId> org.codehaus.mojo< / groupId>
< artifactId> exec-maven-plugin< / artifactId>
< executions>
< execution>
< goals>
< goal> java< / goal>
< / goals>
< / execution>
< / executions>

< configuration>
< mainClass> se.springtest.App< / mainClass>

< / configuration>


< / plugin>
< plugin>
< artifactId> maven-compiler-plugin< / artifactId>
< configuration>
< source> 1.5< / source>
< target> 1.5< / target>
< / configuration>
< / plugin>

< / plugins>
< / build>

< dependencies>
< dependency>
< groupId> junit< / groupId>
< artifactId> junit< / artifactId>
< version> 3.8.1< / version>
< scope> test< / scope>
< / dependency>
< dependency>
< groupId> org.springframework< / groupId>
< artifactId> spring< / artifactId>
< version> 2.5.6.SEC02< / version>
< / dependency>
< / dependencies>
< / project>

我用



$ b <$ p $编译它p> mvn clean compile exec:java

但是我得

 名称是Olle Olsson 
null

而不是

 名字是Olle Olsson 
地址是Storgatan 1

如果有人能告诉我这是什么问题,那将会非常有帮助。它会让我和其他人更好地理解DI ...

解决方案

你只是创建一个BeanFactory,它的数量非常有限功能。它实际上只是实例化bean,让你手动将它们连接在一起。您正在寻找的功能仅存在于ApplicationContexts中,这实际上是Spring的面包和黄油。更改

  BeanFactory factory = new XmlBeanFactory(
new ClassPathResource(application-context.xml));

  BeanFactory factory = new ClassPathXmlApplicationContext(
application-context.xml);

然后阅读BeanFactory熟悉区别。


I'm trying to understand DI, but my spring bean is not injected or created when I use the autowire and component annotations. Instead I get a nullpointer exception because my bean is null. Should I created it manually and in that case where? Where do you specify the order in which the beans are created, when several beans depend on each other? Here is my code:

App.java

package se.springtest;

import se.springtest.Person;

import org.springframework.core.io.ClassPathResource;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;

public class App 
{
  public static void main( String[] args )
  {
    BeanFactory factory = new XmlBeanFactory(
            new ClassPathResource("application-context.xml"));

        Person p = (Person) factory.getBean("person");
        p.show();

  }
}

application-context.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:jdbc="http://www.springframework.org/schema/jdbc"
   xmlns:context="http://www.springframework.org/schema/context"
   xsi:schemaLocation="http://www.springframework.org/schema/beans    http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

<context:component-scan base-package="se.springtest"/>

</beans>

Person.java

package se.springtest;

public interface Person {
public void show();
}

PersonImpl.java

package se.springtest;
import org.springframework.stereotype.Component;
import org.springframework.beans.factory.annotation.Autowired;

@Component("person")
public class PersonImpl implements Person {
private String firstName;
private String lastName;
private AdressInfo adress;

public PersonImpl() {firstName="Olle"; lastName="Olsson";       System.out.println("creating PersonImpl object");}


@Autowired
public void setAdress(AdressInfo adress) {
    this.adress = adress;
}

public AdressInfo getAdress() {
    return adress;
}

public String getFirstName() {
    return firstName;
}

public void setFirstName(String firstName) {
    this.firstName = firstName;
}

public String getLastName() {
    return lastName;
}

public void setLastName(String lastName) {
    this.lastName = lastName;
}

public void show() {
    System.out.println("Name is " + getFirstName() + " " +getLastName());
    if (adress!=null)
        adress.show();
    else System.out.println("null");
}
}

AdressInfo.java

package se.springtest;

public interface AdressInfo {
public void show();
}

AdressInfoImpl.java

package se.springtest;

import org.springframework.stereotype.Service;

@Service("adress")
public class AdressInfoImpl implements AdressInfo {
private String adress;

public AdressInfoImpl() {adress="Storgatan 1"; System.out.println("creating AdressImpl object");}

public String getAdress() {
    return adress;
}

public void setAdress(String adr) {
    this.adress = adr;
}

public void show() {
    System.out.println("Address is " + adress);  
}
}

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>se.springtest</groupId>
<artifactId>spring-hello</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>spring-hello</name>
<url>http://maven.apache.org</url>

<build>
 <plugins>
  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <executions>
      <execution>
        <goals>
          <goal>java</goal>
        </goals>
      </execution>
    </executions>

    <configuration>
      <mainClass>se.springtest.App</mainClass>

    </configuration>


  </plugin>
  <plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
      <source>1.5</source>
      <target>1.5</target>
    </configuration>
  </plugin>

</plugins>
</build>  

<dependencies>
 <dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>3.8.1</version>
  <scope>test</scope>
 </dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring</artifactId>
  <version>2.5.6.SEC02</version>
</dependency>
</dependencies>
</project>

And I compile it with

mvn clean compile exec:java

but I get

Name is Olle Olsson
null

instead of

Name is Olle Olsson
Adress is Storgatan 1

It would be really helpful if someone could tell me what is the problem. It would make me and maybe others understanding DI better...

解决方案

You're only creating a BeanFactory, which has extremely limited functionality. It really does nothing more than instantiate beans and let you manually wire them together. The functionality you're looking for only exists in ApplicationContexts, which is really Spring's bread and butter. Change

BeanFactory factory = new XmlBeanFactory(
        new ClassPathResource("application-context.xml"));

to

BeanFactory factory = new ClassPathXmlApplicationContext(
        "application-context.xml");

Then read "The BeanFactory" to get familiar with the distinction.

这篇关于当我使用autowire和组件注释时,不会注入或创建我的spring bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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