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 8531671
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:32:27+00:00 2026-06-11T09:32:27+00:00

the Spring component-scan seems not to be working. I am using Spring 3.1 and

  • 0

the Spring component-scan seems not to be working. I am using Spring 3.1 and tomcat 7.0.

This is how my applicationContext.xml looks like:

<?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:cache="http://www.springframework.org/schema/cache"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd 
            http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">

    <context:annotation-config/> 
    <context:component-scan base-package="com.abc"/>

    <bean id="myBean" class="com.efg.test.MyBean" />
</beans>

I have a class like this:

package com.abc.test

@Component
class Test {
    @Autowired
    private static MyBean myBean;

    public static MyBean getMyBean() { return myBean; }
    public static void setMyBean(MyBean bean) { myBean = bean; }
}

I had expected that Test will be initialized by Spring on startup of the webapplication and that myBean will be injected automatically, so if I call any (static) method of Test I have a not null reference to myBean.

However, myBean does not get injected and is always null (myBean itself gets initialized as singleton, it just is not injected). What else do I have to do to get this working? If I add the Test-class to the applicationContext.xml everything is working. My guess is that Test is just not initialized by Spring and therefore not wired.

Update:
I need to be able to access the autowired field from a static context (a method of this class is called as a JSP function), therefore it has to be static.

  • 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-11T09:32:28+00:00Added an answer on June 11, 2026 at 9:32 am

    The idea of Spring is that it initializes and configures instances of your classes (i.e. objects). In practice it means that @Autowired cannot be applied to static fields, because static fields don’t belong to any instance (and your accessor methods shouldn’t be static as well).

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

Sidebar

Related Questions

My main Spring Configuration XML file specifies something like this: <context:component-scan base-package=com.application /> The
Hi mapping css doesn't work. This is my config: spring-servlet.xml: <?xml version="1.0" encoding="UTF-8"?> <context:component-scan
Little confused, the basic spring mvc app has this: app-config.xml <context:component-scan base-package=org.springframework.samples.mvc.basic /> and
I using annotation configuration for spring context like <context:annotation-config/> <context:component-scan base-package=example/> But project is
I like to work with Spring features like @Component , @Autowired or ApplicationContext even
This is my app.xml : <context:component-scan base-package=destiny.web /> <context:annotation-config/> And there is a Dao
I am using Spring 3. In my application context xml file I would like
<context:annotation-config/> <context:component-scan... this is used for class that i need to annotated with @Repository
I have a spring component which was used as a flex-blazeds endpoint (using @RemotingDestination
I have the following spring configuration: <context:component-scan base-package=uk.co.mysite.googlecontactsync.aop/> <bean name=simpleEmailSender class=uk.co.mysite.util.email.simple.SimpleEmailSenderImplementation/> <aop:aspectj-autoproxy/> Then I

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.