Friday, August 24, 2012

Spring Auto-Wiring Beans with @Autowired annotation

In Spring, you can use @Autowired annotation to auto wire bean on the setter method, constructor or a field.

There are two ways to can achieve it :
1.Using <context:annotation-config />
    Just Add Spring context and <context:annotation-config /> in bean configuration file

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:annotation-config />

</beans>
    
2. Using AutowiredAnnotationBeanPostProcessor
Add 'AutowiredAnnotationBeanPostProcessor' directly in your bean configuration file.

<bean 
class="org.springframework.beans.factory.annotation
.AutowiredAnnotationBeanPostProcessor"/>
 
And in Class, Just add annotation @Autowired above the setter function as seen below , You can do the same on constructor too:
class A {
private B b;

   @Autowired
   public void setB(B b) {
      this.b = b;
   }
}



Error:No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here

To solve this problem. You have to do two things:

1. mark the DAO as transactional or function which is doing database call like:

  @Transactional
   public class EmployeeDaoImpl  extends DaoImpl implements EmployeeDao{
 /////
}
           OR
@Transactional
    public long addEmployee(Employee employee) {
        System.out.println("Employee:"+employee );
        long id = employeeDao.addEmployee(employee);
        System.out.println("Id1: " );
        return id;
    }


2. Enable the annotation driven transcation management in applicationContext.xml (where your beans are defined):


<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- Main transaction manager for accessing internal DB -->
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
   
    <!-- enable the configuration of transactional behavior based on annotations -->
  <tx:annotation-driven transaction-manager="transactionManager"/>

That's it. Hope it works for other too.

Keyboard Shortcuts That Work in All Web Browsers

Tabs

Ctrl+1-8 – Switch to the specified tab, counting from the left.
Ctrl+9 – Switch to the last tab.
Ctrl+Tab – Switch to the next tab – in other words, the tab on the right. (Ctrl+Page Up also works, but not in Internet Explorer.)
Ctrl+Shift+Tab – Switch to the previous tab – in other words, the tab on the left. (Ctrl+Page Down also works, but not in Internet Explorer.)
Ctrl+W, Ctrl+F4 – Close the current tab.
Ctrl+Shift+T – Reopen the last closed tab.
Ctrl+T – Open a new tab.
Ctrl+N – Open a new browser window.
Alt+F4 – Close the current window. (Works in all applications.)

Mouse Actions for Tabs

Middle Click a Tab – Close the tab.
Ctrl+Left Click, Middle Click – Open a link in a background tab.
Shift+Left Click – Open a link in a new browser window.
Ctrl+Shift+Left Click – Open a link in a foreground tab.

Navigation

Alt+Left Arrow, Backspace – Back.
Alt+Right Arrow, Shift+Backspace – Forward.
F5 – Reload.
Ctrl+F5 – Reload and skip the cache, re-downloading the entire website.
Escape – Stop.
Alt+Home – Open homepage.

Zooming

Ctrl and +, Ctrl+Mousewheel Up – Zoom in.
Ctrl and -, Ctrl+Mousewheel Down — Zoom out.
Ctrl+0 – Default zoom level.
F11 – Full-screen mode.

Scrolling

Space, Page Down – Scroll down a frame.
Shift+Space, Page Up – Scroll up a frame.
Home – Top of page.
End – Bottom of page.
Middle Click – Scroll with the mouse. (Windows only)

Address Bar

Ctrl+L, Alt+D, F6 – Focus the address bar so you can begin typing.
Ctrl+Enter – Prefix www. and append .com to the text in the address bar, and then load the website. For example, type howtogeek into the address bar and press Ctrl+Enter to open www.howtogeek.com.
Alt+Enter – Open the location in the address bar in a new tab.

Search

Ctrl+K, Ctrl+E – Focus the browser’s built-in search box or focus the address bar if the browser doesn’t have a dedicated search box. (Ctrl+K doesn’t work in IE, Ctrl+E does.)
Alt+Enter – Perform a search from the search box in a new tab.
Ctrl+F, F3 – Open the in-page search box to search on the current page.
Ctrl+G, F3 – Find the next match of the searched text on the page.
Ctrl+Shift+G, Shift+F3 – Find the previous match of the searched text on the page.

History & Bookmarks

Ctrl+H – Open the browsing history.
Ctrl+J – Open the download history.
Ctrl+D – Bookmark the current website.
Ctrl+Shift+Del – Open the Clear Browsing History window.

Other Functions

Ctrl+P – Print the current page.
Ctrl+S – Save the current page to your computer.
Ctrl+O – Open a file from your computer.
Ctrl+U – Open the current page’s source code. (Not in IE.)
F12 – Open Developer Tools. (Requires Firebug extension for Firefox.)


How TOPT Works: Generating OTPs Without Internet Connection

Introduction Have you ever wondered how authentication apps like RSA Authenticator generate One-Time Passwords (OTPs) without requiring an i...