首页 新闻 会员 周边 捐助

SSH中注解配置的原理

0
[已解决问题] 解决于 2012-11-06 09:08

比如struts中的@Action("login"),hibernate中的@Column(name = "GRADENAME", length = 50),以及spring中的@Autowired。是怎么实现的,是什么原理?

angelshelter的主页 angelshelter | 大侠五级 | 园豆:9914
提问于:2012-10-31 16:22
< >
分享
最佳答案
0

Struts文件是在xml配置的,hibernate是连接数据库的,Spring是applicationContext里面配置的,每个实现的方式看自己所需要的

奖励园豆:5
若雪封尘 | 小虾三级 |园豆:663 | 2012-11-01 10:47

你没试过三大框架的annotaion配置?

angelshelter | 园豆:9914 (大侠五级) | 2012-11-01 10:56

@angelshelter: 几个配置文件如下:
spring-database.xml(这个文件基本不变,有新的dao在底下加入一行即可。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>

    <!-- ========================= RESOURCE DEFINITIONS ========================= -->

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName"><value>${jdbc.driverClassName}</value></property>
        <property name="url"><value>${jdbc.url}</value></property>
        <property name="username"><value>${jdbc.username}</value></property>
        <property name="password"><value>${jdbc.password}</value></property>
    </bean>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="configurationClass"><value>org.hibernate.cfg.AnnotationConfiguration</value></property>
           <property name="dataSource"><ref bean="dataSource"/></property>
           <property name="configLocation"><value>/hibernate.cfg.xml</value></property>
   </bean>

    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
           <property name="sessionFactory"><ref bean="sessionFactory"/></property>
           <property name="dataSource"><ref bean="dataSource"/></property>
    </bean>

    <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"/>

    <bean class="org.springframework.transaction.interceptor.TransactionAttributeSourceAdvisor">
        <property name="transactionInterceptor"><ref bean="transactionInterceptor"/></property>
    </bean>

    <bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
        <property name="transactionManager"><ref bean="transactionManager"/></property>
        <property name="transactionAttributeSource">
            <bean class="org.springframework.transaction.annotation.AnnotationTransactionAttributeSource"/>
        </property>
    </bean>

    <!-- ========================= DAO DEFINITIONS ========================= -->

    <bean id="testDao" class="org.steeven.TestDaoImpl">
         <property name="sessionFactory">
            <ref bean="sessionFactory"/>
        </property>
    </bean>

    <!-- ========================= DAO DEFINITIONS ========================= -->
     <bean id="mitService" class="org.steeven.TestServiceImpl"></bean>
</beans>

hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.hbm2ddl.auto">create-drop</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.cglib.use_reflection_optimizer">true</property>
        <property name="default-lazy">false</property>
       
        <!-- 用于hibernate持久化的对象" -->
        <mapping class="org.steeven.Test"/>
       
          <listener type="pre-update" class="org.hibernate.validator.event.ValidatePreUpdateEventListener"/>
          <listener type="pre-insert" class="org.hibernate.validator.event.ValidatePreInsertEventListener"/>
    </session-factory>
</hibernate-configuration>
类似于这种

若雪封尘 | 园豆:663 (小虾三级) | 2012-11-01 11:05
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册