Declartive Transaction in spring is not working - java

I am using spring declarative Transaction features. Something like this
XML file for spring configuration..
<?xml version="1.0" encoding="UTF-8"?>
<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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<context:annotation-config/>
<context:component-scan base-package="com" />
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/test" />
<property name="username" value="root" />
<property name="password" value="password" />
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name="prefix">
<value>/WEB-INF/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<!-- Add this tag to enable annotations transactions -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>
Dao Layer code
package com.dao;
#Repository("commonDao")
public class CommonDaoImpl implements CommonDao {
private static Logger logger = Logger.getLogger(CommonDaoImpl.class);
#Autowired
private DataSource dataSource;
private JdbcTemplate jdbcTemplate;
#Autowired
public void setSessionFactory(){
jdbcTemplate = new JdbcTemplate(dataSource);
}
#Override
public Object makePayment(Object e) {
String sql = "insert into payment (id, name, amount) values('abc', 100)";
try{
return jdbcTemplate.update(sql);
}catch(DataAccessException ex){
throw ex;
}
}
#Override
public Object signUp(Object e) {
String sql = "insert into login (userid, password) values('naveen', 'password')";
return jdbcTemplate.update(sql);
}
}
Service Layer code
#Service
public class CommonServiceImpl implements CommonService {
#Autowired
private CommonDao commonDao;
// #Transactional I tried both of them one by one but not worked
#Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class)
#Override
public boolean makePayment() {
try{
commonDao.signUp(new Object());
commonDao.makePayment(new Object());
} catch(DataAccessException ex){
return false;
}
return true;
}
}
but when i send call makePayment() method via controller then it save the record into login table but failed when it move to insert into payment table because i write query so that it can through an exception. I do not understand why transaction is not working. because #Transactional annotation is on makePayment method so not operation should happen in db.
Please tell what's wrong in this code.

Your SQL query in in makePayment method is :
insert into payment (id, name, amount) values('abc', 100)
You are asking the query to insert id, name and amount in the database table, but providing only 2 values i.e. abc and 100.
If id column in the table is autoincrement id then you don't need to mention it in the SQL query. Your SQL query in the makePayment method should be like this:
insert into payment (name, amount) values('abc', 100)

Related

Java spring autowires Jdbctemplate but not NamedParameterJdbcTemplate

I have thingie that uses Jdbc for sql-access. I can use JdbcTemplate but not NamedParameterJdbcTemplate. The namedparam always returns null pointer, while the jdbctemplate works like a charm. I am using spring 4.2.4.RELEASE
Am I missing a step here? Here's the simplified codes:
spring-data.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder location="classpath*:/db_connection.properties" />
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${db.driver}" />
<property name="url" value="${db.url}" />
<property name="username" value="${db.username}" />
<property name="password" value="${db.password}" />
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="namedParameterJdbcTemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
<constructor-arg ref="dataSource" />
</bean>
</beans>
someDAOImpl.java:
/*
imports
*/
#Component
public class someDAOImpl implements someDAO {
#Autowired
private JdbcTemplate jdbcTemplate;
private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
public void thisDoesntWork(Integer intValue) {
String sql = "UPDATE table SET value = :value WHERE id = 1";
MapSqlParameterSource params = new MapSqlParameterSource();
params.addValue("value", intValue);
try {
namedParameterJdbcTemplate.update(sql, params);
} catch (Exception e) {
e.printStackTrace();
}
}
public ArrayList<ResultClass> thisWorks(Integer intValue) {
String sql = "SELECT * FROM table WHERE value = ?";
ArrayList<ResultClass> results = new ArrayList<ResultClass>();
try {
results = (ArrayList<ResultClass>) jdbcTemplate.query(sql, new Object[] { intValue }, new ResultClassMapper());
} catch (Exception e) {
e.printStackTrace();
}
return results;
}
}
You need to autowired also the namedParameterJdbcTemplate
#Autowired
private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

Hibernate JPA interceptor doesn't get call

The setup I have an old project that is stuck to jdk 1.5, thus spring and hibernate versions are also the maximum possible to support this version of java. Hibernate is 3.3.2.GA and spring is 3.1.1.RELEASE. Setup is the following:
<persistence-unit name="myUnit" transaction-type="RESOURCE_LOCAL">
<mapping-file>persistence-query.xml</mapping-file>
...
<properties>
<property name="hibernate.max_fetch_depth" value="3" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.hbm2ddl.auto" value="validate" />
<property name="hibernate.ejb.interceptor" value="com.myproj.common.dao.AuditInterceptor"/>
</properties>
</persistence-unit>
application context:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<context:component-scan base-package="..."/>
<tx:annotation-driven transaction-manager="transactionManager" />
<aop:aspectj-autoproxy/>
<bean id="applicationContextProvder" class="com.myproj.common.utils.ApplicationContextProvider"/>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" >
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceXmlLocation" value="classpath:persistence.xml" />
<property name="persistenceUnitName" value="myUnit" />
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
<property name="jpaDialect" ref="jpaDialect" />
</bean>
<bean id="jpaVendorAdapter"
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="database" value="ORACLE" />
<property name="showSql" value="true" />
</bean>
<bean id="jpaDialect" class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
<!-- Local transaction management -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
<property name="dataSource" ref="dataSource" />
<property name="jpaDialect" ref="jpaDialect" />
</bean>
and my interceptor:
#Component
public class AuditInterceptor extends EmptyInterceptor {
private static final long serialVersionUID = 98658932451L;
#Autowired
private JdbcTemplate jdbcTemplate;
public void afterTransactionBegin(Transaction tx) {
if (user != null) {
jdbcTemplate.execute("call ah_audit_pkg.SetAudit('test')");
super.afterTransactionBegin(tx);
}
}
}
I run a junit to test that interceptor is getting called and it is not. In debug mode, I am able to see this:
Any help is appreciated! Why my intercetprot is not getting called.
Edit:
I also tried the interceptor to override the afterTransactionBegin but it didn't help.
I ended up with the following solution:
I have my entityt that extends from a superclass mapped entity:
#Entity
#Table(name = "my_table")
public class MyTable extends AuditInfo
The AuditInfo entity has the following mapping:
#MappedSuperclass
public abstract class AuditInfo implements Serializable {
...
#PrePersist
void onCreate() throws SQLException {
//this empty method is needed for AOP to trigger the audit information insert before entity is stored
}
#PreUpdate
void onPersist() throws SQLException {
//this empty method is needed for AOP to trigger the audit information insert before entity is updated
}
#PreRemove
void onRemove() throws SQLException {
//this empty method is needed for AOP to trigger the audit information insert before entity is removed
}
}
And the Aspect class:
#Aspect
#Component
public class MyAspect {
#Before("execution(* com.mypackage.entities.AuditInfo.on*(..))")
public void setAuditHistory(JoinPoint jp){
final AuditInfo info = ((AuditInfo)jp.getThis());
JdbcTemplate jdbcTemplate = ApplicationContextProvider.getApplicationContext().getBean(JdbcTemplate.class);
jdbcTemplate.execute(new CallableStatementCreator() {
public CallableStatement createCallableStatement(Connection conn) throws SQLException {
CallableStatement stmt = conn.prepareCall("begin ah_audit_pkg.SetAudit(?,?); end;");
stmt.setString(1, info.getAuditUser());
if(info.getAuditLocation() != null && info.getAuditLocation().trim().length() !=0) {
stmt.setString(2, info.getAuditLocation());
} else {
stmt.setString(2, info.getAuditUser());
}
return stmt;
}
}, new CallableStatementCallback<Object>() {
public Object doInCallableStatement(CallableStatement cs) throws SQLException, DataAccessException {
return cs.executeUpdate();
}
});
}
}
It is to be noted that the Spring beans are extracted from the context and not autowired - this is because AOP is a singleton class in spring implementation, and none of the autowired beans will be ever instantiated even if they are available in the context. So I had to manually retrieve them for later usage.

JdbcTemplate NullPointerException

I am trying to save data to database with Spring's JdbcTemplate, but I get this error message. If I do it normal way with PreparedStatements it's working.
My CarDAO class:
#Repository
#Service
public class CarDAO implements CarDAOService {
private JdbcTemplate jdbcTemplate;
public JdbcTemplate getJdbcTemplate() {
return jdbcTemplate;
}
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public void saveCarToDB(CarBean carbean) {
final String sql = "insert into car (make, model) values (?,?)";
Object[] parameters = new Object[] {carbean.getMake()+
carbean.getModel()};
//if I do here system.out.print(Arrays.toString(parameters));
//it will print right make/model.
jdbcTemplate.update(sql, parameters);
//console says it is that row above, but I don't get how. Both parameters has values?
//WARNING: StandardWrapperValve[spring-dispatcher]: Servlet.service() for servlet
//springdispatcher threw exception
}
Spring-base.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<context:component-scan base-package="bean, dao" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/Jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- DATA SOURCE -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="org.mariadb.jdbc.Driver" />
<property name="url" value="jdbc:mariadb://XXXXX" />
<property name="username" value="XXXXX" />
<property name="password" value="XXXXX" />
<property name="initialSize" value="1" />
<property name="maxActive" value="5" />
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
<mvc:annotation-driven />
</beans>
I didn't include my Controller or CarDAOService class, because I think the problem isn't there. They are forwarding right parameters to CarDAO class.
You need to put the #Autowired annotation on your setter:
#Autowired
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
Autowired bean via #Autowired, and it can be applied on setter method, constructor or a field.
Try this
#Autowired
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}

How to close connection using session factory

I am new to spring mvc and hibernate.
How to close connection in spring mvc applction. I am very frustrated from this issue.
This is my code:
Dispatcher servlet:
<?xml version="1.0" encoding="UTF-8"?>
<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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.kqics" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />
<bean id="userService" class="com.kqics.dao.kqtraveldao">
</bean>
<bean id="viewResolver1" class="org.springframework.web.servlet.view.ResourceBundleViewResolver">
<property name="order" value="1"/>
<property name="basename" value="views"/>
</bean>
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- one of the properties available; the maximum file size in bytes -->
<property name="maxUploadSize" value="10000000" />
</bean>
<import resource="db-config.xml" />
</beans>
dbconfig.xml
<?xml version="1.0" encoding="UTF-8"?>
<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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location"><value>/WEB-INF/jdbc.properties</value></property>
</bean>
<bean id="dataSourceBean" lazy-init="true" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${jdbc.driverClassName}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}"/>
<property name="acquireIncrement" value="${jdbc.acquireIncrement}" />
<property name="minPoolSize" value="${jdbc.minPoolSize}" />
<property name="maxPoolSize" value="${jdbc.maxPoolSize}" />
<property name="maxIdleTime" value="${jdbc.maxIdleTime}" />
<property name="numHelperThreads" value="${jdbc.numHelperThreads}" />
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
p:dataSource-ref="dataSourceBean"
p:packagesToScan="com.kqics" >
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<!-- <prop key="hibernate.hbm2ddl.auto">create</prop> -->
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.connection.release_mode">after_transaction</prop>
<prop key="hibernate.connection.shutdown">true</prop>
</props>
</property>
</bean>
<!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) -->
<tx:annotation-driven/>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ><ref bean="sessionFactory"/></property>
</bean>
</beans>
my service class:
#Service
public class kqtravellogservice implements ikqtravellogservice {
#Autowired
ikqtraveldao iDao;
#Transactional
public void serviceaddnewvehicle(kqvehicle obj) {
// TODO Auto-generated method stub
iDao.addnewvehicle(obj);
}
#Transactional
public List<kqvehicle> servicefetchallvehicle() {
return iDao.fetchallvehicle();
}
#Transactional
public void serviceaddnewvehicletariff(kqvehicletariff obj,String tariff) {
iDao.addnewvehicletariff(obj,tariff);
}
dao impl
public class kqtraveldao implements ikqtraveldao {
private HibernateTemplate hibernateTemplate;
#Autowired
public void setSessionFactory(SessionFactory sessionFactory) {
try {
hibernateTemplate = new HibernateTemplate(sessionFactory);
} catch (Exception w) {
}
}
#Override
public void addnewvehicle(kqvehicle obj) {
hibernateTemplate.save(obj);
}
#SuppressWarnings("unchecked")
#Override
public List<kqvehicle> fetchallvehicle() {
List<kqvehicle> li=null;
li=hibernateTemplate.find("from kqvehicle");
return li;
}
#Override
public void addnewvehicletariff(kqvehicletariff obj, String tariff) {
try
{
hibernateTemplate.getSessionFactory()
.openSession()
.createSQLQuery("insert into "+tariff+" values(?,?,?,?,?)")
.setParameter(0, obj.getTid())
.setParameter(1, obj.getVehicletype())
.setParameter(2, obj.getRupees())
.setParameter(3, obj.getDateupto())
.setParameter(4, obj.getDatetimedetermined())
.executeUpdate();
}
catch(Exception e)
{
}
finally
{
hibernateTemplate.getSessionFactory().close();
}
}
Some friends told me as i am not using singleton, connection closing.. so, i got the too many connection error... Please advice me how to resolve this problem...
What are the changes is needed for my code....
The problem is in your dao, your save method is destroying springs proper tx management. You should NEVER call openSession() when you are using Spring to manage your connections and sessions.
Instead use a HibernateCallback which will give you a spring managed session.
#Override
public void addnewvehicletariff(final kqvehicletariff obj, final String tariff) {
hibernateTemplate.execute(new HibernateCallback() {
public Object doInHibernate(Session session) {
session.createSQLQuery("insert into "+tariff+" values(?,?,?,?,?)")
.setParameter(0, obj.getTid())
.setParameter(1, obj.getVehicletype())
.setParameter(2, obj.getRupees())
.setParameter(3, obj.getDateupto())
.setParameter(4, obj.getDatetimedetermined())
.executeUpdate();
}
}
}
Another note is that you shouldn't be using HibernateTemplate anymore, you should write code against the plain hibernate API using the getCurrentSession() method on the SessionFactory. See http://docs.spring.io/spring/docs/current/spring-framework-reference/html/orm.html#orm-hibernate-straight for more information.
public class kqtraveldao implements ikqtraveldao {
private SessionFactory sessionFactory;
#Autowired
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory=sessionFactory;
}
#Override
public void addnewvehicle(kqvehicle obj) {
sessionFactory.getCurrentSession().save(obj);
}
#SuppressWarnings("unchecked")
#Override
public List<kqvehicle> fetchallvehicle() {
return sessionFactory.getCurrentSession()
.createQuery("from kqvehicle")
.list();
}
#Override
public void addnewvehicletariff(kqvehicletariff obj, String tariff) {
sessionFactory.getCurrentSession()
.createSQLQuery("insert into "+tariff+" values(?,?,?,?,?)")
.setParameter(0, obj.getTid())
.setParameter(1, obj.getVehicletype())
.setParameter(2, obj.getRupees())
.setParameter(3, obj.getDateupto())
.setParameter(4, obj.getDatetimedetermined())
.executeUpdate();
}
}
just autowire the sessionfactory in the dao and remove the method set session factory.
#Autowired
private SessionFactory sessionfactory;
you can close the connection by calling sessionfactory.getCurrentSession().close() in your method.
the Session factory is the 'singleton' in your application.

Struts 2, Spring Plugin + Shiro integration: Autowiring issue

I have an issue with Struts2(2.3.15.1) Spring plugin. I use Shiro for security, and configure it with Annotations. The code looks like this:
public class Order2Action extends BaseAction {
#Autowired private UserDAO userDAO;
private static final Logger log = LoggerFactory.getLogger(Order2Action.class);
#Action(value = "list2",
results = {#Result(name = SUCCESS, location = "list.jsp")}
)
#RequiresRoles(ROLE_DATA_OPERATOR)
public String showOrderList() throws InterruptedException {
log.info("UserDAOImpl {}", userDAO);
return SUCCESS;
}
}
In this case UserDAO is not injected to Action. But if I comment out the string "#RequiresRoles(ROLE_DATA_OPERATOR)" - UserDAO is injected to Action.
What could be the problem here?
My ApplicationContext conf looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<!-- enable processing of annotations such as #Autowired and #Configuration -->
<context:annotation-config/>
<context:component-scan base-package="ee"/>
<bean id="makuDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
...
</bean>
<!--Shiro security configuration -->
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<property name="securityManager" ref="securityManager"/>
<property name="loginUrl" value="/maku/auth/login"/>
<property name="unauthorizedUrl" value="/maku/auth/unauthenticated"/>
</bean>
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="cacheManager" ref="shiroCacheMan"/>
<property name="realm" ref="jdbcRealm"/>
</bean>
<bean id="jdbcRealm" class="org.apache.shiro.realm.jdbc.JdbcRealm">
<property name="dataSource" ref="makuDataSource"/>
<property name="credentialsMatcher">
<bean class="org.apache.shiro.authc.credential.PasswordMatcher"/>
</property>
</bean>
<bean name="shiroCacheMan" class="org.apache.shiro.cache.ehcache.EhCacheManager"/>
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>
<!-- Enable Shiro Annotations for Spring-configured beans. Only run after -->
<!-- the lifecycleBeanProcessor has run: -->
<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor">
<property name="proxyTargetClass" value="true" />
</bean>
<bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
<property name="securityManager" ref="securityManager"/>
</bean>
</beans>
UserDAO:
public interface UserDAO {
User getByUsername(String username);
}
#Repository
public class UserDAOImpl extends AbstractJDBCDAO implements UserDAO {
#Override
public User getByUsername(String username){
return makuTmpl.queryForObject("select id, username, fullname from users where username=?",
new RowMapper<User>() {
#Override
public User mapRow(ResultSet rs, int i) throws SQLException {
User u = new User();
u.setId(rs.getLong( 1 ));
u.setUsername(rs.getString(2));
u.setFullname(rs.getString(3));
return u;
}
}, username);
}
}

Categories