With Spring there is the concept of Profile annotations...
which are great when one wants to build up components for development/test apart from production setups. How do people handle this in EJB 3+?
Seen in http://www.amazon.com/Real-World-Java-Patterns-Rethinking-Practices/dp/1300149310 that CDI injection can be used (i.e. Logger Injection). Basically one creates a Configuration class that produces settings that are injected into other classes that produce (#Alternative classes) based of these settings. Not as smooth as Spring profiles but would work.
Any other ways in EJB/CDI?
Related
TL;DR version:
What's the best way to use the .Net Core IConfiguration framework so values in a single appsettings.json file can be used in multiple projects within the solution? Additionally, I need to be able to access the values without using constructor injection (explanation below).
Long version:
To simply things, lets say we have a solution with 3 projects:
Data: responsible for setting up the ApplicationDbContext, generates migrations
Services: class library with some business logic
WebApi: REST API with a Startup.cs
With this architecture, we have to use a work-around for the "add-migration" issue that remains in Core 1.0.0. Part of this work-around means that we have a ApplicationDbContextFactory class that must have a parameterless constructor (no DI) in order for the migration CLI to use it.
Problem: Right now we have connection strings living in two places;
ApplicationDbContextFactory for the migration work-around
in the WebApi's "appsettings.json" file
Prior to .Net Core, we could use ConfigurationManager to pull connection strings for all solution projects from one web.config file based on the startup project. How do we use this new IConfiguration framework to store connection strings in one place that need to be used all over the solution? Additionally, I can't inject into the ApplicationDbContextFactory class' constructor... so that further complicates things (more-so since they changed how the [FromServices] attribute works).
Side note: I would like to avoid implementing an entire DI middleware just to get attribute injection, since Core includes it's own DI framework. If I can avoid that and still access appsettings.json values, that would be ideal.
If I need to add code let me know, this post was already long enough, so I'll hold off on examples until requested. ;)
currently working with a very large application to test (several custom programs, running in a distributed environment), and has built up a very large set of automated test cases for regression and feature testing. These tests are large and there are a lot, so full test runs are dispatched across many machines, the results gathered, and then imported into a custom web app.
technologies: java/selenium/ant/testng/jenkins
reports: testng,reportng,xslt
how to store results in database(eg: mysql)?
Create a custom Reporter Listener by extending the org.testng.TestListenerAdapter and override the onTestSuccess, onTestFailure and onTestSkipped methods and log the results of the tests there to mySQL. After that you have to add your custom Reporter as a Listener.
You can find on the TestNG's website, how can you define a custom Listener:
http://testng.org/doc/documentation-main.html#testng-listeners
And here you can find how can you override the TestListenerAdapter:
http://testng.org/doc/documentation-main.html#logging-listeners
I have an application running on JBoss AS 7.1.1. This app uses some resources of CDI specification as interceptors, injection, etc. The architecture of my app is very simple with the structure below:
view (xhtml and facelets)
controller (managed beans with #Named, except in the ViewScoped)
model (divided in two layers, service and dao)
service (with #Stateless annotation, here I use an interceptor that I created to manage the transactions with database, because I use native JDBC)
dao
I need to create some scripts to test the application service layer, injecting the service implementation and invoking the business methods.
I believe that this architecture is very common. I'm sorry for my english.
Can someone help me, please?
Thanks!
If you want to test your full container, you probably want Arquillian. If you want to do Unit testing with mocks, start a standalone weld container in your test using weld-se.
new Weld().initialize().instance().select(YourClassName.class).get();
You can substitute your mock objects by using alternatives in your beans.xml. You can also use CDI-Unit which simplifies the process a bit.
I am developing an AS3 application which uses a Singleton class to store Metrics in Arrays. It's a Singleton because I only ever want one instance of this class to be created and it needs to be created from any part of the app.
The difficulty comes when I want to unit test this class. I thought adding public getters and setters would enable me to unit test this properly and would be useful for my app. I have read that changing to a Factory pattern will enable unit testing or using Inversion of control. This would of course make it more flexible too. I would like to know of people's thoughts on this matter as there are SO many conflicting opinions on this!
Thanks
Chris
If you're using an IoC framework, then make your consumers require an instance of the service in their constructor, and configure the IoC framework to only build one instance and keep handing it out to all requests in the entire application. This is the default behavior of Castle Windsor in my experience.
For unit testing you can use a Mock object in place of the real object.
What is the best way to manage property sets to apply to EJB, and easily be able to vary them between machines/environments (e.g. DEV, TEST, PROD)? For example, is there a way to configure your EJB properties on the App Server (which guarantees you can vary them by machine/environment).
Specifically:
1) I have a Singleton EJB which needs certain properties set (environment) specific. Is there annotation(s) which are used to tell the EJB Container where to look up those properties and will automatically apply them to the bean?
2) What is the best way to manage different property sets, i.e. dev, test, prod, so that the J2EE app is portable between servers, and you can seamlessly manage the properties specific to each server?
If there are any good documentation links - let me know. I've Googled around and seen nothing directly to the points above.
I use a singleton helper class "PropertiesHelper" which has a Properties member and reads from an xml configuration file upon the first property access attempt. The helper encapsulates the entire set of configuration settings and prevents them from being read from disk more than once.
The file location is specified in my java opts and read in by PropertiesHelper using System.getProperty()
As for system properties annotations, I don't believe Java supports this natively, but if you're so inclined you may want to look at some AOP/Dependency Injection frameworks like Google Guice which are better at this "cross-cutting".