• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

Why the field is not inject by @Inject

 
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when enter url http://localhost:8080/MySpringMvc/home/test1, I expect apple1 will be injected but not, its value is null, why?







 
Bartender
Posts: 2008
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First of all, in Spring, we use @Autowired instead of @Inject.
Secondly, is your @SpringBootApplication class in the root directory of your project?
 
peter tong
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have no used @SpringBootApplication annotation, in fact I am new in spring, I expect Spring container will create and manage the TestController class as it has @Controller annotation and will instantiate, inject its member field.
I have checked my application pom, it has not use spring boot. the pom is as follow:


Is this related?
 
Himai Minh
Bartender
Posts: 2008
13
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First of all, have you tried @Autowired instead of @Inject?
Secondly, do you have @ComponentScan (basePackages = "....") //where basePackage is the name of the base package such as "com.example" ?
 
peter tong
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Himai Minh wrote:First of all, have you tried @Autowired instead of @Inject?
Secondly, do you have @ComponentScan (basePackages = "....") //where basePackage is the name of the base package such as "com.example" ?



Oh, yes, after change to @Autowired, apple1 can be injected

But why @Inject cannot?
My classpath has javaee-web-api-8.0-sources.jar.
 
Saloon Keeper
Posts: 23041
157
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

peter tong wrote:

Himai Minh wrote:First of all, have you tried @Autowired instead of @Inject?
Secondly, do you have @ComponentScan (basePackages = "....") //where basePackage is the name of the base package such as "com.example" ?



Oh, yes, after change to @Autowired, apple1 can be injected

But why @Inject cannot?
My classpath has javaee-web-api-8.0-sources.jar.



Because @Inject is a CDI annotation, not a Spring annotation.

@Autowired tells Spring to consult its BeanFactory, construct the bean to be injected (if it's not already existing) and set the property value to reference that bean. @Inject references the CDI container, not Spring.
 
peter tong
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:

peter tong wrote:

Himai Minh wrote:First of all, have you tried @Autowired instead of @Inject?
Secondly, do you have @ComponentScan (basePackages = "....") //where basePackage is the name of the base package such as "com.example" ?



Oh, yes, after change to @Autowired, apple1 can be injected

But why @Inject cannot?
My classpath has javaee-web-api-8.0-sources.jar.



Because @Inject is a CDI annotation, not a Spring annotation.

@Autowired tells Spring to consult its BeanFactory, construct the bean to be injected (if it's not already existing) and set the property value to reference that bean. @Inject references the CDI container, not Spring.



"@Inject references the CDI container, not Spring.", yes, I try to use @Inject and test in wildfly server, then even @Inject can do what I want.
 
Do not meddle in the affairs of dragons - for you are crunchy and good with ketchup. Crunchy tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic