第十一讲 使用注解开发

来源:互联网 发布:学习数据库系统工程师 编辑:程序博客网 时间:2024/06/06 03:22
一、spring配置文件(applicationContext.xml):

<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"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/tx
     http://www.springframework.org/schema/tx/spring-tx.xsd
     http://www.springframework.org/schema/aop
     http://www.springframework.org/schema/aop/spring-aop.xsd
     http://www.springframework.org/schema/context
     http://www.springframework.org/schema/context/spring-context.xsd">
     <!-- 配置数据源 -->
     <beanid="dataSource"
          class="org.springframework.jdbc.datasource.DriverManagerDataSource">
           <propertyname="driverClassName"value="com.mysql.jdbc.Driver"></property>
           <propertyname="url"value="jdbc:mysql://localhost:3306/test"></property>
           <propertyname="username"value="root"></property>
           <propertyname="password"value="123456"></property>
     </bean>
     <!-- 声明式事务配置 -->
     <!-- 配置事务管理器 -->
     <beanid="txManager"
          class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
           <propertyname="dataSource"ref="dataSource"/>
     </bean>
     <!-- 配置事务通知 -->
     <tx:adviceid="txAdvice"transaction-manager="txManager">
           <tx:attributes>
                <!-- 配置哪些方法使用什么样的事务,配置事务的传播特性 -->
                <tx:methodname="add"propagation="REQUIRED"/>
                <tx:methodname="insert"propagation="REQUIRED"/>
                <tx:methodname="update"propagation="REQUIRED"/>
                <tx:methodname="delete"propagation="REQUIRED"/>
                <tx:methodname="remove*"propagation="REQUIRED"/>
                <tx:methodname="get"read-only="true"/>
                <tx:methodname="*"propagation="REQUIRED"/>
           </tx:attributes>
     </tx:advice>
     <aop:config>
           <!-- 切入点应该是service.impl下的所有类的所有方法,这里为了方便测试所以直接用了dao.impl -->
           <aop:pointcutexpression="execution(* com.liujie.dao.impl.*.*(..))"
                id="pointcut"/>
           <aop:advisoradvice-ref="txAdvice"pointcut-ref="pointcut"/>
     </aop:config>
     <!-- 声明式事务配置结束 -->
     <!-- 配置sqlSessionFactory -->
     <beanid="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean">
           <propertyname="dataSource"ref="dataSource"></property>
           <propertyname="configLocation"value="classpath:mybatis-config.xml"></property>
     </bean>
     <!-- 自动扫描所配置包下的所有注解
           dao:@Repository("userDao")
           service:@Service("userService")
           action:@Controller("userAction") @Scope("prototype")
           属性的注入:@Autowired
      -->
     <context:component-scanbase-package="com.liujie"></context:component-scan>
</beans>

二、spring注解:

     UserDaoImpl.java:


@Repository("userDao")
publicclass UserDaoImpl extendsSqlSessionDaoSupport implements UserDao {
     @Autowired
     @Override
     publicvoid setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
           super.setSqlSessionFactory(sqlSessionFactory);
     }
     
     publicList<User> getAll() {
           returnthis.getSqlSession().selectList("com.liujie.model.UserMapper.getAll");
     }
}


     <beanid="userDao"class="com.liujie.dao.impl.UserDaoImpl">
           <propertyname="sqlSessionFactory"ref="sqlSessionFactory"></property>
     </bean>

@Repository("userDao"),相当于是创建一个id="userDao"bean@Autowired,可以写在要注入的属性上面,也可以写在要注入
属性的setter方法上面,注入的值是id=属性名bean

     UserServiceImpl.java:


@Service("userService")
publicclass UserServiceImpl implementsUserService {
     @Autowired
     privateUserDao userDao;
     
     publicvoid setUserDao(UserDao userDao) {
           this.userDao= userDao;
     }
     
     publicList<User> getAll() {
           returnuserDao.getAll();
     }
}

   为什么上面UserDaoImpl.java里面,@Repository("userDao")中一定要写"userDao"呢?
   因为这里要注入UserDao的属性名为userDao,如果UserDaoImpl.java里面直接写@Repository,默认创建的bean的
   id=类名(userDaoImpl),所以要么在UserDaoImpl.java里面写@Repository("userDao"),要么把这里privateUserDaouserDao
   写为privateUserDaouserDaoImpl。

     UserAction.java:

@Controller("userAction")
@Scope("prototype")
publicclass UserAction {
     privateList<User> list;
     
     @Autowired
     privateUserService userService;
     
     publicvoid setUserService(UserService userService) {
           this.userService= userService;
     }
     
     publicList<User> getList() {
           returnlist;
     }
     
     publicvoid setList(List<User> list) {
           this.list= list;
     }
     
     publicString list() {
           list= userService.getAll();
           return"success";
     }
     
}

三、总结:


     <beanid="userDao"class="com.liujie.dao.impl.UserDaoImpl">
           <propertyname="sqlSessionFactory"ref="sqlSessionFactory"></property>
     </bean>
     
     <beanid="userService"class="com.liujie.service.impl.UserServiceImpl">
           <propertyname="userDao"ref="userDao"></property>
     </bean>
     
     <beanid="userAction"class="com.liujie.action.UserAction"scope="prototype">
           <propertyname="userService"ref="userService"></property>
     </bean>

   其实就是用注解的方式来生成这3个bean。
原创粉丝点击