Dynamically load spring bean properties from database or Java Objects ) - spring

I have a scenario where I need to load properties from database or java object into beans.
Consider the example:
<bean id="ConfigAsstDemoBeanParent" class="gps.springconfig.DemoClass" lazy-init="true">
<property name="demoValueFromBean" value="demoValue"></property>
<property name="demoValueForKeyFromProperties" value="${DEMO_KEY}"></property>
</bean>
and instead of the ${DEMO_KEY} property placeholder, which loads a value from the property file, I need to load a value from the database, which I retrieve using a stored procedure from a Java class.
Please suggest me a mechanism which I can leverage for the above scenario. Currently I am investigating extending SpringMain and/or PropertyPlaceholderConfigurer class and write my own custom BootStrapper.
Also please suggest hints on writing a BootStrapper for the above mentioned scenario.

One of the cases where Java configuration seems to be a great alternative:
#Configuration
public class Config {
#Resource
private DataSource dataSource;
#Bean
#Lazy
public DemoClass configAsstDemoBeanParent() {
DemoClass demo = new DemoClass();
demo.setDemoValueFromBean("demoValue");
demo.demoValueForKeyFromProperties( /* query the database here */);
return demo;
}
}
Note that you can inject DataSource (or JdbcTemplate) to your #Configuration class providing it was defined elsewhere.

Related

Wiring multiple beans with the same dependency via Spring Boot #Configuration

In older Spring MVC apps, where you'd specify application.xml and declare your app's beans so that Spring DI could instantiate them and wire them together, you might have something like this:
<bean id="chargeFactory" class="com.example.myapp.ChargeFactory" />
<bean id="paymentService" class="com.example.myapp.DefaultPaymentService">
<ref id="chargeFactory"/>
</bean>
<bean id="chargeAuditor" class="com.example.myapp.ChargeAuditor">
<ref id="chargeFactory"/>
</bean>
Which might help wire up the following code:
public interface PaymentService {
public void makePayment(Payment payment);
}
public class DefaultPaymentService implements PaymentService {
#Autowired
private ChargeFactory chargeFactory;
#Override
public void makePayment(Payment payment, String key) {
Charge charge = chargeFactory.createCharge(key);
charge.doCharge(payment);
}
}
public class ChargeAuditor {
#Autowired
private ChargeFactory chargeFactory;
public void auditAllCharges(String key) {
List<Charge> charges = chargeFactory.getAllCharges(key);
// blah whatever
}
}
How do you accomplish the same bean wiring in Spring Boot with the #Configuration class? For example:
#Configuration
public class MyAppInjector {
#Bean
public ChargeFactory chargeFactory() {
return new ChargeFactory();
}
#Bean
public PaymentService paymentService() {
return new DefaultPaymentService(chargeFactory());
}
#Bean
public ChargeAuditor chargeAuditor() {
return new ChargeAuditor(chargeFactory());
}
}
This might work but introduces some issues:
It would force me to have to write value constructors for all my classes, which goes against everything I see in literally every tutorial/article I've come across. Plus, if I had to do that, then there's no real value to #Autowired anyways...
At this point I'm essentially doing "DIY DI", which is OK, except I'm trying to deliberately use Spring DI :-)
Every time I call chargeFactory() I'm getting a new (prototype-style) ChargeFactory instance. Maybe I want a singleton. Using this approach I have to hand-roll my own singleton implementation.
Sure, I can do all of this, but I feel like I'm flagrantly misusing/misunderstanding how #Configuration is supposed to be used, because it seems like I'm introducing a whole lot of DIY/homegrown code to solve something Spring DI should be able to do for me.
How would I reference the chargeFactory bean and wire it into both the "provider methods" for the paymentService and chargeAuditor beans? Again, looking for the Java-based #Configuration solution instead of writing an XML document to define the wirings.
I found this article which seems to be the only tutorial/documentation (surprisingly) on wiring Spring Boot apps via #Configuration (which leads me to believe there might be other/better methods...), but it does not address:
How to specify singleton vs prototype bean instantiation patterns
If multiple instances of a bean-class exist, how do I specify which instance gets wired into which dependency?
How do I get around not defining value constructors for all my classes, and just let Spring/#Autowired inject fields automagically?
When you call chargeFactory() , spring won't create new instance everytime. Give it a try and see. Same object will be returned. Anyways
You can do something like this.
#Bean
public PaymentService paymentService(ChargeFactory chargeFactory) { return new DefaultPaymentService(chargeFactory); }

Setting #Autowired(required=false) globally during spring-boot test

I can't get the classic XML config
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor">
<property name="requiredParameterValue" value="false" />
</bean>
to work in spring-boot test. I've tried the equivalent Java Style
#Bean
public AutowiredAnnotationBeanPostProcessor annotation() {
AutowiredAnnotationBeanPostProcessor annotation = new AutowiredAnnotationBeanPostProcessor();
annotation.setRequiredParameterValue(false);
return annotation;
}
also to no avail.
I'm just trying to find a solution to cherry-pick the beans I need for unit testing.
Here's the code I'm using in the test itself:
#Rollback
#SpringApplicationConfiguration({PackageServiceTestConfiguration.class, DatabaseConfiguration.class, MybatisConfiguration.class})
#ActiveProfiles("test")
public class PackageServiceTest extends AbstractTransactionalTestNGSpringContextTests {...}
The content of PackageServiceTestConfiguration.class (Database and mybatis are the config part of the apps itself what I need to perform my test
#Configuration
#ComponentScan(basePackages = "com.project.api.permission.service",
includeFilters = #ComponentScan.Filter(Service.class))
#ImportResource("classpath:testContext.xml")
public class PackageServiceTestConfiguration {}
testContext.xml contains some test fixtures and the failing AutowiredAnnotationBeanPostProcessor configuration
Thanks to #ComponentScan I can filter down to the package for which component to configure, but this is still not as convenient as XML where I can actually define each of the bean I need with a one liner.
I still need to make the #Autowired component not required globally for test, and I can't find a way to do this.

How to write simple Service Bean xml in Java for JPA

I can't find the syntax anywhere for this. I simply need to define 2 services in a new app. Previously they were defined in an xml file like:
<bean id="service1" class="somePackage.service.service1Impl" />
<bean id="service2" class="somePackage.service.service2Impl" />
I want to put these in to a java file. Is there some simple code I can write to convert these to Java? Thanks.
If you mean to convert their XML definition to Java definition, you will need to create a #Configuration class with #Bean annotated methods.
#Configuration
public class JavaConfig {
#Bean
public service1Impl service1() {
return new service1Impl();
}
}
I'm not sure if I asked this question correctly. I simply moved my xml to somewhere that the Class was looking.

Spring and manually created DAOs how to refactor to enable hasPermisions on such DAOs objects

I have got Dao implentation like this
public class EntityDao<T> {
private Class clazz;
private SessionFactory sessFactory;
public EntityDao(Class clazz, SessionFactory sessFactory) {
this.clazz = clazz;
this.sessFactory = sessFactory;
}
.... dao methods
}
and factory for retriving and storing particular dao
EntityBeanDaoFactory {
private HashMap<EntityDaoType, EntityDao> daoMap = new HashMap<EntityDaoType, EntityDao>();
// return dao from daoMap if exists a if not create it and put it in the map then return dao
public EntityDao createDao(EntityDaoType entityType) {
switch (entityType) {
case mySpecialDaoTYPE:
if (!daoMap.containsKey(entityType)) {
EntityDao<Type> mySpecialDao = new EntityDao(Type.class, sessFactory);
daoMap.put(entityType, mySpecialDao);
}
return daoMap.get(entityType);
}
}
now I want to annotate dao methods with #PreAuthorize("hasPermission()") but spring doesn't know about daos created this way and I am not able to refactor whole project at once so I have created dao ,on wich I need to use the annotation, inside aplicationContectxt.xml
<bean id="mySpecialDao" class="..EntityDao" >
<constructor-arg>
<value>myClass</value>
</constructor-arg>
<constructor-arg ref="sessionFactory" />
</bean>
inside the factory I have changet behavior for creating this particular dao like this
if (!daoMap.containsKey(entityType)) {
EntityDao<Class> dao = (EntityDao<Class>) AppContext.getApplicationContext().getBean("mySpecialDao");
daoMap.put(entityType, dao);
}
is there some better way how to make spring aware of my DAOs ? I mean is there way how to make Spring aware of manualy created instancess ?
You could do this using Spring AOP support using AspectJ. Read more here: http://static.springsource.org/spring/docs/3.2.x/spring-framework-reference/html/aop.html#aop-atconfigurable
After enabling this, Spring would be aware of any instances created of classes annotated with the Configurable annotation. Spring would then be able to recognize the PreAuthorize annotations.
Why do you need a factory to create DAOs? That's what the Spring application context is.
You look like you want to restrict the ability to call DAO methods using role based security. I think that's fine, and possible to do, but you need not restrict creation of the DAO. Create it using Spring and then restrict access. Your way is overkill and unnecessary.

What are advantages of spring DAOSupport

I read about Spring framework's DAOSupport classes. But I could not understand the advantages of these DAOSuport classes. In DAOSupport classes we call getXXXTemplate() method to get the specific template object and then use it further for DB access.
Even without extending DAOSupport we can inject XXXTemplate in our class.
Rest of this things will remain same.
Then what is advantage of extending DAOSupport class?
EDIT:- Adding example
Class extends spring's SimpleJdbcDaoSupport
public class JdbcDao extends SimpleJdbcDaoSupport {
public int create(Bb obj) {
getSimpleJdbcTemplate().update("insert into ..... ") //insert query
}
Bean of this class can be defined as :-
<bean id="jdbcDao" class="example.dao.support.JdbcDao">
<property name="dataSource"><ref local="dataSource"/></property>
</bean>
We can create a custom class without extending SimpleJdbcDaoSupport which will have property of type JdbcTemplate
public class MyDAO {
public myJdbcTemplate; // ant its getter and setter
public int create(Bb obj) {
getMyJdbcTemplate().update("insert into ..... ") //insert query
}
It's bean wil be defined as :-
<bean id="jdbcDao" class="MyDAO">
<property name="myJdbcTemplate"><ref local="jdbcTemplateBean"/></property>
</bean>
As you can see both classes do same thing. While extending SimpleJdbcDaoSupport we are injecting DataSource and without it we are injecting directly jdbcTemplate bean. That's it. No more difference.
So I do not see any much advantage with this much use of DAOSupport classes. Any additional functionality given by DAOSupport classes ?
When you use HibernateDAOSupport you can see the difference.
1. Config the transaction to optimize the performance of the application on the applicationContext :
select : read only
create/update : read and write.
You use one session only(with getHibernateTemplate() and the sessionFactory)
When we update some data on the database we do only merge the modifications whith the method impleted on HibernateDAOSupport.
There are many method already implemented on the DAOSupport and we can use this to our need.

Resources