如何在使用new创建的对象中在spring中自动装配对象

use*_*859 13 java spring autowired

我想要做的就是在NotesPanel类中自动连接字段backgroundGray,但我得到的只是下面的例外.

那么,问题是,如何正确地自动装配它?它真的让我发疯,因为它可能是非常愚蠢的我做错了...

谢谢你的帮助!托尔斯滕

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'notepad' defined in class path resource [Beans.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [notepad.Notepad]: Constructor threw exception; nested exception is java.lang.NullPointerException
Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [notepad.Notepad]: Constructor threw exception; nested exception is java.lang.NullPointerException
Caused by: java.lang.NullPointerException
    at notepad.NotesPanel.<init>(NotesPanel.java:23)
    at notepad.Notepad.<init>(Notepad.java:18)
Run Code Online (Sandbox Code Playgroud)

类记事本:

package notepad;

import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.JFrame;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Notepad
{

  public Notepad()
  {
    JFrame frame = new JFrame();
    frame.setLayout(new BorderLayout());
    frame.add(new NotesPanel(), BorderLayout.CENTER);

    frame.setPreferredSize(new Dimension(1024, 768));
    frame.pack();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    frame.setLocationRelativeTo(null);
  }

  public static void main(String[] args)
  {

    ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
    context.getBean("notepad");

  }
}
Run Code Online (Sandbox Code Playgroud)

课堂笔记:

package notepad;

import java.awt.BorderLayout;

import javax.swing.JPanel;
import javax.swing.JTextPane;

import org.springframework.beans.factory.annotation.Autowired;

public class NotesPanel
    extends JPanel
{
  JTextPane tPane = new JTextPane();

  @Autowired
  private BackgroundGray backgroundgray;

  public NotesPanel()
  {
//    backgroundgray = new BackgroundGray();
//    backgroundgray.setGray("200");
    setLayout(new BorderLayout());
    tPane.setBackground(backgroundgray.getGrayObject());
    add(tPane, BorderLayout.CENTER);
    tPane.setText("Fill me with notes... ");
  }

}
Run Code Online (Sandbox Code Playgroud)

类BackgroundGray:

package notepad;

import java.awt.Color;

public class BackgroundGray
{
  String gray;

  public BackgroundGray()
  {
    System.out.println("Background Gray Constructor.");
  }

  public String getGray()
  {
    return gray;
  }

  public void setGray(String gray)
  {
    this.gray = gray;
  }

  public Color getGrayObject()
  {
    int val = Integer.parseInt(gray);
    return new Color(val, val, val);
  }

}
Run Code Online (Sandbox Code Playgroud)

beans.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" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <context:annotation-config />

    <bean id="notepad" class="notepad.Notepad"/>

    <bean id="backgroundgray" class="notepad.BackgroundGray" autowire="byName">
        <property name="gray" value="120"></property>
    </bean>
</beans>
Run Code Online (Sandbox Code Playgroud)

Ral*_*lph 21

Spring支持@Autowire,...仅适用于Spring Beans.通常,Java类在由Spring创建时变为Spring Bean,而不是由Spring创建new.

一个workarround是注释类,@Configurable但你必须使用AspectJ(编译时或加载时挥手)!

@see 使用Spring的@Configurable三个简单步骤进行简短的逐步指导.


Xst*_*ian 6

当你用new创建一个对象时,autowire\inject不起作用......

作为解决方法,您可以尝试这样做:

创建NotesPanel的模板bean

<bean id="notesPanel" class="..." scope="prototype">
    <!-- collaborators and configuration for this bean go here -->
</bean>
Run Code Online (Sandbox Code Playgroud)

并以这种方式创建一个istance

context.getBean("notesPanel");
Run Code Online (Sandbox Code Playgroud)

PROTOTYPE:这可以将单个bean定义范围包含任意数量的对象实例.