Access DbContext in another class asp.net core 2 - dbcontext

In my Controller I have this
private readonly DbContext _context;
public CountryController(DbContext context)
{
_context = context;
}
How can I retrieve DbContext in other classes like static classes without passing as parameter to the method call

You can create new instances of your DBContext by creating services.
First you have to define an interface
public interface IMyService
{
void Test1();
}
then, you need to create the service class implementing the interface. Note that you request IServiceProvider to the Dependency Injector.
internal sealed class MyService : IMyService
{
private readonly IServiceProvider m_ServiceProvider;
// note here you ask to the injector for IServiceProvider
public MyService(IServiceProvider serviceProvider)
{
if (serviceProvider == null)
throw new ArgumentNullException(nameof(serviceProvider));
m_ServiceProvider = serviceProvider;
}
public void Test1()
{
using (var serviceScope = m_ServiceProvider.CreateScope())
{
using (var context = serviceScope.ServiceProvider.GetService<DbContext>())
{
// you can access your DBContext instance
}
}
}
}
finally, you instruct the runtime to create your new service a singleton. This is done in your ConfigureServices method in Startup.cs:
public void ConfigureServices(IServiceCollection services)
{
// other initialization code omitted
services.AddMvc();
services.AddSingleton<IMyService, MyService>();
// other initialization code omitted
}
Note that MyService needs to be thread safe.

Related

Autowiring not working in springboot application

I am trying to create a Spring boot application with JFrame. I can see my beans in applicationContext but they are not getting autowired. I am unable to find the reason for this issue. Can someone help me with this?
Here is the code:
JavauiApplication - it is showing both userManager and userNameRepository is beans
#SpringBootApplication
public class JavauiApplication implements CommandLineRunner {
#Autowired
private ApplicationContext appContext;
public static void main(String[] args) {
new SpringApplicationBuilder(JavauiApplication.class).headless(false).run(args);
java.awt.EventQueue.invokeLater(() -> new InputNameForm().setVisible(true));
}
#Override
public void run(String... args) throws Exception {
String[] beans = appContext.getBeanDefinitionNames();
Arrays.sort(beans);
for (String bean : beans) {
System.out.println(bean);
}
}
}
InputNameForm.java -> userManager coming null
#Component
public class InputNameForm extends javax.swing.JFrame {
/**
* Creates new form InputNameForm
*/
public InputNameForm() {
initComponents();
}
#Autowired
UserManager userManager;
private void submitButtonActionPerformed(java.awt.event.ActionEvent evt) {
userManager.setName(firstName.getText(), lastName.getText());
}
/**
* #param args the command line arguments
*/
public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(InputNameForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new InputNameForm().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JTextField firstName;
private javax.swing.JLabel firstNameLabel;
private javax.swing.JTextField lastName;
private javax.swing.JLabel lastNameLabel;
private javax.swing.JButton submitButton;
// End of variables declaration//GEN-END:variables
}
UserManager.java -> userNameRepository is coming null
#Component
public class UserManager {
#Autowired
UserNameRepository userNameRepository;
public void setName(String firstName, String lastName) {
userNameRepository.save(new UserName(firstName, lastName));
System.out.println(userNameRepository.findAllByFirstName(firstName));
}
}
It's a very common problem and it occurs because newcomers don't understand how the IoC container works.
Firstly, BeanDefinitionReader reads metadata about your beans from XML, Annotations(#Component, #Service etc), JavaConfig or Groovy script.
There are several BeanPostProcessor's which is responsible for reading all of these Spring annotation you're writing(#Autowired etc).
BeanFactory creates all BeanPostProcessor's then it creates all of your beans.
What happen if you create your bean with #Autowired dependencies via new operator? Nothing, because it isn't actually a bean. The object you created isn't related to IoC container. You may have the bean already in your ApplicationContext if you marked it with #Component(for example) but the object which was created via new operator wont be processed by Spring(annotations won't work).
Hope this helps.
PS: The lifecycle is simplified.
I had the same problem few days ago. What I undertood was that GUI builders like the one that comes with netbeans will automatically create components using new keyword. This means that those components won't be manage by spring. The code usually loks like this:
private void initComponents() {
jPanel1 = new javax.swing.JPanel(); //This component will not be managed by spring.
//...
}
You could use the following class provided here, to make it work.
#Component
public class BeanProvider {
private static ApplicationContext applicationContext;
// Autowires the specified object in the spring context
public static void autowire(Object object) {
applicationContext.getAutowireCapableBeanFactory().autowireBean(object);
}
#Autowired
private void setApplicationContext(ApplicationContext applicationContext) {
BeanProvider.applicationContext = applicationContext;
}
}
The top level SwingApp class:
#SpringBootApplication
public class SwingApp implements CommandLineRunner {
public static void main(String[] args) {
new SpringApplicationBuilder(SwingApp.class)
.headless(false).bannerMode(Banner.Mode.OFF).run(args);
}
#Override
public void run(String... args) throws Exception {
SwingUtilities.invokeLater(() -> {
MainFrame frame = new MainFrame();
frame.setVisible(true);
});
}
}
The MainFrame class:
public class MainFrame extends javax.swing.JFrame {
public MainFrame() {
initComponents();
}
private void initComponents() {
//Gui Builder generated code. Bean not managed by spring.
//Thus, autowired inside CustomPanel won't work if you rely on ComponentScan.
jPanel1 = new CustomJPanel();
//...
}
private CustomJPanel jPanel1;
}
The panel class where you want to autowire things:
//#Component //not needed since it wont work with gui generated code.
public class CustomJPanel extends javax.swing.JPanel{
#Autowired
private SomeRepository someRepository
public CustomJPanel(){
BeanProvider.autowire(this); //use someRepository somewhere after this line.
}
}
I have the same problem in a JavaFx project. Service and Component annotated classes were null in UI controllers even if it was shown in context that it was created. Below code worked for me
#Component
public class FxmlLoaderWithContext {
private final ApplicationContext context;
#Autowired
public FxmlLoaderWithContext(ApplicationContext context) {
this.context = context;
FXMLLoader fxmlloader = new FXMLLoader();
fxmlloader.setControllerFactory(context::getBean); //this row ensure services and components to be autowired
}
}
I think it returns null because you using command new to create object, such as new InputNameForm(). When creating object like that, the object isn't managed by Spring. That's why autowired not working.
The solution is registering your class as a bean.
You can use a class like in here.
#Component
public class BeanProvider {
private static ApplicationContext applicationContext;
public static void autowire(Object object) {
applicationContext.getAutowireCapableBeanFactory().autowireBean(object);
}
#Autowired
private void setApplicationContext(ApplicationContext applicationContext) {
BeanProvider.applicationContext = applicationContext;
}
}
And then, in your class InputNameForm constructor, call this:
class InputNameForm() {
BeanProvider.autowire(this);
...
}
And that's it. Spring will take care the rest.

Spring: Dynamically instantiate a bean from a factory

Is it possible to dynamically instantiate a bean in Spring using a factory?
I'd like to be able to instantiate some beans when a particular annotation is detected.
E.g.
#Retention ( RetentionPolicy.RUNTIME)
#Target (ElementType.TYPE)
#Import(CreateFooRegistrar.class)
public #interface CreateFoo {
String name();
}
public interface Foo {
String getName();
void setName(String name);
}
public class FooImpl implements Foo {
...
}
I can easily create beans directly using the concrete class:
public class CreateFooRegistrar implements ImportBeanDefinitionRegistrar {
#Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
AnnotationAttributes attributes = AnnotationAttributes.fromMap(importingClassMetadata.getAnnotationAttributes(CreateFoo.class.getName()));
String fooName = attributes.getString("name");
BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(FooImpl.class);
builder.addPropertyValue("name", fooName);
registry.registerBeanDefinition(fooName + "Foo", builder.getBeanDefinition());
}
}
But is there any way I can create a bean using the returned object from a factory?
E.g.
public class FooFactory {
public static FooImpl create(String name) {
FooImpl foo = new FooImpl();
foo.setName(name);
return foo;
}
}
The reason I ask is that all the beans I want to automatically create are typically instantiate via a factory. I don't own the factory code, so I'd prefer not to attempt to mimic its inner-workings myself.

Calling service layer directly from main class

I have a project structure controller -> response builder -> service layer.
Here service layer call the repository layer(database layer).
Everything is ok when follow the structure.
But,for testing i want to call the service layer directly from a java main class.
How can i do this??????
My controller:
#RestController
#RequestMapping("/ishmam")
public class IshmamAddressController {
#Autowired
#Qualifier("ishmamAddressBuilder")
IshmamAddressBuilder ishmamAddressBuilder;
#RequestMapping("/getAddress")
public IResponseDto<WebbCustomerAddressDto> getAllAddress(){
return ishmamAddressBuilder.getAllAddress();
}
}
My builder class is:
#Component("ishmamAddressBuilder")
public class IshmamAddressBuilder {
#Autowired
#Qualifier("ishmamAddressServiceImpl")
IshmamAddressInterface ishmamAddressService;
public IResponseDto<IshmamAddressResponseDto> getAllAddress(){
IResponseDto<WebbCustomerAddressDto> response=new
IResponseDto<WebbCustomerAddressDto>();
try{
//here i call the service layer
List<String> addressList=ishmamAddressService.getAllAddress();
}catch(Exception e){
throw e;
}
return addressList;
}
My service layer is:
#Service("ishmamAddressServiceImpl")
#Transactional
public class IshmamAddressServiceImpl implements IshmamAddressInterface {
#Autowired(required = true)
#Qualifier("webCustomerAddressRepository")
WebCustomerAddressRepository webCustomerAddressRepository;
#Override
public IshmamAddressResponseDto getAllAddress() {
List<WebCustomerAddress> aList = new ArrayList<WebCustomerAddress>();
List<WebbCustomerAddressDto> dtoWebCustomerAddressList = new
ArrayList<WebbCustomerAddressDto>();
IshmamAddressResponseDto ishmamAddressResponseDto=new
IshmamAddressResponseDto();
try{
aList =
address.getAllAddress(1);//Calling database layer
ishmamAddressResponseDto=//Doing something,not important for
//question
}
return ishmamAddressResponseDto;
}
Now what i want is to call the service layer directly from the main class:
public class Address{
public void getAddress(){
IshmamAddressServiceImpl i=new IshmamAddressServiceImpl();
List<String> list=i.getAllAddress();
}
public static void main(String[] args){
Address a=new Address();
a.getAddress();
}
}
This process is not working.How can i do this???????
As soon as you use spring, you must never use new to build an object that should be managed by spring.
So you could:
either do it manually, that means bootstrap an application context with the same intialization that it would have in your application and explicitely get a bean from it
ApplicationContext ctx = new AnnotationConfigApplicationContext(
Class<?>... annotatedClasses); // if it is the way you use it ...
IshmamAddressInterface i = ctx.getBean("ishmamAddressServiceImpl",
IshmamAddressInterface .class);
or use the Spring test framework that does it automatically for you in Junit tests
#WebAppConfiguration
#ContextConfiguration(classes = ...)
public class WebIntegrationTests {
#Autowired
#Qualifier("ishmamAddressServiceImpl")
IshmamAddressInterface ishmamAddressService;
...
#Test
public void testGetAddress() {
ishmamAddressService.getAddress();
...
}
}

Spring-Boot Access object created in main from RequestMapping

I am using spring-boot for implementing a REST server. Inside a function for request mapping, I have to create an object which is heavyweight, so for every REST call I have do it and it is slowing things down. Is it possible to create the object in main and access from the function?
#SpringBootApplication
public class MyClass{
public static void main(String[] args) {
/* I want to initialize the object here */
SpringApplication.run(MyClass.class, args);
}
}
#RestController
public class MyController {
#RequestMapping("/go/to/user/summary")
public Users[] getUsers(#RequestParam(value="length", defaultValue="10") int length) {
/* I want to use the object here */
}
You can create a bean in MyClass and then consume that bean in MyController. Spring will only create a single instance of the bean so you'll avoid the cost of creating it multiple times. Something like this:
#SpringBootApplication
public class MyClass {
public static void main(String[] args) {
SpringApplication.run(MyClass.class, args);
}
#Bean
public Heavyweight heavyweight() {
// Initialize and return heavyweight object here
}
}
#RestController
public class MyController {
private final Heavyweight heavyweight;
#Autowired
public MyController(Heavyweight heavyweight) {
this.heavyweight = heavyweight;
}
#RequestMapping("/go/to/user/summary")
public Users[] getUsers(#RequestParam(value="length", defaultValue="10") int length) {
// Use heavyweight here
this.heavyweight.foo();
// ...
return users;
}
}
I think You can use #Service for this approach. Service class is Singleton so if You create object inside it on startup application then You can use it requests in Your controllers class.
Example service:
#Service
public class MyService{
private MyLargeObject largeObject;
public MyLargeObject set(MyLargeObject largeObject){
this.largeObject = largeObject;
}
public MyLargeObject get(){
return largeObject;
}
}
Example controller:
#RestController
public class MyController{
#Inject
private MyService myService;
#RequestMapping("/go/to/user/summary")
public Users[] getUsers(#RequestParam(value="length", defaultValue="10") int length) {
MyLargeObject o = myService.get();
}
}
EDIT1:
If You want init Your largeObject directly in service You can use #PostConstruct annotation. For example:
#PostConstruct
public void init() {
// initialization Your object here
}

How to set Spring application context through setter or constructor in another class

I have a Spring class.
#Service("dbManager")
#Repository
#Transactional
public class DatabaseManager {
GenericXmlApplicationContext context;
#PersistenceContext
private EntityManager em;
public DatabaseManager(GenericXmlApplicationContext context) {
this.context = context;
}
....
} //end of class DatabaseManager
I have SpringUtil class
public class SpringUtil {
public static GenericXmlApplicationContext loadSpringContext(String springXmlFile) {
GenericXmlApplicationContext context = new GenericXmlApplicationContext();
context.load(springXmlFile);
context.refresh();
return context;
} //end of loadSpringContext()
} //end of class SpringUtil
Now in main i am using some thing like
public class Regulator {
public static void main( String[] args ) {
Test test = new Test;
test.start();
} //end of main()
} //end of class Regulator
Here is test class
public class Test {
public void start() {
String springXmlFile = "classpath:spring/plcb-app-context-xml.xml";
GenericXmlApplicationContext context = SpringUtil.loadSpringContext(springXmlFile);
} //end of reportStudent()
} //end of class Test
But i am getting error that
Could not instantiate bean class [...DatabaseManager]: No default constructor
found; nested exception is java.lang.NoSuchMethodException:
...DatabaseManager.<init>()
I want that when DatabaseManager class created then spring context taht i am creating using SpringUtil.loadSpringContext(springXmlFile) must pass to it. How can i do it ?
Thanks
Edit
-------------------
public void switchDataSource(DatabaseType databaseType) {
DriverManagerDataSource dataSource = null;
if (databaseType == DatabaseType.LEGACY) {
dataSource = (DriverManagerDataSource)context.getBean("myLegacyDataSource");
} else if (databaseType == DatabaseType.LS360) {
dataSource = (DriverManagerDataSource)context.getBean("myLs360DataSource");
}
LocalContainerEntityManagerFactoryBean emf = context.getBean("myEmf", LocalContainerEntityManagerFactoryBean.class);
emf.setDataSource(dataSource);
}
#SuppressWarnings("unchecked")
#Transactional(readOnly=true)
public List<Object> getResultList(String query, Class mappingClass) throws Exception {
Query emQuery = em.createNativeQuery(query, mappingClass);
return emQuery.getResultList();
} //end of findTraineeFromLegacy()
Actually i have these two methods in my DatabaseManager class. I am setting context so i can get bean from the context in switchDataSource() method.
One thing that i can do is remove instance filed and change the method to
public void switchDataSource(DatabaseType databaseType, GenericXmlApplicationContext context) {
....
}
This is why i am doing this ?
Thanks
Have a no-arg constructor for DatabaseManager.
Implements ApplicationContextAware in DatabaseManager. Spring will know this bean needs to be notified of the application context:
#Service("dbManager")
#Repository
#Transactional
public class DatabaseManager implements ApplicationContextAware {
private ApplicationContext context;
public DatabaseManager() {...}
#Override
public void setApplicationContext(ApplicationContext appContext) {
this.context = appContext;
}
} //end of class DatabaseManager
however, double think if you really need that injected. In most case you are doing something wrong.
Update:
For your requirement in your update, which you want your DB Manager to switch datasource base on input type, although it doesn't seems very normal doing such thing, you can simply have your DB Manager injected with a Map and do whatever you want, instead of injecting the app context.
#Service("dbManager")
#Repository
#Transactional
public class DatabaseManager implements ApplicationContextAware {
#Resource("&emfBean")
private LocalContainerEntityManagerFactoryBean emfBean;
#Resource("dbManagerDsMap")
private Map<DatabaseType, Datasource> dsMapping;
public DatabaseManager() {...}
public void switchDataSource(DatabaseType databaseType) {
emfBean.setDatasource(dsMapping.get(databaseType));
}
} //end of class DatabaseManager
However I strongly suggest you not doing such thing. Consider having individual entityManagerFactory for each DB you are connecting to, and use the correct emf to connect to DB, instead doing this weird switching logic. I believe it is not supposed to be changed after your application start.

Resources