Spring 2.5注解の魔力
この文を読んでください。ありがとうございます。remote_ロムメードのコラム remote_。ロムマーは詳しく書いていますが、自分で配置する部分に違いがあります。自分の配置を張り出してください。
<?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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="url" value="jdbc:mysql://localhost:3306/test">
</property>
<property name="username" value="root"></property>
<property name="password" value=""></property>
<property name="driverClassName"
value="com.mysql.jdbc.Driver">
</property>
</bean>
<!-- hibernate SessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<!-- spring2.5.6 <property name="packagesToScan" value="org.eline.entity*.*" /> -->
<property name="annotatedClasses">
<list>
<value>model.entity.Userinfo</value>
</list>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<context:component-scan base-package="model.service" />
<bean id="transactionInterceptor"
class="org.springframework.transaction.interceptor.TransactionInterceptor">
<!-- bean -->
<property name="transactionManager">
<ref local="transactionManager" />
</property>
<property name="transactionAttributes">
<!-- -->
<props>
<prop key="insert*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="add*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="remove*">PROPAGATION_REQUIRED</prop>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="change*">PROPAGATION_REQUIRED</prop>
<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
<!-- hibernate -->
<bean id="hibernateTemplate"
class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- BeanNameAutoProxyCreator -->
<bean id="beanNameAutoProxyCreator"
class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<!-- bean name bean -->
<property name="beanNames">
<list>
<value>*Service</value>
<!-- <value>*ServiceImpl</value> -->
</list>
</property>
<!-- BeanNameAutoProxyCreator -->
<property name="interceptorNames">
<list>
<!-- Interceptor -->
<value>transactionInterceptor</value>
</list>
</property>
</bean>
</beans>
Domainを貼ります
package model.entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "userinfo", catalog = "test")
public class Userinfo implements java.io.Serializable {
// Fields
private String id;
private String username;
private String password;
private String email;
// Constructors
/** default constructor */
public Userinfo() {
}
/** full constructor */
public Userinfo(String id, String username, String password, String email) {
this.id = id;
this.username = username;
this.password = password;
this.email = email;
}
// Property accessors
@Id
@Column(name = "id", unique = true, nullable = false, length = 50)
public String getId() {
return this.id;
}
public void setId(String id) {
this.id = id;
}
@Column(name = "username", nullable = false, length = 40)
public String getUsername() {
return this.username;
}
public void setUsername(String username) {
this.username = username;
}
@Column(name = "password", nullable = false, length = 20)
public String getPassword() {
return this.password;
}
public void setPassword(String password) {
this.password = password;
}
@Column(name = "email", nullable = false, length = 60)
public String getEmail() {
return this.email;
}
public void setEmail(String email) {
this.email = email;
}
}
Serviceを張り出す
package model.service.impl;
import model.dao.UserInfoDao;
import model.entity.Userinfo;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import model.service.UserInfoService;
@Component
public class UserInfoServiceImpl implements UserInfoService {
//Spring 2.5 @Autowired ,
// 、 , 。
@Autowired
private UserInfoDao userInfoDao;
/* (non-Javadoc)
* @see service.impl.UserInfoService#createUserInfo(java.entity.UserInfo)
*/
public boolean createUserInfo(Userinfo user){
if(user==null) return false;
try{
userInfoDao.addUserInfo(user);
return true;
}catch(Exception e){
e.printStackTrace();
return false;
}
}
/* (non-Javadoc)
* @see service.impl.UserInfoService#getUserInfoAll(java.lang.Class)
*/
public List getUserInfoAll(Class user){
List list = null;
try{
list= userInfoDao.getUserInfoAll(Userinfo.class);
}catch(Exception e){
e.printStackTrace();
return null;
}
return list;
}
}
以上のコードは一貫していません。構成ファイルの例だけです。数日後には実際の例を整理する時間があります。