Enlightensoft's Blog

Helping in your each step

  • Categories

  • Authors

Archive for the ‘Spring’ Category

Read property value in Java file using sprng 3 annotation

Posted by Pankil Patel on May 6, 2013

Step 1: Add your property bean definition in applicationContext.xml:

<bean id=“myPropValue”     class=“org.springframework.beans.factory.config.PropertiesFactoryBean”>
               <property name=“location”value=“classpath:myPropertyFile.properties” />
</bean>
 

Step 2: Read Property by constructor injection of property bean into Spring service.:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 

@Service(“myService”)

public class MyServiceImpl implements MyService {

private Properties allValueProperties;

       @Autowired
       public MyServiceImpl (Properties myPropValue) {
              this.allValueProperties = myPropValue;
       }
       @Override
       public void springWebMethod() throws Exception {
                   String valueFromProperty = this.allValueProperties.getProperty(“enlighten.my.default.value.url”);
       }

}

Note: If you don’t want to inject property bean in to your each spring service bean where it is required, alternatively you can inject it into one common bean and create static map from it or create enum from that properties and use that static map or enum in your any class of your application.

Posted in Spring | Tagged: , , , , , , | Leave a Comment »

How to Use Hibernate Annotation & .hbm.xml files at single time on a Same project?

Posted by Pankil Patel on June 11, 2012

Use sessionFactory as org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean in its configuration file instead of org.springframework.orm.hibernate3.LocalSessionFactoryBean

Sample configuration is as below:

<bean id=”sessionFactory”
class=”org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean”>
<property name=”annotatedClasses“>
<list>
<value>co.cc.enlightensoft.model.businessobject.Product</value>
</list>
</property>
<property name=”mappingResources“>
<list>
<value>co/cc/enlightensoft/model/businessobject/Category.hbm.xml</value>
<value>co/cc/enlightensoft/model/businessobject/User.hbm.xml</value>
</list>
</property>

<property name=”hibernateProperties”>
<props>
<prop key=”hibernate.dialect”>org.hibernate.dialect.MySQLDialect</prop>
<prop key=”hibernate.show_sql”>true</prop>
<prop key=”hibernate.cglib.use_reflection_optimizer”>true</prop>
<prop key=”hibernate.cache.provider_class”>org.hibernate.cache.HashtableCacheProvider</prop>
</props>
</property>

<property name=”dataSource”>
<ref bean=”dataSource” />
</property>

</bean>

Posted in Hibernate, Spring | Tagged: , , , , , , , , , | Leave a Comment »

How to call Spring Bean Method from JSP using scriptlet???

Posted by Pankil Patel on July 7, 2011

If you just want to invoke any Spring Bean Method from JSP using scriptlet here is the code for that:

Please not that here Spring Bean is:

Posted in JSP, Spring | 1 Comment »

How to use Properties File in Spring

Posted by Pankil Patel on August 22, 2010

Style 1: Configure property file to be used in spring xml

# Add new bean entry into your application context.

<bean id=”propertyConfigurer”
class=”org.springframework.beans.factory.config.PropertyPlaceholderConfigurer”>
<property name=”location”>
<value>classpath:abcConfig.properties</value>
</property>
</bean>

# Create new property file into src folder
abcConfig.properties

# Use that property file value

<property name=”user”>
<value>${jdbc.user}</value>
</property>
<property name=”password”>
<value>${jdbc.password}</value>
</property>

Style 2: Configure property file to be used in spring xml as well as in Java files
Step 1: Do All steps as in Style 1

Step 2: Add one more property called property-override with your “property-placeholder”
<context:property-override
location=”classpath:envSettings.properties” />

Step 3: Create one Java Class called “PathConfiguration” and add all properties & it’s setter, getter into this class. After that configure this class in your spring xml file like bellow.

<bean name=”pathConfiguration”
class=”co.cc.enlightensoft.service.common.PathConfiguration”>
</bean>

<bean
class=”org.springframework.web.context.support.ServletContextAttributeExporter”>
<property name=”attributes”>
<map>
<entry key=”pathConf” value-ref=”pathConfiguration” />
</map>
</property>
</bean>

Posted in Spring | Leave a Comment »

Send Mail Using Spring

Posted by Pankil Patel on August 22, 2010

Step 1:
Create a class named “MailService”

package co.cc.enlightensoft.service.mail;

import javax.mail.internet.MimeMessage;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

public class MailService {
private static final Log LOG = LogFactory.getLog(MailService.class);
private JavaMailSenderImpl mailSender;
public void setMailSender(JavaMailSenderImpl mailSender) {
this.mailSender = mailSender;
}

public void sendMail(String from, String[] to, String subject, StringBuffer content) {
try {
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(from);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content.toString());
mailSender.send(message);
LOG.info(“Mail has been sent.”);
} catch (Exception ex) {
System.err.println(ex.getMessage());
LOG.error(“Exception while sending mail…” + ex.getMessage());
}
}
}

Step 2:
Create Two Spring Bean (1) mailSender and Use this bean into (2) mailService which is based on created file in step 1

<bean id=”mailSender” class=”org.springframework.mail.javamail.JavaMailSenderImpl”>
<property name=”host” value=”192.168.1.1″ />
</bean>
<bean name=”mailService”>
<property name=”mailSender” ref=”mailSender” />
</bean>

Step 3:
Use “mailService” Spring Bean into your service as a property injection to send email

Goooooooooooooooooooooood Luck…….. Enjoyeeeeeeeeeeeeeeeeeeeeeee…………

Posted in Spring | Leave a Comment »

Logging Using Spring AOP 1.2

Posted by Pankil Patel on June 9, 2010

Step 1:

Add three new class as described in Logging Using Spring AOP 2.0 / 3.0 just changes are, you need to implement interfaces ThrowsAdvice, MethodBeforeAdvice, AfterReturningAdvice respactively each for each class.

Step 2:

Add entry of these three bean and “adviceList” bean for logging into your applicationContext.xml as like below:

Now you need to add property “<property name=”adviceList” ref=”adviceList”/>” into your each Service bean definition of applicationContext.xml

Posted in Spring | Leave a Comment »

Logging Using Spring AOP 2.0 / 3.0

Posted by Pankil Patel on June 9, 2010

How to use Interceptor for logging each method in centralized manner?

Step 1:

Add Three new class for your logging say named ThrowsAdviceInterceptor, MethodBeforeAdviceInterceptor, AfterReturningAdviceInterceptor as like below:

Step 2:

Add entry of these three bean and AOP point cut for logging for Service layer into your applicationContext.xml

as like below:

Here in “aop:config” there is a parameter called:  proxy-target-class=”true”

It is basically responsible for logging methods that are not covered into Services interfaces.

And for this you required cglib.jar

Jar used in this example are:

aspectjweaver-1.6.5.jar
aspectjrt-1.6.5.jar
cglib-2.2.jar
asm-3.1.jar

Posted in Spring | 2 Comments »

Spring Security 3.0 – Part 6

Posted by Pankil Patel on April 30, 2010

How to use role base access into jsp with Spring Security 3.0?

or

How to render component / tag using defined role of Spring Security 3.0 into jsp?

Step 1:

Include <%@ taglib prefix=”sec” uri=”http://www.springframework.org/security/tags&#8221; %> into your jsp page.  And use <sec:authorize> tag to render role base component Like:

Note:

Please make sure that you have entry “/case/admin” into your “http” section of “applicationContext-security.xml” file like:

That’s it for using role into jsp……
Hureeeeeeeeeeee Ready to use access mechanism of spring Security 3.0. into your jsp.

BEST OF LUCK

Posted in Spring | Leave a Comment »

Spring Security 3.0 – Part 5

Posted by Pankil Patel on April 30, 2010

How to Handle Authentication Fail when we are using “Custom EntryPoint” or “Custom authenticationProcessingFilter” or ” Custom UsernamePasswordAuthenticationFilter” in Spring Security 3.0?

Step 1:
Add new  bean entry of “authenticationFailureHandler” into your “applicationContext-security.xml” file like:

Here languageProcessingFilter is the same as explained in post Spring Security 3 .0 – Part 3.

Step 2:

Create your new class “AuthenticationFailHandler” Which will implements “org.springframework.security.web.authentication.AuthenticationFailureHandler” Like:

That’s it for Authentication Fail Handling……
Hureeeeeeeeeeee Ready to use Customized spring Security 3.0. with Authentication Fail Handling.

BEST OF LUCK

Posted in Spring | Leave a Comment »

Spring Security 3.0 – Part 4

Posted by Pankil Patel on April 14, 2010

How you will Handle “default-target-url” of “form-login” tag when you are using your custom Entry Point?

When you are using Custom Entry Point, say Using “UsernamePasswordAuthenticationFilter” and you want the same behavior as like “default-target-url” functionality of “form-login” from “HTTP” section than in your Customized Class say “MyAuthenticationProcessingFilter” (as in Spring Security 3.0 – Part 3) you have to Override one more method that is “successfulAuthentication”. Say like:

Please Note: Here return statement of getParameter method is like ‘ return “/login/homePage.action?request_locale=” + selectedLanguage; ‘ this means that you are also passing extra parameter(language) to the action which is passed from your Login page. If you simply want to call homePage Action without passing that parameter your return statment will be like: ‘ return “/login/homePage.action”; ‘

Yeeeeeeee I am able to call my homeAction After successful Authentication using spring Security 3.0.

BEST OF LUCK

Posted in Spring | 3 Comments »

 
Follow

Get every new post delivered to your Inbox.