Here is my AppConfig:
#Configuration
#EnableWebMvc
#ComponentScan
class AppConfig{
#Bean("myname")
fun name(): Name = Name("Quang")
}
Data class:
data class Name(val value: String ="")
My Controller Class:
#Controller
#RequestMapping("/")
class Main{
#RequestMapping("/")
#ResponseBody
fun index(#Autowired #Qualifier("myname") name: Name): Name {
//this return ""
return name
}
}
The result is "" instead of "Quang".
But now if I inject it from field like this, it works fine:
#Controller
#RequestMapping("/")
class Main{
#Autowired(required = true)
#Qualifier("myname")
lateinit var name:Name
#RequestMapping("/")
#ResponseBody
fun index(): Name {
//this return "Quang" as expected
return name
}
}
So can you explain why #Autowired doesn't works when I use it in method parameter
According to documentation:
Marks a constructor, field, setter method or config method as to be
autowired by Spring's dependency injection facilities.
In your first example, Spring will not autowire anything.
Related
Per the doc, #RestController is just a convenience annotation which combines #Controller and #ResponseBody and #GetMapping is a composed annotation that acts as a shortcut for #RequestMapping(method = RequestMethod.GET), which means #GetMapping should work well with both #RestController and #Controller
In fact #GetMapping only works with #RestController.
#RestController
public class HelloController {
#GetMapping("/")
public String hello(){
return "hello";
}
}
while
#Controller
public class HelloController {
#GetMapping("/")
public String hello(){
return "hello";
}
}
doesn't work.
I know I can use #RequestMapping(value = "/", method = RequestMethod.GET) instead, I'd just like to know why. Could someone give a clue?
#Controller
public class TestController {
#GetMapping("/")
#ResponseBody
public String hello() {
return "hello";
}
}
Add the #ResponceBody annotation then, it will work.
The #Controller annotation indicates that the class is a “Controller”
e.g. a web controller
The #RestController annotation indicates
that the class is a controller where #RequestMapping methods assume
#ResponseBody semantics by default i.e. servicing REST API.
While Trying to autowire a service class to my restcontroller in springboot, it is throwing build error unexpected token #, at #Autowired notation
This is my controller class.
#RestController
class RestAPIController{
#Autowired
private getTextBooks service
#RequestMapping(value = "/textbooks", method = RequestMethod.GET)
Interface for autowiring
import org.springframework.stereotype.Component
#Component
public interface Books{
public String getText(String name)
}
Class implementing Interface
#Componet
Class getTextBooks implements Books{
#Override
def getText(String name){
return "Text Book Name is" + name
}
}
Unable to Autowire in Controller
Name your class in capital letter like :
#Componet
Class GetTextBooks implements Books{
#Override
def getText(String name){
return "Text Book Name is" + name
}
}
It's java convention to name class by starting with Capital case letters.
I'm writing a specification class in spock framework.
#ContextConfiguration(classes = [MyServiceImplementation.class])
class MyServiceSpecification extends Specification {
#Autowired
private MyService myServiceImplementation
def " " {
//code
}
}
The class MyServiceImplementation is annotated #Service. I'm not using XML configuration. MyServiceImpl is an implementation of the interface: MyService.
Why is the autowired object myServiceImplementation null?
I tried using ComponentScan and it still didn't work.
First, you need to have both spock-core and spock-spring on the classpath. Second, #ContextConfiguration(classes= takes a list of configuration classes, not bean classes.
#ContextConfiguration(classes = [MyConfig.class])
class MyServiceSpecification extends Specification {
#Autowired
private MyService myServiceImplementation
def " " {
//code
}
}
// You could also define #ComponentScan here
class MyConfig {
#Bean
MyService myService() {
return new MyServiceImplementation();
}
}
I'm trying to autowire a service in my rest controller like these:
rest controller:
#ApplicationPath("/greetings")
#Component(immediate = true, service = Application.class)
public class RestControllerApplication extends Application {
#Autowired
private MyService myService;
public Set<Object> getSingletons() {
return Collections.<Object>singleton(this);
}
#POST
#Path("/getUploadType")
#Produces("application/json")
public JsonObject getUploadType() {
...
myService.findUploadTypes();
...
}
}
service:
#Component
public class UploadService {
private static final Logger log = Logger.getLogger(UploadService.class);
#Autowired
private OneDAO oneDAO;
#Autowired
private TwoDAO twoDAO;
...
}
but in my rest controller, uploade service is null. Why?
Spring uses its own set of annotations. Instead of #Path plus #[HTTP method] you should use #RequestMapping.
You can find an example here
There is also an extended example here
I have got access to my bean, with these few line of code:
WebApplicationContext context = ContextLoader.getCurrentWebApplicationContext();
MyService myService = context.getBean(MyService.class);
You are declaring a UploadService as #Component but trying to autowire a MyService instance in your controller...
There are two options: you can declare correct service type in your controller or you can make UploadService inheriting from MyService.
I have one problem with #Autowire annotation in my Kotlin code.
There is one piece of code that works perfectly
#Controller
open class PaymentController {
#Autowired
lateinit var autowiredBean: AutowiredBean
#RequestMapping(value = "/SomePage", method = arrayOf(RequestMethod.GET))
fun somePage(#RequestParam("param") param: Int): ModelAndView {
// some code
}
}
But after adding some security checking, #Autowire annotation stops working
#Controller
open class PaymentController {
#Autowired
lateinit var autowiredBean: AutowiredBean
#RequestMapping(value = "/SomePage", method = arrayOf(RequestMethod.GET))
#PreAuthorize("hasPermission('MODULE', 'FINANCE')")
fun somePage(#RequestParam("param") param: Int): ModelAndView {
// some code
}
}
It's just doesn't initialize. I tried to initialize it by my controller constructor but had got the same result.
Any ideas?