AutoWired Setters rather than AutoWired instance variables in Spring
Tag : java , By : barefootChild
Date : March 29 2020, 07:55 AM
may help you . Some observations from me (actually 3 years in Java EE): Advantages:
|
@Autowired service is null but I need create new instance
Tag : java , By : Praetoriansentry
Date : March 29 2020, 07:55 AM
wish of those help I'm developing an app with Spring boot, I'm using the MVC model. I have an entity called A, which has its controller, service and repository. all right up here. , If you need to do some task periodically: @SpringBootApplication
@EnableScheduling
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application .class, args);
}
}
@Component
class Runner {
@Autowired
private Service service;
@Scheduled(cron = "0 */2 * * * ?") // execute every 2 hours
public void run() {
// put your logic here
}
}
|
@Autowired not work inside class that class instance create using reflection
Tag : java , By : user119985
Date : March 29 2020, 07:55 AM
should help you out Spring can't inject anything in an instance that you create yourself with new or via reflection. One of the ways to do what you want would be to request the beans from the application context, something along the following lines:
@Autowired
private ApplicationContext applicationContext;
public void createUser(Class<?> beanClass) {
ILogic logic = applicationContext.getBean(beanClass);
logic.saveUser();
}
|
Is there way to reload autowired instance or replace autowired behavior in spring
Tag : java , By : Patastroph
Date : March 29 2020, 07:55 AM
wish helps you Your approach is very wrong. Autowiring is for connecting dependencies at startup. (It is actually discouraged these days, in favour of constructor argument injection.) What you probably need is to have a @Service which retrieves the data model from the remote service. You then inject this service in the classes that need it to get the model. @Service
public class ModelService {
private final RestTemplate restTemplate;
public ModelService(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
@Cacheable(value = "model", key = "#root.methodName")
public Model getModel() {
MetadataReader metadataReader = new MetadataReader();
String prismFormatJson = null;
if (!isHasLatestTransformedJson()) {
prismFormatJson = metadataReader.transformToPrismJson(restTemplate, environment);
setLastGoodPrismConfiguration(prismFormatJson);
} else {
prismFormatJson = getLastGoodPrismConfiguration();
}
if (model != null) {
return model;
} else {
return metadataReader.createModelForPrism(prismFormatJson);
}
}
//... the rest of the code
}
<config
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xmlns='http://www.ehcache.org/v3'
xmlns:jsr107='http://www.ehcache.org/v3/jsr107'
xsi:schemaLocation="
http://www.ehcache.org/v3 http://www.ehcache.org/schema/ehcache-core-3.0.xsd
http://www.ehcache.org/v3/jsr107 http://www.ehcache.org/schema/ehcache-107-ext-3.0.xsd">
<service>
<jsr107:defaults>
<jsr107:cache name="model" template="model-cache"/>
</jsr107:defaults>
</service>
<cache-template name="model-cache">
<expiry>
<ttl unit="minutes">10</ttl>
</expiry>
</cache-template>
</config>
|
How can i create constructor with autowired and non autowired fields simultaniosly?
Date : January 02 2021, 06:48 AM
like below fixes the issue You need to create your bean explicitly in the configuration. You don't need any autowiring inside your class: public class EditSite{
private final A a;
private final B b;
private final C c;
private Site site;
public EditSite(Site site, A a, B b , C c) {
this.site=site;
this.a = a;
this.b = b;
this.c = c;
}
public void run(){ check(); }
public void check(){}
}
@Configuration
public class EditSiteConfig {
@Bean
public EditSite editSite(A a, B b, C c){
Site site = getSite(); //since site is not a bean, you need to get it manually
return new EditSite(site, a, b, c);
}
}
|