Comment autowire un objet au printemps dans un objet créé avec de nouvelles
tout ce que je veux faire est de autowire le domaine backgroundGray dans le NotesPanel classe, mais tout ce que j'obtiens est l'exception ci-dessous.
Donc, la question est, comment autowire il correctement ? - Il vraiment me rend fou parce que c'est probablement quelque chose de très stupide, je fais mal...
merci pour toute aide!
Thorsten
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)
Classe Le Bloc-Notes:
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");
}
}
Classe Notespanel:
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... ");
}
}
Classe 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);
}
}
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>
source d'informationauteur user1119859
Vous devez vous connecter pour publier un commentaire.
Printemps de soutien
@Autowire
... uniquement pour les Beans Spring. Normalement une Classe Java devenir un Printemps Bean lorsqu'il est créé par le Printemps, mais pas parnew
.Un workarround est d'annoter la classe avec
@Configurable
mais vous devez utiliser AspectJ (moment de la compilation ou loadtime agitant)!@voir À l'aide du Printemps
@Configurable
en trois étapes faciles pour un court instructions étape par étape.Lorsque vous créez un objet, par exemple, l'autowire\injecter ne fonctionnent pas...
comme solution de contournement, vous pouvez essayer ceci:
créer votre modèle de bean de NotesPanel
et de créer un istance de cette façon
PROTOTYPE : Ce étendues un seul haricot définition à avoir un nombre quelconque d'instances de l'objet.
Le problème est ici:
cadre.add(new NotesPanel(), BorderLayout.CENTRE);
vous créez un nouvel objet de la classe NotesPanel dans le constructeur de la classe du bloc-notes.
Le constructeur est appelé avant que méthode principale, de sorte Printemps contexte n'a pas encore été chargé.
Lors de l'instanciation de l'objet pour NotesPanel, il ne peut pas automatique fil BackgroundGray parce que le Printemps contexte n'existe pas à l'heure actuelle.
Je partage avec vous un exemple.
J'espère que vous l'aimez 🙂
Ma configuration bean:
Mon ihm java swing qui utilise ma configuration bean: