Last active
January 9, 2019 13:51
-
-
Save mageddo/6dff307d651a49682a17b926b8a27dcb to your computer and use it in GitHub Desktop.
Spring Self Injection On Constructor Based Beans
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.springframework.beans.BeansException; | |
import org.springframework.context.ApplicationContext; | |
import org.springframework.context.ApplicationContextAware; | |
import org.springframework.context.annotation.Configuration; | |
@Configuration | |
public class ApplicationContextProvider implements ApplicationContextAware { | |
private static ApplicationContext context; | |
public static ApplicationContext context() { | |
return context; | |
} | |
@Override | |
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { | |
context = applicationContext; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.mageddo.sqldatapartitioning.service; | |
import com.mageddo.sqldatapartitioning.ApplicationContextProvider; | |
import com.mageddo.sqldatapartitioning.dao.SkinPriceByDayDAO; | |
import com.mageddo.sqldatapartitioning.entity.SkinPriceEntity; | |
import com.mageddo.sqldatapartitioning.enums.SkinPriceType; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import org.springframework.stereotype.Service; | |
import org.springframework.transaction.annotation.Propagation; | |
import org.springframework.transaction.annotation.Transactional; | |
import java.time.LocalDate; | |
@Service | |
public class CustomerService { | |
private final CustomerDAO customerDAO; | |
public SkinPriceByDayService(CustomerDAO customerDAO) { | |
this.customerDAO = customerDAO; | |
} | |
public void updateCustomers() { | |
for(var customer: customerDAO.getCustomers()){ | |
self().updateCustomer(customer); | |
} | |
} | |
@Transactional | |
public updateCustomer(Customer customer){ | |
customer.setLastUpdate(LocalDateTime.now()); | |
customerDAO.update(customer); | |
} | |
protected SkinPriceByDayService self(){ | |
return ApplicationContextProvider.context().getBean(getClass()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment