Spring常用注解

一、使用注解之前首先要在applicationContext.xml中开启自动扫描功能,其中base-package为需要扫描的包

<context:annotation-config/>
<context:component-scan base-package="com.dongtian.MyBatis_Spring.*"/>

二、常用注解

1、@Configuration把一个类作为一个IoC容器,它的某个方法头上如果注册了@Bean,就会作为这个Spring容器中的Bean。

2、@Service("courseDAO")

  @Scope("prototype")

相当于:

<bean id="courseDAO" class="xxx" scope="prototype">   
</bean>

 举例:

@Service
public class UserServiceImp implements UserService{
    @Autowired //把UserMappper作为属性注入
    private UserMapper userMapper = null;
    //设置隔离级别,传播行为
    @Transactional(propagation = Propagation.REQUIRES_NEW,
            isolation=Isolation.READ_COMMITTED)
    public void insertUser(User user) {
        userMapper.insertUser(user);
    }

}

 

3、@Repository用于标注数据访问组件,即DAO组件。

@Repository
public interface UserMapper {
    public void insertUser(User user);
    public User findUsers(String username);
}

4、@Service用于标注业务层组件、 
5、@Controller用于标注控制层组件(如struts中的action)

原文:https://www.cnblogs.com/xingzc/p/5777814.html

posted @ 2019-05-06 19:31  DongTian  阅读(139)  评论(0编辑  收藏  举报