Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8199609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T06:12:21+00:00 2026-06-07T06:12:21+00:00

i am trying to use spring in desktop application, but i am facing a

  • 0

i am trying to use spring in desktop application, but i am facing a problem with autowiring in action methods of my JPanel.

i am loading the applicationContext in my main method as follows:

public static void main(String[] args) {

        new ClassPathXmlApplicationContext(
                "classpath:/META-INF/spring/applicationContext.xml");
            MainFrame frame = new MainFrame();
    Signup signup = new Signup();
    frame.add(signup);
    frame.setResizable(false);
    frame.setTitle("Please input your data");
    frame.setBounds(100, 100, 450, 180);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);

}

and i can see that it’s loaded with no problems.

my panel code:

@Component
public class Signup extends JPanel {


    @Autowired
    private UserDao userDao;

    public Signup() {



        JButton btn_submit = new JButton("Submit");
        btn_submit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                registerUser();
            }
        });



    }

    private void registerUser() {

        User newUser = new User();
        newUser.setName(username);
        newUser.setSalary(salary);
        userDao.addUser(newUser);

    }
}

the context:component-scan is configured properly, and i am using context:annotation-config too but i always gets NullPointerException in userDao.addUser(newUser);
which means that the Dependency Injection is not working as it should.

please advise how to fix this issue.

UPDATE: applicationContext.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:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx" 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.0.xsd
                http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
                http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:component-scan base-package="${project.groupId}" />

    <context:annotation-config />

    <tx:annotation-driven transaction-manager="transactionManager" />

    <bean id="propertyPlaceholderConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>

                <value>classpath:messages/application.properties</value>

            </list>
        </property>
    </bean>


    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">

        <property name="dataSource" ref="dataSource" />
        <property name="packagesToScan" value="${project.groupId}.domain" />


        <property name="hibernateProperties">
            <value>
                hibernate.dialect=org.hibernate.dialect.DerbyDialect
                hibernate.show_sql=false
                hibernate.format_sql=false
                hibernate.hbm2ddl.auto=validate
            </value>
        </property>

    </bean>


    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">

        <property name="driverClassName" value="org.apache.derby.jdbc.EmbeddedDriver" />

        <property name="url" value="jdbc:derby:test" />

        <property name="username" value="root" />

        <property name="password" value="root" />

    </bean>


    <bean id="transactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />


</beans>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T06:12:23+00:00Added an answer on June 7, 2026 at 6:12 am

    If you are going to configure Spring in a desktop environment, then you must be the one to work with the ApplicationContext.

    For example, if you want to get a hold of your Signup class that you have posted here, you would do something like this in your main method:

    public static void main(String[] args) {
        ApplicationContext appContext = new ClassPathXmlApplicationContext(
                "classpath:/META-INF/spring/applicationContext.xml");
        Signup signup = appContext.getBean(Signup.class);
        //use signup here...
    }
    

    Using new Signup() to get a new instance of the Signup class, which won’t work the way you want, because you want it to be a Spring managed class! (Actually, you could get it to work that way, but that is beyond my answer here)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use UTF-8 encoding for the Spring application I'm developing but I
I am trying to use Spring.NET with a C# application to populate a parameter
I have a Spring MVC application trying to use a rich domain model, with
I am trying to use Spring Security 3.0.5 in my web application. Basically, I
I am trying to use Spring LDAP for coding <ldap-server ldif=classpath:my-ldap-clone.ldif /> but I
I'am trying to use web-service from a desktop-application I used the Tutorials in here
I'm trying to use Spring DAO with Hibernate for a web application. When I
I'm new to Spring and trying to use the Spring Formatting SPI for UI
I'm trying to understand why one would use Spring Batch over a scripting language
I'm trying to use aspectj with compile time weaving to support annotations like Spring's

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.