1、使用注解以前首先要在applicationContext.xml中开启自动扫描功能,其中base-package为须要扫描的包html
<context:annotation-config/> <context:component-scan base-package="com.dongtian.MyBatis_Spring.*"/>
2、经常使用注解app
一、@Configuration把一个类做为一个IoC容器,它的某个方法头上若是注册了@Bean,就会做为这个Spring容器中的Bean。spa
二、@Service("courseDAO")prototype
@Scope("prototype")code
至关于:component
<bean id="courseDAO" class="xxx" scope="prototype"> </bean>
举例:xml
@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); } }
三、@Repository用于标注数据访问组件,即DAO组件。htm
@Repository public interface UserMapper { public void insertUser(User user); public User findUsers(String username); }
四、@Service用于标注业务层组件、
五、@Controller用于标注控制层组件(如struts中的action)blog