Spring容器托管两种方法

Spring最核心的就是IOC(控制反转)和AOP(面向切面编程)。

IOC可以理解为把Spring当做一个容器,用来管理各种service、dao等。不用再去手动new。

将service、dao等注册到spring容器中,有两种办法:

1、在xml中定义bean,比如:

<bean class="com.xxx.trade.common.xxx.xxx"/>

2、通过注解。

   常用的注解有

@Controller  主要是controller层。

@Service  业务层。  

@Repository  dao层。

@Component  通用注解。

如果没有注入bean,在编译的时候不会报错,但是启动程序的时候,会报Bean未定义异常。

猜你喜欢

转载自www.cnblogs.com/jylsgup/p/9757399.html