I'm using spring + hibernate in my application.
I need to map the entities that are annoted by hibernate annotations.
I have this configuartion.
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>com.example.repositoryComment.Model1</value>
<value>com.example.repositoryControlUpload.Model2</value>
<value>com.example.repositoryCycleTicketSummary.Model3</value>
</list>
</property>
I'd like that the entities configuration stay in another file.
Exemplo:
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
filesThatContainsModels
</list>
</property>
This classes (Model1, Model2, Model3) are annoted by hibernate.
I don't use packagesToScan, because my warmup need to be fast.
There is way for configuration only the class that annoted, but not using packagesToScan?
Thanks
One option at build-time would be to take advantage of annotation processing.
Basically a custom annotation processor will scan your source files at build time and generate a list of all files found to be annotated with #Entity. It takes this list of classes along with an external property file that describes your static SessionFactory configuration and it generates your spring XML file as applicationContext-persistence.xml.
You then just make sure your main applicationContext.xml imports that file for runtime.
Another alternative would actually to use the packagesToScan property. But rather than do what a lot of developers do and point it to the root package of your application, provide the property with a more restrictive list of packages that represent exactly where it should look, helping it avoid inspecting unnecessary classes. For example:
<property name="packagesToScan">
<array>
<value>com.company.application.feature1.persistence</value>
<value>com.company.application.feature2.persistence</value>
...
</array>
</property>
But I honestly think you're over optimizing. If you have this type of bootstrap performance issues, there has to be something else going wrong here to give you cause for concern.
I have worked on a monolithic application with tens of thousands of class files where the scan pointed to the package root of the application and it didn't take any more than a few seconds to bootstrap the Hibernate persistence classes.
Related
Hy, i have a webapplication where i am trying to integrate JPA2(Hibernate)+Spring+Flyway
I added flyway to my ApplicationContext like this:
<bean id="flyway" class="org.flywaydb.core.Flyway" init-method="migrate">
<property name="baselineOnMigrate" value="true" />
<property name="dataSource" ref="dataSource" />
</bean>
Theoretically this works fine and updates the schema with scripts that i save under db/migration. So far so good.
The one problem that is left for me is that if i change something (e.g. adding a String field to an Entity) the application won't even get this far because Hibernates Schema-Validator will throw something like this: Caused by: org.hibernate.HibernateException: Missing column: showCaseField in demo.testEntity. This happens because i have set "hibernate.hbm2ddl.auto" to "validate"
Now i have read about Hibernate failing to recognize perfeclty valid schemas in some (rare?) cases and i MAY (or not) reach a point someday where i disable this feature altogether. But as of now i actually like the extra-validation and don't want to turn it off.
Is it possible to integrate Spring and Flyway while still keeping Hibernates-Schema-Validation? I guess this could be a problem, because Flyway probably depends on a DataSource-bean or something and in conclusion requires the applicationContext to be completely initialized, which in turn Hibernate prevents because of the schema mismatch..
Any ideas?
Found the answer now. Basically all you have to do is letting your entityManagerFactory-bean depend on your Flyway bean (there's an attribute for that). Now Flyway (and in turn your dataSource) is initialized first and the Flyway-Scripts are executed before Hibernates schema-validation
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
depends-on="flyway"> ....
</bean>
<bean id="flyway" class="org.flywaydb.core.Flyway" init-method="migrate">
<property name="baselineOnMigrate" value="true"/>
<property name="dataSource" ref="dataSource"/>
</bean>
Mine is a old legacy project and I am using Spring 1.2.5 . I am trying below code to apply aop over one of my business classes :
<bean id="hijackBeforeMethodBean" class="com.mycom.myapp.common.logging.LoggingAdvice" />
<bean id="demoServiceProxy"
class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target">
<ref local="demoDelegate"></ref>
</property>
<property name="interceptorNames">
<list>
<value>hijackBeforeMethodBean</value>
</list>
</property>
</bean>
</beans>
In addition to this :
I firstly tried this over controllers ,but it didnt work.
I followed this link
http://www.javatpoint.com/spring-aop-example
but this asks me to inject or use the proxyfactory bean. A sper my current code I am injecting delegate beans in the controllers ,so in tht case I might have to change all my bean ids.
Please suggest some solution to this.
I am new to spring.I know that AfterAdvice will cause the after method to execute whether target method completes or exits with exception, but i am not able to find out any example for it.
As AfterAdvice is a marker interface, I don't know which method i need to define in it's implementation class.
Thanks,
You do not have to implement those interfaces directly. Instead, you use either
Use #After annotation to mark the method you want to it to be called.
Use spring xml bean configuration aop:advice to declare an after advice method
However, if you choose to use ProxyFactoryBean
As you indicate that you want to use ProxyFactoryBean, you can declare the xml like this
<bean id="interceptor"
class="yourimplementation">
</bean>
<bean id="setterAdvisor"
class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="advice">
<ref bean="interceptor"/>
</property>
<property name="patterns">
<list>
<value>.*set.*</value>
</list>
</property>
</bean>
<bean id="person"
class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces" value="com.mycompany.Person"/>
<property name="target" ref="personTarget"/>
<property name="interceptorNames">
<list>
<value>setterAdvisor</value>
</list>
</property>
</bean>
For the java implementation, there is no use to implement Advice interface. You should either implement ThrowingAdvice or AfterReturningAdvice. Refer to this for more info.
For more information, you can refer to a couple tutorial guides on Spring AOP and play around with it to get a sense.
I have a project with spring and hibernate in GWT,
I am using below applicationcontext.xml,
I was just looking for some best approach of making this file
like all the annotated classes below i.e entity.user, entity.secretQuestion and many more , they all get called when my application runs even if i don't need them , which i guess makes my application quite slow,
so is it possible that only the class which i am calling is getting load in applicationcontext.xml and if yes then would it be a better approach as well ?
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>com.cricsite.persistence.entity.User</value>
<value>com.cricsite.persistence.entity.SecretQuestion</value>
</list>
</property>
</bean>
<bean id ="ManagerAdmin" class= "com.persistence.MySQLRdbHelper">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
You might be looking for something called "lazy loading"
Please take a look at these threads;
Help needed with Spring/Hibernate Lazy-loading
What is lazy loading in Hibernate?
how does spring allow for lazy-loading?
I'm about to extend org.springframework.context.support.AbstractMessageSource that will allow me to dynamically add and edit messages in Spring. I'm planning on storing these values in a database. Is there something out there that does this already? Is there a different approach I should think of?
Here are the requirements:
I have to be able to add messages
I have to be able to edit messages
These adds and edits should take place immediately
Sure.
Develop custom MessageSource and set it as parent to existing (for example based on property files).
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames">
<list>
<value>message/messages</value>
</list>
</property>
<property name="parentMessageSource">
<bean class=com.example.DatabaseMessageSource"/>
</property>
</bean>