spring注釈annotation
16411 ワード
@Resorse(name=" xxx")は文脈からxxxの名前と同じものを探して導入するという意味です。
xmlns:context="http://www.springframework.org/schema/context「
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
spring.xml中
implコメント (コメントが出ていますので、Hbernature DaoSupportを引き継ぐことはできません。組み合わせを利用しています。)
@Repository("personDao") bean bean
spring.xml新規加入xmlns:context="http://www.springframework.org/schema/context「
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
spring.xml中
1 <beans xmlns="http://www.springframework.org/schema/beans"
2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3 xmlns:aop="http://www.springframework.org/schema/aop"
4 xmlns:context="http://www.springframework.org/schema/context"
5 xmlns:tx="http://www.springframework.org/schema/tx"
6 xsi:schemaLocation="http://www.springframework.org/schema/beans
7 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
8 http://www.springframework.org/schema/aop
9 http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
10 http://www.springframework.org/schema/context
11 http://www.springframework.org/schema/context/spring-context-2.5.xsd
12 http://www.springframework.org/schema/tx
13 http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
14 <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
15 <property name="configLocation">
16 <value>classpath:hibernate/hibernate.cfg.xml</value>
17 </property>
18 </bean>
19
20 <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
21 <property name="sessionFactory">
22 <ref bean="sessionFactory"/>
23 </property>
24 </bean>
25
26 <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
27 <property name="sessionFactory">
28 <ref bean="sessionFactory"/>
29 </property>
30 </bean>
31
32 <context:component-scan base-package="cn.itcast.s2sh"></context:component-scan>
33
34 <tx:annotation-driven transaction-manager="transactionManager"/>
35
36 </beans>
implコメント (コメントが出ていますので、Hbernature DaoSupportを引き継ぐことはできません。組み合わせを利用しています。)
1 package cn.itcast.s2sh.sh.dao.impl;
2
3 import java.io.Serializable;
4
5 import javax.annotation.Resource;
6
7 import org.springframework.orm.hibernate3.HibernateTemplate;
8 import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
9 import org.springframework.stereotype.Repository;
10
11 import cn.itcast.s2sh.domain.sh.Person;
12 import cn.itcast.s2sh.sh.dao.PersonDao;
13
14 @Repository("personDao") // beanname
15 public class PersonDaoImpl implements PersonDao{
16 @Resource(name="hibernateTemplate") // spring.xml
17 private HibernateTemplate hibernateTemplate; //set get ,
18 @Override
19 public void savePerson(Person person) {
20 // TODO Auto-generated method stub
21 this.hibernateTemplate.save(person);
22 }
23
24 @Override
25 public Person getPesonById(Serializable id) {
26 // TODO Auto-generated method stub
27 return (Person) this.hibernateTemplate.load(Person.class, id);
28 }
29
30 }
public class PersonDaoImpl extends HibernateSupport implements PersonDao
{// hibernateTemplate
}
HibernateTemple spring , private HibernateTemplate hibernateTemplate; , HibernateSupport , , hibernateTemplate , , , 。
serviceImpl 1 package cn.itcast.s2sh.sh.service.impl;
2
3 import java.io.Serializable;
4
5 import javax.annotation.Resource;
6
7 import org.springframework.stereotype.Service;
8 import org.springframework.transaction.annotation.Transactional;
9
10 import cn.itcast.s2sh.domain.sh.Person;
11 import cn.itcast.s2sh.sh.dao.PersonDao;
12 import cn.itcast.s2sh.sh.service.PersonService;
13
14 @Service("personService")
15 public class PersonServiceImpl implements PersonService{
16 @Resource(name="personDao")
17 private PersonDao personDao;
18
19 @Override
20 @Transactional(readOnly=false) //
21 public void savePerson(Person person) {
22 // TODO Auto-generated method stub
23 this.personDao.savePerson(person);
24 }
25
26 @Override
27 public Person getPersonByID(Serializable id) {
28 // TODO Auto-generated method stub
29 Person person = this.personDao.getPesonById(id);
30 return person;
31 }
32 }