IntelliJ IDEA do not show injected dependencies when using #Resource - spring

IntelliJ IDEA do not show spring injected dependencies when using #Resource:
#Resouce
List<People> people;
Idea can not navigate to dependencyies when using #Resource,but if use #Autowired,it works!
How make #Resource works as #Autowired?

When using the #Resource annotation, you can specify the name attribute of the annotation and then the IntelliJ navigation works again:
#RestController
public class HelloWorldRestController {
// navigatable
#Resource(name = "httpServletRequest")
private HttpServletRequest request;
// not-navigatable
#Resource
private HttpServletRequest request1;
// ... more
}

Related

How to use application.properties in org.quartz.Job Class

I have created a spring boot application to implement Quartz scheduler. In Job class, I want to use some property from application.properties. How to inject that?
I have tried below but getting null:
#Component
public class ScheduleJob implements org.quartz.Job {
private static final Logger LOGGER = LogManager.getLogger(ScheduleJob.class);
public ScheduleJob() {
}
#Value("${ijobs.service.url}")
private String ijobsUrl;
public void execute(JobExecutionContext context) throws JobExecutionException {
LOGGER.info("Job exceution starts--->");
System.out.println("-------------------"+ijobsUrl);
Spring requires annotating the class with #Component for it to manage it (including loading any properties into the class). If you use "new" to create the object, then it is outside Spring's control and it won't inject the dependencies/values into it.
On side note, there is native support for Quartz if using Spring Boot: https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-quartz.html
Firstly, the ScheduleJob class should be annotated with #Component for spring to manage it. Secondly, if you want any property to be injected you could do it in a similar way to how you are injecting value of ijobsUrl.
put your required property in application.properties
#Value("${my.property}")
private String myProperty

Spring #Autowired annotaion

Spring #Autowired
I have a doubt on Spring #Autowired annotation.Please Help...
In Spring mvc ,when I tried #Autowired in this order
Controller--->Service--->Dao
ie,In Controller I autowired Service Class Object , In Service Class Autowire Dao Object.
This Injection chain works perfectly.
Similliarly In strutrs2+Spring ,I applied #Autowired Annotation in this way
Action--->Service-->Dao
This Injection chain also works fine.
If I call a funtion from outside this chain (eg:Custom Taglib class (from jsp)) to funtion in Service class Then in this Service class the Autowired dao object is null(ie,this call braks the chain).
My questions is
Is this #Autowired works in a Injection chain Only?
Beans that have #Autowired fields only have them set if they are sent through the Spring Bean Postprocessor -- that is, like you said, if you autowire them yourself. That is a big reason that constructor injection is much more preferred than field injection. Instead of doing
#Service
public class MyService {
#Autowired
private MyDao dao;
...
}
you should do
#Service
public class MyService {
private final MyDao dao;
#Autowired
public MyService(MyDao dao) {
this.dao = dao;
}
}
That way, when you're in a situation where you can't rely on a service to be post-processed (as in your case of using the jsp tag library), you can simply instantiate a new instance with a MyDao object and be on your merry way.

Regarding Spring's #Autowired and Wicket's #SpringBean

I am currently looking into integrating Mockito and JUnit into my Wicket/Spring/Hibernate project and have found a tutorial on how to do this using annotations.
Trouble is I am unfamiliar with #Autowired and after a look on google I am finding it hard to see the difference between this annotation and the #SpringBean annotation.
Are they one in the same or is there a difference I should be aware of ?
My code to offer some context to this question:
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(locations = {"classpath:applicationContext.xml"})
#TransactionConfiguration(transactionManager = "txManager", defaultRollback = false)
public class TestHome
{
private WicketTester tester;
#Autowired
private ApplicationContext ctx;
#Autowired
private WebApplication webApplication;
#Before
public void setUp() {
tester = new WicketTester(webApplication);
}
#Test
#Transactional
#Rollback(true)
public void testRenderHomePage() {
tester.startPage(Home.class);
tester.assertRenderedPage(Home.class);
tester.assertComponent("home", Home.class);
}
}
If you use Wicket SpringComponentInjector, it uses its own injection. The #Autowired annotation is a Springframework annotation, but Wicket SpringComponentInjector ignores that. So the Wicket annotation is #SpringBean that marks a field to be autowired (injected) by Spring bean or component that has to exist in Spring context.
In you code snippet you use the SpringJUnit4ClassRunner runner, so your fields are injected by Spring, so it is correct.
See an example, how to use SpringComponentInjector at How can I get a Spring bean injected in my custom Wicket model class?

#ManagedProperty equivalent in Spring

I am using Spring for my DI.
Is there an equivalent of #ManagedProperty? I want to inject the value from one view scoped bean into another one on the next page.
e.g
#Component
#Scope("view")
public class Page1Bean(){
private String value;
}
#Component
#Scope("view")
public class Page2Bean(){
#ManagedProperty(value = #{page1Bean}") //doesnt work in Spring
private Page1Bean bean;
}
#Resource or #Autowired should work. #Resource is the Java EE implementation, #Autowired is the spring specific annotation. I can't find the reference now, but it seems like I read once to prefer #Resource over #Autowired.
here's a blog post I found that talks about #Inject vs. #Resource vs. #Autowired
http://blogs.sourceallies.com/2011/08/spring-injection-with-resource-and-autowired/#more-2350

#Autowired in Spring MVC #Controller does not work on a private field

I have a Spring MVC Controller in a very XML-slimmed application, we use a lot of annotations and as little config as possible. The Controller is working and it also has a number of resource values injected. But I've experienced a really strange behavior with this controller; annotated private fields referencing other components will not be injected.
This will not work.
#Controller
public class EntranceUnitController {
#Value("${remote.baseUrl}")
private String baseUrl = "http://localhost";
#Value("${remote.port}")
private String pushPort = "8080";
#Autowired
private HttpClientFactory httpClientFactory;
...
It seems that the httpClientFactory isn't there yet when the private fields are set, if I set a break point to inspect the value there is of course null set when the controller is created.
BUT, if I make a setter for the component and annotate the set-method instead of the private field the controller works as expected.
#Controller
public class EntranceUnitController {
#Value("${remote.baseUrl}")
private String baseUrl = "http://localhost";
#Value("${remote.port}")
private String pushPort = "8080";
private HttpClientFactory httpClientFactory;
#Autowired
public void setHttpClientFactory(HttpClientFactory httpClientFactory) {
this.httpClientFactory = httpClientFactory;
}
...
To me this is really annoying. Isn't the auto wiring injection for annotated values happening at the same time regardless injection point? I.e. why does it matter that the object is injected with a setter? I thought that private field injections are directly followed by constructs and setters, me start to think I'm wrong in that case...
Seems like your dependencies are in fact injected, you are just putting a breakpoint in the wrong moment (too early) and the dependencies aren't injected yet, despite class being already created.
Remember that, unless you are using constructor injection, the first place where you can use injected dependencies is #PostConstruct method:
#Controller
public class EntranceUnitController {
#Autowired
private HttpClientFactory httpClientFactory;
#PostConstruct
public void init() {
httpClientFactory //should not be null
}

Resources