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
posted 5 days ago
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: 2006
13
posted 5 days ago
1
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
posted 4 days ago
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.
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.
Some people, when well-known sources tell them that fire will burn them, don't put their hands in the fire.
Some people, being skeptical, will put their hands in the fire, get burned, and learn not to put their hands in the fire.
And some people, believing that they know better than well-known sources, will claim it's a lie, put their hands in the fire, and continue to scream it's a lie even as their hands burn down to charred stumps.
peter tong
Ranch Hand
Posts: 257
posted 2 days ago
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.
Any sufficiently advanced technology will be used as a cat toy. And this tiny ad contains a very small cat: