Spring常用注解

用注解来向Spring容器注册Bean。需要在applicationContext.xml中注册

    <context:component-scan base-package="com.lzz.dao">
    </context:component-scan>

表明在com.lzz.dao包及子包中,如果某个类带有特定注解(@Component/@Controller/@Service/@Repository),就会将这个对象作为Bean注册进Spring容器;也可以指定多个包:

1、@Component

表示所有受Spring管理的组件,但不推荐使用;一般用于难以划分组件的类中。

2、@Controller

顾名思义,表示控制器的bean组件,对应表现层

@Controller
public class HelloController {
    @RequestMapping("/hello")
    public String hello() throws Exception {
        if (true) throw new Exception("some exception");
        return "Springboot mybatis xml";
    }
}

使用@Controller注解标识HelloController之后,就表示要把HelloController交给Spring容器管理,在Spring容器中会存在一个名字为”helloController”的Controller,这个名字是根据HelloController类名来取的。注意:如果@Controller不指定其value【@Controller】,则默认的bean名字为这个类的类名首字母小写,如果指定value【@Controller(value=”HelloController”)】或者【@Controller(“HelloController”)】,则使用value作为bean的名字。

这里的HelloController还可以使用@Scope注解,@Scope(“prototype”)表示将Controller的范围声明为原型,可以利用容器的scope=”prototype”来保证每一个请求有一个单独的Action来处理,避免struts中Action的线程安全问题。spring 默认scope 是单例模式(scope=”singleton”),这样只会创建一个Action对象,每次访问都是同一Action对象,数据不安全,struts2 是要求每次次访问都对应不同的Controller,scope=”prototype” 可以保证当有请求的时候都创建一个Controller对象。

3、@ Service

对应业务层bean组件,例如:

@Service("userService")
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Override
    public User login(User loginUser) {
        User user = userMapper.getOne(loginUser.getId());
        if(user.getPassWord().equals(loginUser.getPassWord())){
            user.setPassWord("******");
            return user;
        }
        return null;
    }
}

@Service(“userService”)注解是告诉Spring,当Spring要创建UserServiceImpl的的实例时,bean的名字必须叫做”userService”,这样当Controller需要使用UserServiceImpl的的实例时,就可以由Spring创建好”userService”,然后注入给Controller:在Controller只需要声明一个名字叫“userService”的变量来接收由Spring注入的”userService”即可,具体代码如下:

 // 注入userService
 @Resource(name = "userService")
 private UserService userService;

或者

 @Autowired
 private UserService userService;

注意:在Controller声明的“userService”变量的类型必须是“UserServiceImpl”或者是其实现的接口“UserService”,否则由于类型不一致而无法注入,由于Controller中的声明的“userService”变量使用了@Resource注解去标注,并且指明了其name = “userService”,这就等于告诉Spring,说我Controller要实例化一个“userService”,你Spring快点帮我实例化好,然后给我,当Spring看到userService变量上的@Resource的注解时,根据其指明的name属性可以知道,Controller中需要用到一个UserServiceImpl的实例,此时Spring就会把自己创建好的名字叫做”userService”,类型是UserServiceImpl的实例注入给Controller中的“userService”变量,帮助Controller完成userService的实例化,这样在Action中就不用通过“UserService userService = new UserServiceImpl();”这种最原始的方式去实例化userService了。如果没有Spring,那么当Action需要使用UserServiceImpl时,必须通过“UserService userService = new UserServiceImpl();”主动去创建实例对象,但使用了Spring之后,Action要使用UserServiceImpl时,就不用主动去创建UserServiceImpl的实例了,创建UserServiceImpl实例已经交给Spring来做了,Spring把创建好的UserServiceImpl实例给Controller,Controller拿到就可以直接用了。Controller由原来的主动创建UserServiceImpl实例后就可以马上使用,变成了被动等待由Spring创建好UserServiceImpl实例之后再注入给Controller,Controller才能够使用。这说明Controller对“UserServiceImpl”类的“控制权”已经被“反转”了,原来主动权在自己手上,自己要使用“UserServiceImpl”类的实例,自己主动去new一个出来马上就可以使用了,但现在自己不能主动去new“UserServiceImpl”类的实例,new“UserServiceImpl”类的实例的权力已经被Spring拿走了,只有Spring才能够new“UserServiceImpl”类的实例,而Controller只能等Spring创建好“UserServiceImpl”类的实例后,再“恳求”Spring把创建好的“UserServiceImpl”类的实例给他,这样他才能够使用“UserServiceImpl”,这就是Spring核心思想“控制反转”,也叫“依赖注入”,“依赖注入”也很好理解,Controller需要使用UserServiceImpl干活,那么就是对UserServiceImpl产生了依赖,Spring把Controller需要依赖的UserServiceImpl注入(也就是“给”)给Controller,这就是所谓的“依赖注入”。对Controller而言,Controller依赖什么东西,就请求Spring注入给他,对Spring而言,Controller需要什么,Spring就主动注入给他。

4、@ Repository

对应数据持久层bean组件,例如:

@Repository("userMapper")
public interface UserMapper {

    List<User> getAll();

    User getOne(Long id);

    void insert(User user);

    void update(User user);

    void delete(Long id);

}

@Repository(“userMapper”)注解是告诉Spring,让Spring创建一个名字叫“userDao”的userMapperImpl实例。

当Service需要使用Spring创建的名字叫“userMapper”的userMapperImpl实例时,就可以使用@Resource(name = “userMapper”)注解告诉Spring,Spring把创建好的userMapper注入给Service即可。

@Resource(name = "userMapper")
private UserMapper userMapper;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值