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:
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" ?
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.
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: